蓝牙在断开连接时,存在的问题(建议优化)
Wheats opened this issue · 1 comments
Wheats commented
感谢作者无私分享,非常谢谢
1.框架存储蓝牙信息在 MultipleBluetoothController类
1.通过分析,看出意图,一个bleLruHashMap类,跟bleTempHashMap类。
bleTempHashMap保存临时准备连接的设备,
在设备连接成功后,转移至bleLruHashMap类,
问题在设备还处于bleTempHashMap类时,我们调用断开连接,这个时候bleLruHashMap 还未入列
从而引发问题,设备会走超时动作。app端在使用时,点击断开了,超时后还是发出来了通知,多弹了一个框
解决方法,内部没有太多时间深究,
1.MultipleBluetoothController类增加
public synchronized BreoBleBluetooth getBleTempBluetooth(BreoBleDevice breoBleDevice) {
if (breoBleDevice != null) {
if (bleTempHashMap.containsKey(breoBleDevice.getKey())) {
return bleTempHashMap.get(breoBleDevice.getKey());
}
}
return null;
}
public synchronized boolean isConnectIngDevice(BluetoothDevice bluetoothDevice) {
return bluetoothDevice != null && bleTempHashMap.containsKey(bluetoothDevice.getName() + bluetoothDevice.getAddress());
}
3.修改
public synchronized void disconnect(BreoBleDevice breoBleDevice) {
if (isContainDevice(breoBleDevice)) {
getBleBluetooth(breoBleDevice).disconnect();
}
if (isConnectIngDevice(breoBleDevice.getDevice())) {
getBleTempBluetooth(breoBleDevice).disConnectTemp();
}
}
1.在BreoBleBluetooth类增加删除断连接方法
逻辑为
1.断开超时接受(连接动作已产生)
2.移除临时缓存的bleTempHashMap
3.这里我已主动断开的方式,发送给上层app做处理。而不是超时啦
4.关闭需要关闭的方法,
public synchronized void disConnectTemp() {
//取消还未连接上的设备
if (mainHandler != null) {
mainHandler.removeMessages(BreoBleMsg.MSG_CONNECT_OVER_TIME);
lastState = LastState.CONNECT_DISCONNECT;
BreoCoreBleManager.getInstance().getMultipleBluetoothController().removeConnectingBle(BreoBleBluetooth.this);
if (bleGattCallback != null)
bleGattCallback.onDisConnected(true, breoBleDevice, bluetoothGatt, 1);
disconnect();
refreshDeviceCache();
closeBluetoothGatt();
removeRssiCallback();
removeMtuChangedCallback();
clearCharacterCallback();
mainHandler.removeCallbacksAndMessages(null);
}
}
到这里搞定啦,
rayzeng0221 commented
你那边会出现经常出现连接超时的问题吗