火山软件开发平台

标题: 大佬怎么取蓝牙设备已连接? [打印本页]

作者: 434673799    时间: 2020-7-6 12:46
标题: 大佬怎么取蓝牙设备已连接?
本帖最后由 434673799 于 2020-8-10 21:19 编辑

大佬怎么取蓝牙设备已连接?通过广播获取到蓝牙设备已连接.通过提示框提醒.谢谢大佬
  1. BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
  2.         Class<BluetoothAdapter> bluetoothAdapterClass = BluetoothAdapter.class;//得到BluetoothAdapter的Class对象
  3.         try {//得到连接状态的方法
  4.             Method method = bluetoothAdapterClass.getDeclaredMethod("getConnectionState", (Class[]) null);
  5.             //打开权限
  6.             method.setAccessible(true);
  7.             int state = (int) method.invoke(adapter, (Object[]) null);

  8.             if(state == BluetoothAdapter.STATE_CONNECTED){
  9.                 Log.i("BLUETOOTH","BluetoothAdapter.STATE_CONNECTED");
  10.                 Set<BluetoothDevice> devices = adapter.getBondedDevices();
  11.                 Log.i("BLUETOOTH","devices:"+devices.size());

  12.                 for(BluetoothDevice device : devices){
  13.                     Method isConnectedMethod = BluetoothDevice.class.getDeclaredMethod("isConnected", (Class[]) null);
  14.                     method.setAccessible(true);
  15.                     boolean isConnected = (boolean) isConnectedMethod.invoke(device, (Object[]) null);
  16.                     if(isConnected){
  17.                         Log.i("BLUETOOTH","connected:"+device.getName());
  18.                         deviceList.add(device);
  19.                     }
  20.                 }
  21.             }

  22.         } catch (Exception e) {
  23.             e.printStackTrace();
  24.         }
复制代码


作者: 创世魂    时间: 2020-8-13 11:30
火山自带的蓝牙通讯例程,无论是低功耗蓝牙,还是蓝牙通讯。都有蓝牙连接成功提示。。可以仔细看看。。

建议用低功耗蓝牙吧,这个使用起来更加简单方便
作者: 434673799    时间: 2020-8-14 18:45
创世魂 发表于 2020-8-13 11:30
火山自带的蓝牙通讯例程,无论是低功耗蓝牙,还是蓝牙通讯。都有蓝牙连接成功提示。。可以仔细看看。。

建 ...

谢谢  没有蓝牙设备已连接的返回
作者: qq419649693    时间: 2020-8-17 17:37
434673799 发表于 2020-8-14 18:45
谢谢  没有蓝牙设备已连接的返回

无名模块里面那个蓝牙模块,不是所有功能都有了吗???
作者: 飞仔の    时间: 2021-3-23 21:19
创世魂 发表于 2020-8-13 11:30
火山自带的蓝牙通讯例程,无论是低功耗蓝牙,还是蓝牙通讯。都有蓝牙连接成功提示。。可以仔细看看。。

建 ...

火山自带的蓝牙通讯例程     低功耗蓝牙例程   只能链接  不能收发**




欢迎光临 火山软件开发平台 (https://bbs.voldp.com/) Powered by Discuz! X3.4