Nexus 4 上 BluetoothGatt.java 中的 NullPointerException

NullPointerException in BluetoothGatt.java on Nexus 4

我正在断开连接并关闭蓝牙 GATT 实例并在 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.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)

以下是紧接此错误之前的本机事件:

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 版本: Android 4.4.4

设备: Nexus 4

有人可以解释一下这是怎么回事吗?

在此位置 BluetoothGatt 尝试在您的 BluetoothGattCallback 实例上调用 onConnectionStateChange(...)

你覆盖了那个吗?

private final BluetoothGattCallback gattCallback = new BluetoothGattCallback() {
        @Override
        public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
        ...
        }
}

看来你不能同时调用这些方法。断开连接回调可能比执行 close() 方法晚到达。

您可以将mGatt.close();添加到onConnectionStateChange回调中以关闭连接。

check this link

一种解决方案是在 BluetoothGatt 对象上调用 disconnect()。 在 OnConnectionStateChange() 回调中调用 close()。

我在 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.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)

尝试从状态更改处理程序内部重新连接时:

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

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

}

并通过将重新连接移动到计时器线程解决了这个问题:

    case BluetoothProfile.STATE_DISCONNECTED:
      timer.schedule(
        new TimerTask() {
          @Override
          public void run() {
            device.connectGatt(...);
          }
        }, 3000);
      break;