I am trying to read the initial state of a BLE device when I connect to it. Here is the code I should try to do:
@Override public void onServicesDiscovered(BluetoothGatt gatt, int status) { if(status == BluetoothGatt.GATT_SUCCESS) { Log.i(TAG, gatt.getDevice().toString() + "Discovered Service Status: " + gattStatusToString(status)); for(BluetoothGattService service : gatt.getServices()) { Log.i(TAG, "Discovered Service: " + service.getUuid().toString() + " with " + "characteristics:"); for(BluetoothGattCharacteristic characteristic : service.getCharacteristics()) { // Set notifiable if(!gatt.setCharacteristicNotification(characteristic, true)) { Log.e(TAG, "Failed to set notification for: " + characteristic.toString()); } // Enable notification descriptor BluetoothGattDescriptor descriptor = characteristic.getDescriptor(CCC_UUID); if(descriptor != null) { descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE); gatt.writeDescriptor(descriptor); } // Read characteristic if(!gatt.readCharacteristic(characteristic)) { Log.e(TAG, "Failed to read characteristic: " + characteristic.toString()); } } } } else { Log.d(TAG, "Discover Services status: " + gattStatusToString(status)); } }
But reading fails every time! Later, if I start reading based on interaction with the user interface, it reads just fine! Any ideas on what's going on here?
source share