1

I am currently working on an Android App to read the temperature from a remote device. At the remote device I have a server which is running a device Information Service and a Health Thermometer service. My Android App can scan through and detect both the service and their characteristics. I have a button on my App which on click should request for the temperature from the remote server. Below is the code that executes on Button click.

public void readCustomCharacteristic() {
    if (mBluetoothAdapter == null || mBluetoothGatt == null) {
        Log.w(TAG, "BluetoothAdapter not initialized");
        return;
    }
    /*check if the service is available on the device*/
    BluetoothGattService mCustomService = mBluetoothGatt.getService(UUID.fromString("00001809-0000-1000-8000-00805f9b34fb"));
    if(mCustomService == null){
        Log.w(TAG, "Custom BLE Service not found");
        return;
    }

    /*get the read characteristic from the service*/
    BluetoothGattCharacteristic mReadCharacteristic = mCustomService.getCharacteristic(UUID.fromString("00002a1c-0000-1000-8000-00805f9b34fb"));
    Log.i(TAG, mReadCharacteristic.toString());
    BluetoothGattDescriptor descriptor = mReadCharacteristic.getDescriptor(
            UUID.fromString(GattAttributes.CLIENT_CHARACTERISTIC_CONFIG));
    Log.i(TAG, descriptor.toString());
   // final byte[] data = descriptor.getValue();
   // Log.i(TAG, data.toString());
   // descriptor.setValue(BluetoothGattDescriptor.ENABLE_INDICATION_VALUE);
    if(mBluetoothGatt.readCharacteristic(mReadCharacteristic) == false){
        Log.w(TAG, "Failed to read characteristic");
    }
}

The code detects the service properly along with the descriptors, but on read characteristics the value returned is false and I get Failed to read characteristics as a log message. Is there something that I am not doing. Any advise on this would definitely help. Thanks

Embed_Programmer
  • 35
  • 1
  • 1
  • 7

1 Answers1

0

As you can see in the source code at https://android.googlesource.com/platform/frameworks/base/+/master/core/java/android/bluetooth/BluetoothGatt.java#919 there are basically three reasons the call returns false: 1. The characteristic doesn't have the read property 2. There is already an ongoing gatt operation running and you must wait for it to complete first 3. The gatt object has been closed

Emil
  • 16,784
  • 2
  • 41
  • 52