问题描述
当我从参数禁用蓝牙时,我遇到了一个问题.我使用广播接收器侦听此操作,当其状态关闭时,我调用一种方法断开与外围设备的连接.在我的日志中,我仅看到D/BluetoothGatt: cancelOpen()
,但是根据BluetoothGatt.class
,该服务在disconnect
方法中使用onConnectionStateChanged
调用我们的BluetoothGattCallback
变为DEVICE_DISCONNECTED
,并且20或30秒后我也什么也没有(主管超时)
I'm facing a problem when I disable bluetooth from parameter.I use a Broadcast Receiver to listen this action and when its state is turned off I call a method to disconnect my peripheral.In my log I only see D/BluetoothGatt: cancelOpen()
but according to BluetoothGatt.class
the service calls our BluetoothGattCallback
in disconnect
method with onConnectionStateChanged
turns to DEVICE_DISCONNECTED
and I have nothing after 20 or 30 seconds too (supervisor timeout).
当我想使用内部方法直接断开设备连接时,
When I want to disconnect my device directly with my inner method it works correctly.
这是断开连接的方法:
/**
* 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的行为
I checked with reflection if mClientIf equals 0 or if mService is Null but he goes to the next step and enter in the try/catch. So I don't understand the Android behavior here
推荐答案
我根据该库和此类找到了一个解决方案: https://github.com/NordicSemiconductor/Android-BLE-Library/blob/master/ble/src/main/java/no/nordicsemi/android/ble/BleManager.java
I found a solution according to this library and this 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;
}
}
};
我根据需要对其进行了调整,但是我还使用断开的设备信息来调度事件,并调用自己的close方法来释放资源并为另一个连接做准备.
I adapted it to my need but I also dispatch my event with my disconnected device info and call my own close method to release resources and prepare for another connection.
这篇关于断开设备连接后未调用OnConnectionStateChange的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!