B-Rad
B-Rad

Reputation: 1557

Android BLE onCharacteristicRead and onCharacteristicChanged never called

I am trying to connect to a Bluetooth LE thermometer. Connecting to the device is working good. The only part hanging me up is the gattCallBack and it's onCharacteristicChanged / Read. the 'setNotification' and descriptor 'setValue' and 'writeDescriptor' all return true. The onCharacteristicChanged is never called to return a value.

I used a pretty handy little program from the Play Store called BLE Scanner to help me to give me more information about the device and it's services and characteristics.

enter image description here

This is why I simply hard coded service 2, characteristic 0. I just can't seem to figure out why after I writeDescriptor, I never see anything come back. The interesting thing is, I can use some of the other Characteristics (one being Temperature Interval) and I do receive a response (although the data is garbled.)

Also, out of curiosity, why are there 2 descriptors on this characteristic?

This code is contained in my MainActivity method. Not sure if that would make a difference here. I have looked at and tried several methods posted on here with no luck.

private final BluetoothGattCallback gattCallback = new BluetoothGattCallback()
{
    @Override
    public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState)
    { ... }

    @Override
    public void onServicesDiscovered(BluetoothGatt gatt, int status)
    {
        mGatt = gatt;

        List<BluetoothGattService> services = mGatt.getServices();
        Log.i("onServicesDiscovered", services.toString());
        
        BluetoothGattCharacteristic characteristic = services.get(2).getCharacteristics().get(0);

        mGatt.setCharacteristicNotification(characteristic, true);
        
        BluetoothGattDescriptor descriptor = characteristic.getDescriptor(UUID.fromString(CLIENT_CHARACTERISTIC_CONFIG));
        descriptor.setValue(BluetoothGattDescriptor.ENABLE_INDICATION_VALUE);
        
        mGatt.writeDescriptor(descriptor);
        
    }            

    @Override
    public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status)
    { ... }

    @Override
    public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic)
    { ... }
};

UPDATE: I decided to check the the onDescriptorWrite method and Log some information.

@Override            
public void onDescriptorWrite(BluetoothGatt gatt,  BluetoothGattDescriptor descriptor, int status)
    {
        Log.i("descriptorWRITE", Integer.toString(status));
    }

Interesting thing here is that status is returning 13 which is 'A write operation exceeds the maximum length of the attribute'.

I will be looking into this further.

Upvotes: 8

Views: 3974

Answers (2)

B-Rad
B-Rad

Reputation: 1557

I found the problem here. I was assuming that the Thermometer was using the standard BLE service and characteristic setup. It is not. They created their own Custom Characteristic. Once I switched to that Characteristic, the 'changed' method started firing.

Upvotes: 5

mpostal
mpostal

Reputation: 355

I think you are missing mGatt.readCharacteristic(characteristic).

When you call it, it will wait for the read to be finished and call onCharacteristicRead.

Upvotes: 0

Related Questions