2015-02-08 58 views
2

我编写了一个Android应用程序(4.4.2),它可以在大多数情况下正常连接/断开与BLE外设的连接/断开连接。然而,每过一段时间,我得到NullPointerException异常的logcat的预警Bluetootgatt.java onClientConnectionState(): errorAndroid BluetoothGatt:在回调中未处理的执行

我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上运行此操作。为什么会发生此异常?

+0

你让它工作? – shamaleyte 2015-12-27 11:56:23

回答

3

这似乎是由我在我的onPause()函数中调用disconnect()和close()引起的。我删除了断开连接()和关闭()处理断开罚款,我没有看到异常了。