Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 796
L. Gangemi Avatar asked Sep 23 '16 12:09

L. Gangemi


1 Answers

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.

like image 102
Emil Avatar answered Nov 15 '22 16:11

Emil