微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Android BLE:订阅多个特征通知

如何解决Android BLE:订阅多个特征通知

我正在尝试订阅 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? 我该怎么做?

谢谢。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。