Android 蓝牙设备的查找和连接(2)

private BroadcastReceiver receiver = new BroadcastReceiver() {      @Override      public void onReceive(Context context, Intent intent) {           String action = intent.getAction();           if (BluetoothDevice.ACTION_FOUND.equals(action)) {               BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);               System.out.println(device.getName());           }      }   }  

5.蓝牙设备的配对和状态监视

private BroadcastReceiver receiver = new BroadcastReceiver() {       @Override       public void onReceive(Context context, Intent intent) {           String action = intent.getAction();           if (BluetoothDevice.ACTION_FOUND.equals(action)) {               // 获取查找到的蓝牙设备                BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);               System.out.println(device.getName());               // 如果查找到的设备符合要连接的设备,处理                if (device.getName().equalsIgnoreCase(name)) {                   // 搜索蓝牙设备的过程占用资源比较多,一旦找到需要连接的设备后需要及时关闭搜索                    adapter.cancelDiscovery();                   // 获取蓝牙设备的连接状态                    connectState = device.getBondState();                   switch (connectState) {                       // 未配对                        case BluetoothDevice.BOND_NONE:                           // 配对                            try {                               Method createBondMethod = BluetoothDevice.class.getMethod("createBond");                               createBondMethod.invoke(device);                           } catch (Exception e) {                                e.printStackTrace();                           }                           break;                       // 已配对                        case BluetoothDevice.BOND_BONDED:                           try {                               // 连接                                connect(device);                           } catch (IOException e) {                               e.printStackTrace();                           }                           break;                   }               }          } else if(BluetoothDevice.ACTION_BOND_STATE_CHANGED.equals(action)) {               // 状态改变的广播                BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);               if (device.getName().equalsIgnoreCase(name)) {                    connectState = device.getBondState();                   switch (connectState) {                       case BluetoothDevice.BOND_NONE:                           break;                       case BluetoothDevice.BOND_BONDING:                           break;                       case BluetoothDevice.BOND_BONDED:                           try {                               // 连接                                connect(device);                           } catch (IOException e) {                               e.printStackTrace();                           }                           break;                   }               }           }       }   }  

6.蓝牙设备的连接

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wyxfxd.html