tosa
tosa

Reputation: 421

Android BluetoothGatt: Unhandled execption in callback

I wrote an Android app (4.4.2) which connects/disconnects properly most of the time to a BLE peripheral. However, every once in a while I get a LogCat warning of NullPointerException in Bluetootgatt.java onClientConnectionState(): error

My BluetoothGattCallback callback is as follows:

@Override
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) 
{
if (status == BluetoothGatt.GATT_SUCCESS && newState == BluetoothProfile.STATE_CONNECTED) 
{
     gatt.discoverServices();   // triggers onServicesDiscovered() callback
} 
else if (status == BluetoothGatt.GATT_SUCCESS && newState == BluetoothProfile.STATE_DISCONNECTED) 
{
      return;
} 
else if (status != BluetoothGatt.GATT_SUCCESS) 
{
     gatt.disconnect();
}
}

The app always properly goes to the onConnectionStateChange newState==STATE_CONNECTED callback upon connection, but when it throws the exception (upon app disconnect), it never goes anywhere in the onConnectionStateChange callback. I'm running this on a 2013 Nexus 7. Why is this exception occurring?

Upvotes: 2

Views: 1220

Answers (1)

tosa
tosa

Reputation: 421

This seems to be caused by my calling disconnect() followed by close() in my onPause() function. I removed disconnect() and the close() handles the disconnection fine, and I don't see the exception anymore.

Upvotes: 3

Related Questions