目前当我尝试执行GATT操作并未成功完成该操作时,会自动开始配对过程(实际上是自动启动)。一旦配对过程完成并且我与远程设备建立了绑定关系,我就可以继续进行GATT操作,一切都正常。
我注意到在配对过程中:
1)如果我断开与远程设备的连接却没有关闭GATT客户端,那么它仍然能够成功配对,并且我进入BluetoothDevice.BOND_BONDED
状态,我的应用程序继续正常工作。
2)如果我断开连接并且关闭了GATT客户端,则一旦进入BluetoothDevice.BOND_BONDED
状态后,我的应用程序就会崩溃。
为什么在配对过程中关闭远程设备的GATT客户端会导致应用程序崩溃?这是正常现象还是我做错了什么?
这是获取远程设备绑定状态的广播接收器实现:
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
final String action = intent.getAction();
if (action.equals(BluetoothDevice.ACTION_BOND_STATE_CHANGED)) {
final int state = intent.getIntExtra(BluetoothDevice.EXTRA_BOND_STATE, BluetoothDevice.ERROR);
if(state == BluetoothDevice.BOND_BONDING){
DebugWrapper.debugMsg("On Bonding...", TAG);
mIsBonding = true;
onBonding();
} else if(state == BluetoothDevice.BOND_BONDED){
DebugWrapper.debugMsg("On Bonded", TAG);
mIsBonded = true;
mActivity.unregisterReceiver(mReceiver);
/*
* finish what we started
*/
if(mBluetoothGatt != null){
if(mOperationState == OperationState.READ_CHARACTERISTIC){
readCharacteristic(mCurrentCharacteristic);
} else if(mOperationState == OperationState.WRITE_CHARACTERISTIC){
writeCharacteristic(mCurrentCharacteristic);
} else if(mOperationState == OperationState.READ_DESCRIPTOR){
readDescriptor(mCurrentDescriptor);
} else if(mOperationState == OperationState.WRITE_DESCRIPTOR){
writeDescriptor(mCurrentDescriptor);
}
mOperationState = OperationState.NONE;
}
onBonded();
} else if(state == BluetoothDevice.BOND_NONE){
DebugWrapper.debugMsg("Not Bonded", TAG);
notBonded();
}
}
}
};
更新 LogCat 文件(链接到 Pastebin.com,因为文件太大无法在此处添加)