Search code examples
androidandroid-ble

GATT onDescriptorRead - only one callback


I've initiated a BLE connection, in onServicesDiscovered I'm going to read out all descriptors. There are five of them and I proceed with this:

for (int n=0;n<descriptors.size();n++)
{
     gatt.readDescriptor(descriptors.get(n));
}

descriptors contains all descriptors... Then the read callback is called and I'm going to read out the descriptors value:

@Override
public void onDescriptorRead(BluetoothGatt gatt, final BluetoothGattDescriptor descriptor, int status)
{
     deviceInfoArray.add(new ItemSlideMenu(new String(descriptor.getValue()));
}

The problem is, that the read callback is only called once, instead of five times. Can you tell me why and how to fix this?


Solution

  • That's a great idea, thank you! Especially when you have different GATT operations. Nevertheless I solved it this way:

    @Override
    public void onDescriptorRead(BluetoothGatt gatt, final BluetoothGattDescriptor descriptor, int status)
        {
            deviceInfoArray.add(new ItemSlideMenu(new String(descriptor.getValue()), descriptor.getCharacteristic()));
            //Call this to update the adapter of deviceInfoList
            getActivity().runOnUiThread(updateAdapter);
            Log.d("xxx", new String(descriptor.getValue()));
    
            if(descriptors.size() > 1)
            {
                BluetoothGattDescriptor tempDescriptor = descriptors.get(1);
                descriptors.remove(1);
                mGatt.readDescriptor(tempDescriptor);
            }
        }
    

    onDescriptorRead is called recursive and terminates itselfs.