【问题标题】:Android BLE App can ONLY subscribe characteristic notification for 1 BLE deviceAndroid BLE App 只能订阅 1 个 BLE 设备的特征通知
【发布时间】:2021-05-06 07:19:11
【问题描述】:

我是 Android 新手。我正在尝试从 2 个 BLE 设备订阅特征通知。现在,我的应用程序只能从第一个设备接收 BLE 数据。 我知道 BLE 是串行通信,所以我必须等到调用 onDescriptorWrite() 回调后才能启用第二个设备的通知。我们每次只能有 1 个 GATT 操作。 我的问题是:

  1. 如何修改onDescriptorWrite()Thread.sleep() 延迟方法没有帮助。

  2. 有人提到使用 queue 来添加/删除一些东西?

     public void setCharacteristicNotification(BluetoothGattCharacteristic characteristic, boolean enabled,int device) {
     if (mBluetoothAdapter == null || mBluetoothGatt == null || mBluetoothGatt1 == null) {
         Log.e(TAG, "BluetoothAdapter not initialized");
         return;
     }
     if(device == 0) {
         mBluetoothGatt.setCharacteristicNotification(characteristic, enabled);
         BluetoothGattDescriptor descriptor = characteristic.getDescriptor(UUID.fromString(SampleGattAttributes.CLIENT_CHARACTERISTIC_CONFIG));
         descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
         mBluetoothGatt.writeDescriptor(descriptor);
         try {
             Thread.sleep(100);
         } catch (InterruptedException e) {
             e.printStackTrace();
         }
         Log.e(TAG,"Device #0 is done for notification!");
     }
     else if(device == 1){
         mBluetoothGatt1.setCharacteristicNotification(characteristic,enabled);
         BluetoothGattDescriptor descriptor = characteristic.getDescriptor(UUID.fromString(SampleGattAttributes.CLIENT_CHARACTERISTIC_CONFIG));
         descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
         mBluetoothGatt.writeDescriptor(descriptor);
         try {
             Thread.sleep(100);
         } catch (InterruptedException e) {
             e.printStackTrace();
         }
         Log.e(TAG,"Device #1 is done for notification!");
     }
    

    }

     private final BluetoothGattCallback mGattCallback = new BluetoothGattCallback() {
     @Override
     public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
         String intentAction;
         if (newState == BluetoothProfile.STATE_CONNECTED) {
             intentAction = ACTION_GATT_CONNECTED;
             mConnectionState = STATE_CONNECTED;
             broadcastUpdate(intentAction);
             Log.e(TAG, "Connected to GATT server #0.");
             // Attempts to discover services after successful connection.
             Log.e(TAG, "Attempting to start service discovery #0:" + mBluetoothGatt.discoverServices());
    
         } else if (newState == BluetoothProfile.STATE_DISCONNECTED) {
             intentAction = ACTION_GATT_DISCONNECTED;
             mConnectionState = STATE_DISCONNECTED;
             Log.e(TAG, "Disconnected from GATT server #0.");
             broadcastUpdate(intentAction);
         }
     }
    
     @Override
     public void onServicesDiscovered(BluetoothGatt gatt, int status) {
         if (status == BluetoothGatt.GATT_SUCCESS) {
             broadcastUpdate(ACTION_GATT_SERVICES_DISCOVERED);
         } else {
             Log.e(TAG, "onServicesDiscovered received: " + status);
         }
     }
    
     @Override
     public void onCharacteristicRead(BluetoothGatt gatt,
                                      BluetoothGattCharacteristic characteristic,
                                      int status) {
         if (status == BluetoothGatt.GATT_SUCCESS) {
             broadcastUpdate(ACTION_DATA_AVAILABLE, characteristic,0);
         }
         try {
             Thread.sleep(700);
         }catch(InterruptedException ex) {
             Thread.currentThread().interrupt();
         }
         Log.e(TAG,"onCHAR READ");
     }
    
     @Override
     public void onCharacteristicChanged(BluetoothGatt gatt,
                                         BluetoothGattCharacteristic characteristic) {
         broadcastUpdate(ACTION_DATA_AVAILABLE, characteristic,0);
         try {
             Thread.sleep(700);
         }catch(InterruptedException ex) {
             Thread.currentThread().interrupt();
         }
         Log.e(TAG,"onCharacteristicChanged #0 = ACTION_DATA_AVAILABLE, Done! ");
     }
    
     @Override
     public void onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) {
         super.onDescriptorWrite(gatt, descriptor, status);
    
         // Do something here ????????????
    
         // Thread.sleep() delay method doesn't help
         try {
             Thread.sleep(700);
         }catch(InterruptedException ex) {
             Thread.currentThread().interrupt();
         }
         Log.e(TAG,"onDescriptorWrite #0, Done! ");
     }
    

    };

【问题讨论】:

  • 启用第一个特征的通知后是否会调用onDescriptorWrite
  • 嗨@M。科茨詹,感谢您的支持。是的,第一个特征调用 onDescriptorWrite()。这就是为什么我可以接收 1 个 BLE 设备数据。
  • 请不要在 BLE 代码中像 tihs 那样使用延迟/睡眠。它永远不会以正确的方式帮助您。无论如何,每个 BluetoothGatt 对象只有一个挂起操作正在进行的规则,因此如果它们分别针对一个设备,您可以安全地并行处理两个挂起操作。

标签: android bluetooth-lowenergy


【解决方案1】:
 mBluetoothGatt1.setCharacteristicNotification(characteristic,enabled);
 BluetoothGattDescriptor descriptor = characteristic.getDescriptor(UUID.fromString(SampleGattAttributes.CLIENT_CHARACTERISTIC_CONFIG));
 descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
 mBluetoothGatt.writeDescriptor(descriptor);

我认为应该是:

 mBluetoothGatt1.setCharacteristicNotification(characteristic,enabled);
 BluetoothGattDescriptor descriptor = characteristic.getDescriptor(UUID.fromString(SampleGattAttributes.CLIENT_CHARACTERISTIC_CONFIG));
 descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
 mBluetoothGatt1.writeDescriptor(descriptor);

【讨论】:

  • 嗨@Emil,这是一个错字,感谢您指出。如果我正确理解您的评论,您的意思是我需要为 2 个 BLE 外围设备提供 2 个 BluetoothGatt 对象?是的,我也是这样做的。我创建了private BluetoothGatt mBluetoothGatt; private BluetoothGatt mBluetoothGatt1; 然后,我有mGattCallback and mGattCallback1。我有 2 个相同的回调函数,只需将 mBluetoothGatt 更改为 mBluetoothGatt1。但仍然没有运气。我更新了上面的代码以包含 2 个 GattCallback() 函数。有人建议使用 Queue/ArrayList 方法。请问你知道怎么实现吗?
  • 正如我在对您的主题帖子的评论中提到的,您不需要在一个队列中为多个 BluetoothGatt 对象排队操作。您的代码中的某些部分可能存在您的帖子中未包含的错误。
猜你喜欢
  • 2015-01-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-01-01
  • 2015-12-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多