一、功能说明
蓝牙通信的主要功能设计为以下三个部分,分别是:
1、搜索附近蓝牙设备,并向周围设备发起蓝牙通信请求
2、实现设备之间通过蓝牙进行通信
3、用户自定义蓝牙设备能否被搜索
二、核心代码和步骤
1,首先开启蓝牙
2,搜索可用设备
3,创建蓝牙socket,获取输入输出流
4,读取和写入数据
5,断开连接关闭蓝牙
1、页面设计
list.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/title_paired_devices"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#666"
android:text="@string/title_paired_devices"
android:textColor="#fff"
android:visibility="visible" />
<ListView android:id="@+id/paired_devices"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="2" />
<TextView
android:id="@+id/title_new_devices"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#666"
android:text="@string/title_other_devices"
android:textColor="#fff"
android:visibility="visible" />
<ListView android:id="@+id/new_devices"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="2" />
<Button android:id="@+id/button_scan"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/button_scan" />
</LinearLayout>
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android