Android BLE:订阅多个特征通知

时间:2021-06-18 21:48:10

标签: android android-studio bluetooth-lowenergy

我正在尝试订阅 9 个不同的同步特征通知。 我的目标和最小 SDK 版本是 23

我的应用目前最多只能订阅 5 条通知,然后停止。但是,在使用 nRF Connect 应用程序时,我可以手动订阅所有 9 个。 订阅的 5 个特征正确触发了 onCharacteristicChanged() 回调。

这是我设置通知的代码:

numNotifications = 8;

public void setNotify(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic){
   boolean registered = gatt.setCharacteristicNotification(characteristic, true);       
    if(registered){
        BluetoothGattDescriptor descriptor = characteristic.getDescriptor(UUID_DESCRIPTOR);    
        descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);      
        gatt.writeDescriptor(descriptor);
    }
    numNotifications--;
}

在为第 9 个特征调用 setNotify() 后,我一次异步写入描述符一个特征。 (是的,我知道我可以使用循环而不是开关,并尝试使用相同的结果)。

@Override
    public void onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) {
        // Called when the descriptor is updated for notification
        if (status == BluetoothGatt.GATT_SUCCESS) {
            switch (numNotifications){
                case 7: setNotify(gatt ,chars.get(numNotifications));
                    break;
                case 6: setNotify(gatt ,chars.get(numNotifications));
                    break;
                case 5: setNotify(gatt ,chars.get(numNotifications));
                    break;
                case 4: setNotify(gatt ,chars.get(numNotifications));
                    break;
                case 3: setNotify(gatt ,chars.get(numNotifications));
                    break;
                case 2: setNotify(gatt ,chars.get(numNotifications));
                    break;
                case 1: setNotify(gatt ,chars.get(numNotifications));
                    break;
                case 0: setNotify(gatt ,chars.get(numNotifications));
                    break;
            }     
        }
    }

问题:

  1. 是否有更好的方式订阅所有 9 个特征?
  2. 是不是因为我的设备(Samsung A30s 2019)只能处理 5 个订阅 - 如果是这样,nRF Connect 如何做更多?
  3. 如果是我的设备,也许我可以在与 @Emil 在这里提到的同一应用程序中创建 2 个 BluetoothGatt 对象:Android Bluetooth Low Energy Characteristic notification count limit: does this vary by device? 我该怎么做?

谢谢。

0 个答案:

没有答案
相关问题