断开设备连接后未调用 OnConnectionStateChange
OnConnectionStateChange not called after disconnecting device
我在通过参数禁用蓝牙时遇到问题。
我使用 Broadcast Receiver 来监听这个动作,当它的状态关闭时,我调用一个方法来断开我的外围设备。
在我的日志中我只看到 D/BluetoothGatt: cancelOpen()
但根据 BluetoothGatt.class
服务调用我们的 BluetoothGattCallback
in disconnect
方法 onConnectionStateChanged
转向 DEVICE_DISCONNECTED
和我20 或 30 秒后也没有任何内容(主管超时)。
当我想用我的内部方法直接断开我的设备时,它工作正常。
这是断开连接的方法:
/**
* Disconnects an established connection, or cancels a connection attempt
* currently in progress.
*
* <p>Requires {@link android.Manifest.permission#BLUETOOTH} permission.
*/
public void disconnect() {
if (DBG) Log.d(TAG, "cancelOpen() - device: " + mDevice.getAddress());
if (mService == null || mClientIf == 0) return;
try {
mService.clientDisconnect(mClientIf, mDevice.getAddress());
} catch (RemoteException e) {
Log.e(TAG,"",e);
}
}
我用反射检查了 mClientIf 是否等于 0 或者 mService 是否为 Null 但他进入下一步并输入 try/catch。所以我不明白 Android 这里的行为
我根据这个库和这个 class 找到了解决方案:
https://github.com/NordicSemiconductor/Android-BLE-Library/blob/master/ble/src/main/java/no/nordicsemi/android/ble/BleManager.java
private final BroadcastReceiver mBluetoothStateBroadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(final Context context, final Intent intent) {
final int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.STATE_OFF);
final int previousState = intent.getIntExtra(BluetoothAdapter.EXTRA_PREVIOUS_STATE, BluetoothAdapter.STATE_OFF);
switch (state) {
case BluetoothAdapter.STATE_TURNING_OFF:
case BluetoothAdapter.STATE_OFF:
if (mConnected && previousState != BluetoothAdapter.STATE_TURNING_OFF && previousState != BluetoothAdapter.STATE_OFF) {
// The connection is killed by the system, no need to gently disconnect
mGattCallback.notifyDeviceDisconnected(mBluetoothDevice);
}
// Calling close() will prevent the STATE_OFF event from being logged (this receiver will be unregistered). But it doesn't matter.
close();
break;
}
}
};
我根据自己的需要对其进行了调整,但我也使用断开连接的设备信息发送了我的事件,并调用了我自己的关闭方法来释放资源并准备另一个连接。
我在通过参数禁用蓝牙时遇到问题。
我使用 Broadcast Receiver 来监听这个动作,当它的状态关闭时,我调用一个方法来断开我的外围设备。
在我的日志中我只看到 D/BluetoothGatt: cancelOpen()
但根据 BluetoothGatt.class
服务调用我们的 BluetoothGattCallback
in disconnect
方法 onConnectionStateChanged
转向 DEVICE_DISCONNECTED
和我20 或 30 秒后也没有任何内容(主管超时)。
当我想用我的内部方法直接断开我的设备时,它工作正常。
这是断开连接的方法:
/**
* Disconnects an established connection, or cancels a connection attempt
* currently in progress.
*
* <p>Requires {@link android.Manifest.permission#BLUETOOTH} permission.
*/
public void disconnect() {
if (DBG) Log.d(TAG, "cancelOpen() - device: " + mDevice.getAddress());
if (mService == null || mClientIf == 0) return;
try {
mService.clientDisconnect(mClientIf, mDevice.getAddress());
} catch (RemoteException e) {
Log.e(TAG,"",e);
}
}
我用反射检查了 mClientIf 是否等于 0 或者 mService 是否为 Null 但他进入下一步并输入 try/catch。所以我不明白 Android 这里的行为
我根据这个库和这个 class 找到了解决方案: https://github.com/NordicSemiconductor/Android-BLE-Library/blob/master/ble/src/main/java/no/nordicsemi/android/ble/BleManager.java
private final BroadcastReceiver mBluetoothStateBroadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(final Context context, final Intent intent) {
final int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.STATE_OFF);
final int previousState = intent.getIntExtra(BluetoothAdapter.EXTRA_PREVIOUS_STATE, BluetoothAdapter.STATE_OFF);
switch (state) {
case BluetoothAdapter.STATE_TURNING_OFF:
case BluetoothAdapter.STATE_OFF:
if (mConnected && previousState != BluetoothAdapter.STATE_TURNING_OFF && previousState != BluetoothAdapter.STATE_OFF) {
// The connection is killed by the system, no need to gently disconnect
mGattCallback.notifyDeviceDisconnected(mBluetoothDevice);
}
// Calling close() will prevent the STATE_OFF event from being logged (this receiver will be unregistered). But it doesn't matter.
close();
break;
}
}
};
我根据自己的需要对其进行了调整,但我也使用断开连接的设备信息发送了我的事件,并调用了我自己的关闭方法来释放资源并准备另一个连接。