androidbluetooth-lowenergygattcharacteristics

Android read characteristics issue


i'm using the code from official android developers website and find difficulties in reading charactaristics

i use this code for creating the gatt characteristic read call in DeviceControlActivity class at displayGattServices function when iterating all the uuid's:

mBluetoothLeService.readCharacteristic(new BluetoothGattCharacteristic(
                    UUID.fromString(uuid),
                    BluetoothGattCharacteristic.PERMISSION_READ,
                    BluetoothGattCharacteristic.PROPERTY_READ));

the readCharacteristic function int BluetoothLeService calss is:

public void readCharacteristic(BluetoothGattCharacteristic characteristic) {
    if (mBluetoothAdapter == null || mBluetoothGatt == null) {
        Log.w(TAG, "BluetoothAdapter not initialized");
        return;
    }
    mBluetoothGatt.readCharacteristic(characteristic);
}

and the callback also in BluetoothLeService class is:

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

        if (status == BluetoothGatt.GATT_SUCCESS) {
            broadcastUpdate(ACTION_DATA_AVAILABLE, characteristic);
        }

i put a break point at the callback and i never get stopped there... could it be the permission or property? something else?... does somebody have a working example of it?


Solution

  • This is normally not how you read Characteristics. The steps that you should follow are: - Find the device - Connect to the device - Discover services - Pick the characteristic you want from your service - Use that characteristic to read the value

    You have to first get a reference to the characteristic within the service to read it instead of just creating a new characteristic using the constructor.

    Let me know if you need this to be clarified further.