BLE Android,不能启用超过1个有关读取特征的通知 [英] BLE Android, can't enable more than 1 notify on read characteristics

查看:186
本文介绍了BLE Android,不能启用超过1个有关读取特征的通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个Android应用程序,该应用程序将打开Android设备与BLE外围设备(简单的发送器)之间的BLE连接.

I'm developing an Android application which opens a BLE connection between the Android device and a BLE pheripheral (a simple transmitter).

外围设备被编程为具有多个我发现的读取特征. 当我尝试启用通知时出现问题.

The peripheral is programmed to have multiple reading characterics which I found. The problem shows up when I try to enable the notification.

第一个总是返回true,然后开始触发我的notify回调,其他的总是返回false.

The first always returns true, and than it starts to trigger my notify callback, the others always return a false value.

 List<BluetoothGattDescriptor> descrittoriDellaChar = getListaDescrittoriDaCharact(charact);
            Boolean status = null;
            for (int i = 0; i < descrittoriDellaChar.size(); i++) {
                BluetoothGattDescriptor TargetDescriptor = descrittoriDellaChar.get(i);
                byte[] valore = TargetDescriptor.getValue();
                if (valore != BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE) {
                    getCharDiLettura().add(charact);
                    TargetDescriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
                    //TargetDescriptor.setValue(BluetoothGattDescriptor.ENABLE_INDICATION_VALUE);
                    boolean success=false;
                    while(success==false) {
                         success = gattGlobale.writeDescriptor(TargetDescriptor);

                    }
                    status = gattGlobale.setCharacteristicNotification(charact, true);
                }
            }
   boolean prio= gattGlobale.requestConnectionPriority(gattGlobale.CONNECTION_PRIORITY_HIGH);

我使用相同的方法,因为我只有1个特征可以读取,但现在它不再起作用了.

I was using the same method since I had just 1 characteristic to read, and now it doesn't work anymore.

推荐答案

同步发送读写请求不起作用,因为android一次仅允许一个挂起的GATT操作(这就是它返回false的原因).一旦前一个请求的回调(onCharacteristicRead/onCharacteristicWrite/onDescriptorWrite)到达后,您就必须以某种方式使工作入队并继续发送下一个请求.

Sending read and write requests one after one other synchronously does not work since android only allows one pending GATT operation at a time (that's the reason it returns false). You must somehow enqueue the work and continue sending the next request once the callback (onCharacteristicRead/onCharacteristicWrite/onDescriptorWrite) of the previous request arrives.

这篇关于BLE Android,不能启用超过1个有关读取特征的通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆