1

I am disconnecting and closing a bluetooth GATT instance and see the following in logcat:

07-22 09:33:20.699    5095-5113/com.assaabloy.stg.cliqconnect.android W/BluetoothGatt﹕ Unhandled exception in callback
java.lang.NullPointerException
        at android.bluetooth.BluetoothGatt$1.onClientConnectionState(BluetoothGatt.java:168)
        at android.bluetooth.IBluetoothGattCallback$Stub.onTransact(IBluetoothGattCallback.java:71)
        at android.os.Binder.execTransact(Binder.java:404)
        at dalvik.system.NativeStart.run(Native Method)

Here are the native events that immediately precede this error:

07-22 09:33:20.689    1260-1277/? D/BtGatt.GattService﹕ clientDisconnect() - address=84:EB:18:44:D2:04, connId=9
07-22 09:33:20.689    1260-1277/? D/BtGatt.btif﹕ btif_gattc_close
07-22 09:33:20.689    1260-1322/? D/BtGatt.btif﹕ btgattc_handle_event: Event 1005
07-22 09:33:20.689    1260-1568/? E/bt-btif﹕ Do not find the bg connection mask for the remote device
07-22 09:33:20.689    1260-1322/? D/BtGatt.btif﹕ btif_gattc_upstreams_evt: Event 5
07-22 09:33:20.689    1260-1322/? D/BtGatt.GattService﹕ onDisconnected() - clientIf=9, connId=9, address=84:EB:18:44:D2:04
07-22 09:33:20.689    1260-1328/? D/BtGatt.GattService﹕ unregisterClient() - clientIf=9
07-22 09:33:20.689    1260-1328/? D/BtGatt.btif﹕ btif_gattc_unregister_app

OS version: Android 4.4.4

Device: Nexus 4

Can someone please explain what's going on?

Alix
  • 2,630
  • 30
  • 72
  • No code? How can we help? – Jared Burrows Jul 22 '15 at 20:07
  • Since the crash happens in the native BLE stack, I wasn't sure that my code was relevant for finding out the cause (this scenario works fine on other devices). But basically i have a BluetoothGatt instance and have invoked `disconnect()` and then immediately after `close()` – Alix Jul 22 '15 at 21:02
  • Now this is weird. `close()` calls `unregisterApp()` which sets the callback to `null`. It seems as if the disconnect is either too "slow" or does not go through so that the `onConnectionStateChange` callback gets called after the app is unregistered (thus the callback is already set to `null`).. odd. Very curious for the sourcecode. – Dominik Gebhart Jul 22 '15 at 22:18

4 Answers4

1

It seems you can't call these method together. The disconnection callback might arrive later than close() method performed.

You can add mGatt.close(); into onConnectionStateChange callback to close the connection.

check this link

Sushil Sharma
  • 2,321
  • 3
  • 29
  • 49
Zero
  • 2,764
  • 1
  • 17
  • 20
  • Your suggestion is what is currently implemented and I haven't seen this problem anymore. Maybe this solves it then... I'll award you the right answer meanwhile – Alix Nov 09 '15 at 07:35
0

At this position BluetoothGatt tries to call onConnectionStateChange(...) on your BluetoothGattCallback instance.

Have you overridden that one?

private final BluetoothGattCallback gattCallback = new BluetoothGattCallback() {
        @Override
        public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
        ...
        }
}
Dominik Gebhart
  • 2,980
  • 1
  • 16
  • 28
  • Yes,I can confirm that this callback has been overriden – Alix Jul 22 '15 at 21:01
  • Add some debug output to it. Does it get called on Connect/Disconnect? Can you supply the bluetooth related code? – Dominik Gebhart Jul 22 '15 at 21:04
  • I can get back to you on that, but I'm pretty sure that it does get called on connect – Alix Jul 22 '15 at 21:05
  • 1
    You can try to set a breakpoint in BluetoothGatt.java line 181, where it tries to call `mCallback.onConnectionStateChange(BluetoothGatt.this, status, profileState);` and see if mCallback is `null`, if yes (it should as the error hints on that) try to find out why. – Dominik Gebhart Jul 22 '15 at 21:10
  • Can you try to comment out the `close()` and check again if the exception still occure? (To verify the call to close is the reason why the callback gets set to `null`) – Dominik Gebhart Jul 22 '15 at 22:38
  • Unfortunately, I've not been able to reproduce this problem – Alix Jul 27 '15 at 13:44
0

I got a similar exception on Samsung SM G318H, "Trend 2 Lite":

D/BluetoothGatt( 4182): onClientConnectionState() - status=0 clientIf=6 device=D0:5F:B8:57:FE:33
W/BluetoothGatt( 4182): Unhandled exception in callback
W/BluetoothGatt( 4182): java.lang.NullPointerException
W/BluetoothGatt( 4182): at android.bluetooth.BluetoothGatt$1.onClientConnectionState(BluetoothGatt.java:172)
W/BluetoothGatt( 4182): at android.bluetooth.IBluetoothGattCallback$Stub.onTransact(IBluetoothGattCallback.java:71)
W/BluetoothGatt( 4182): at android.os.Binder.execTransact(Binder.java:404)
W/BluetoothGatt( 4182): at dalvik.system.NativeStart.run(Native Method)

when trying to reconnect from inside the state change handler:

public final void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {

  switch (newState) {
     case BluetoothProfile.STATE_DISCONNECTED:
       device.connectGatt(...);
       break;
  }

}

and solved it by moving the reconnect to a timer thread:

    case BluetoothProfile.STATE_DISCONNECTED:
      timer.schedule(
        new TimerTask() {
          @Override
          public void run() {
            device.connectGatt(...);
          }
        }, 3000);
      break;
zut
  • 806
  • 4
  • 12
-1

One solution is just invoke disconnect() on BluetoothGatt object. Call the close() in OnConnectionStateChange() callback.

Naren
  • 2,706
  • 1
  • 21
  • 15