【问题标题】:Android BluetoothGatt: Unhandled execption in callbackAndroid BluetoothGatt:回调中未处理的执行
【发布时间】:2015-02-09 10:42:06
【问题描述】:

我编写了一个 Android 应用程序 (4.4.2),它在大多数情况下都能正确连接/断开与 BLE 外围设备的连接。但是,每隔一段时间,我就会在 Bluetootgatt.java onClientConnectionState() 中收到关于 NullPointerException 的 LogCat 警告:

我的BluetoothGattCallback回调如下:

@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();
}
}

应用程序在连接时总是正确地转到 onConnectionStateChange newState==STATE_CONNECTED 回调,但是当它抛出异常时(在应用程序断开连接时),它永远不会在 onConnectionStateChange 回调中进行任何地方。我在 2013 Nexus 7 上运行此程序。为什么会发生此异常?

【问题讨论】:

  • 你成功了吗?

标签: android bluetooth


【解决方案1】:

这似乎是由于我在 onPause() 函数中调用 disconnect() 后跟 close() 引起的。我删除了 disconnect() 并且 close() 可以很好地处理断开连接,并且我再也看不到异常了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-10-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-21
    • 2019-10-21
    • 2013-03-01
    • 2019-08-23
    相关资源
    最近更新 更多