434673799 发表于 2020-7-6 12:46:06

大佬怎么取蓝牙设备已连接?

本帖最后由 434673799 于 2020-8-10 21:19 编辑

大佬怎么取蓝牙设备已连接?通过广播获取到蓝牙设备已连接.通过提示框提醒.谢谢大佬:handshakeBluetoothAdapter 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();
      }

创世魂 发表于 2020-8-13 11:30:14

火山自带的蓝牙通讯例程,无论是低功耗蓝牙,还是蓝牙通讯。都有蓝牙连接成功提示。。可以仔细看看。。

建议用低功耗蓝牙吧,这个使用起来更加简单方便

434673799 发表于 2020-8-14 18:45:54

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

建 ...

谢谢没有蓝牙设备已连接的返回

qq419649693 发表于 2020-8-17 17:37:12

434673799 发表于 2020-8-14 18:45
谢谢没有蓝牙设备已连接的返回

无名模块里面那个蓝牙模块,不是所有功能都有了吗???

飞仔の 发表于 2021-3-23 21:19:05

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

建 ...

火山自带的蓝牙通讯例程   低功耗蓝牙例程   只能链接不能收发**
页: [1]
查看完整版本: 大佬怎么取蓝牙设备已连接?