JoSeBu
JoSeBu

Reputation: 305

W/BluetoothGatt: Unhandled exception in callback

I will explain my problem, I charge a list of BLEs availables an when a find one in concret i do the next

gatt = device.connectGatt(MainActivity.this, true, gattCallback);

and everything is fine there (or so I think).

But when I run the code below: gatt.getService(BLOOD_PRESURE_SERVICE_UUID).getCharacteristic(BLOOD_PRESURE_MEASUREMENT_CHAR_UUID);

I got this error:

W/BluetoothGatt: Unhandled exception in callback java.lang.NullPointerException: Attempt to invoke virtual method 'android.bluetooth.BluetoothGattCharacteristic android.bluetooth.BluetoothGattService.getCharacteristic(java.util.UUID)' on a null object reference

And I have no idea why, the gatt object comes with content, so it is not null, or shouldn't

BluetoothGattCallback gattCallback = new BluetoothGattCallback() {
@Override
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
   super.onConnectionStateChange(gatt, status, newState);
   if (newState == BluetoothProfile.STATE_CONNECTED){
      gatt.discoverServices();
   }
}

@Override
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
   super.onServicesDiscovered(gatt, status);
   BluetoothGattCharacteristic characteristic =
      gatt.getService(BLOOD_PRESURE_SERVICE_UUID)
         .getCharacteristic(BLOOD_PRESURE_MEASUREMENT_CHAR_UUID);
   gatt.setCharacteristicNotification(characteristic, true);

   BluetoothGattDescriptor descriptor =
      characteristic.getDescriptor(CLIENT_CHARACTERISTIC_CONFIG_UUID);

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

I have read, in another entry that a person happened that the CallBack was empty and that's why the error jumped. This user comments on the solution they provide, Unhandled exception in callback while trying to connect with BluetoothGatt But if this is my case, I don't know how to solve it.

Please help. Thank you

Upvotes: 0

Views: 813

Answers (1)

Emil
Emil

Reputation: 18517

The object you try to call getCharacteristic on is null. That means gatt.getService(BLOOD_PRESURE_SERVICE_UUID) returned null. That means the device you connected to doesn't have the blood pressure gatt service. (And you misspelled pressure...)

Upvotes: 1

Related Questions