0
votes

I have a problem with CharacteristicNotification, I follow docs from android and search in many stackoverflow post. All my code works until I try to get data from my device, when I subscribe to notification I get this log : BluetoothGatt: setCharacteristicNotification() But my methods was never call

My UUID are good and my service send data I already check with other application. I enable location for my app.

I have this permission in Manifest :

uses-permission android:name="android.permission.BLUETOOTH" android:required="true"
uses-permission android:name="android.permission.BLUETOOTH_ADMIN" uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"

I have try with 3 mobiles all are samsung but different Android version

I really don't understand what happen

Here my callback :

private final BluetoothGattCallback gattCallback = new BluetoothGattCallback() {
    @Override
    public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
        switch (newState) {
            case BluetoothProfile.STATE_CONNECTED:
                Log.i("gattCallback", "STATE_CONNECTED");
                gatt.discoverServices();
                break;
            case BluetoothProfile.STATE_DISCONNECTED:
                Log.e("gattCallback", "STATE_DISCONNECTED");
                break;
            default:
                Log.e("gattCallback", "STATE_OTHER");
        }
    }

    @Override
    public void onServicesDiscovered(BluetoothGatt gatt, int status) {
        Log.i("onServicesDiscovered", "connection to service");
        BluetoothGattCharacteristic characteristic =
                gatt.getService(UUID.fromString("a389d578-6285-4b3d-a9dc-b83a2ba4c095"))
                        .getCharacteristic(UUID.fromString("9ecbe31b-f77e-4886-a0a8-c416906387e7"));
        gatt.readCharacteristic(characteristic);

        BluetoothGattDescriptor descriptor = characteristic.getDescriptor(UUID.fromString("00002902-0000-1000-8000-00805F9B34FB"));

        descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
        descriptor.setValue(BluetoothGattDescriptor.ENABLE_INDICATION_VALUE);
        gatt.writeDescriptor(descriptor);

        gatt.setCharacteristicNotification(characteristic, true);
    }

    @Override
    public void onCharacteristicRead(BluetoothGatt gatt,
                                     BluetoothGattCharacteristic
                                             characteristic, int status) {
        Log.i("onCharacteristicRead", "read");
    }

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

        Log.i("onCharacteristicChanged", "changement");
    }
};
1
Have you got proper permissions in manifest? What is your phone android version?chebad
Yes I have all permission : <uses-permission android:name="android.permission.BLUETOOTH" android:required="true" /> <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" /> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />, it's samsung 6 edge and android 7.0Thomas.O

1 Answers

1
votes

Two errors:

1.

descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);

descriptor.setValue(BluetoothGattDescriptor.ENABLE_INDICATION_VALUE);

The second call overwrites the first value.

  1. You must wait for the read callback before you start the descriptor write operation. Generally, you must always wait for the GATT callback before you can start the next operation.