//允许程序连接到已配对的蓝牙设备
<uses-permission android:name="android.permission.BLUETOOTH"/>
//允许程序发现和配对蓝牙设备
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
蓝牙模块的串口(规定好的)
String SPP_UUID = "00001101-0000-1000-8000-00805F9B34FB";
//蓝牙开关
// 初始化本机蓝牙功能
BluetoothAdapter btAdapter = BluetoothAdapter.getDefaultAdapter();
//打开蓝牙
btAdapt.enable();
//关闭蓝牙
btAdapt.disable();
//已经配对的蓝牙
btAdapt.getState()//得到当前设备的蓝牙状态
//先判断当前蓝牙设备是否是打开状态
if (btAdapt.getState() == BluetoothAdapter.STATE_OFF) {
Toast.makeText(MainActivity.this, "请先打开蓝牙", 1).show();
return;
}
// 获得以经匹配过得蓝牙设备,是一个数组
Object[] lstDevice = btAdapt.getBondedDevices().toArray();
//循环取出每一个设备的对象
for (int i = 0; i < lstDevice.length; i++) {
BluetoothDevice device = (BluetoothDevice) lstDevice[i];
//将设备信息取出,展示在listView上
String str = "已配对设备 | " + device.getName() + "|"<