BLE Android, can't enable more than 1 notify on read characteristics

余生长醉 提交于 2019-12-10 17:47:03

问题


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.

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);

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


回答1:


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.



来源:https://stackoverflow.com/questions/39661417/ble-android-cant-enable-more-than-1-notify-on-read-characteristics

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!