|
本帖最后由 434673799 于 2020-8-10 21:19 编辑
大佬怎么取蓝牙设备已连接?通过广播获取到蓝牙设备已连接.通过提示框提醒.谢谢大佬- BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
- Class<BluetoothAdapter> bluetoothAdapterClass = BluetoothAdapter.class;//得到BluetoothAdapter的Class对象
- try {//得到连接状态的方法
- Method method = bluetoothAdapterClass.getDeclaredMethod("getConnectionState", (Class[]) null);
- //打开权限
- method.setAccessible(true);
- int state = (int) method.invoke(adapter, (Object[]) null);
-
- if(state == BluetoothAdapter.STATE_CONNECTED){
- Log.i("BLUETOOTH","BluetoothAdapter.STATE_CONNECTED");
- Set<BluetoothDevice> devices = adapter.getBondedDevices();
- Log.i("BLUETOOTH","devices:"+devices.size());
-
- for(BluetoothDevice device : devices){
- Method isConnectedMethod = BluetoothDevice.class.getDeclaredMethod("isConnected", (Class[]) null);
- method.setAccessible(true);
- boolean isConnected = (boolean) isConnectedMethod.invoke(device, (Object[]) null);
- if(isConnected){
- Log.i("BLUETOOTH","connected:"+device.getName());
- deviceList.add(device);
- }
- }
- }
-
- } catch (Exception e) {
- e.printStackTrace();
- }
复制代码
|
|