微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Flutter blue 蓝牙扫描连接不稳定问题

@[Flutter Blue] @[Android]

Flutter blue Android 蓝牙扫描连接不稳定问题

主要和gatt close bug 有关

https://github.com/pauldemarco/Flutter_blue/blob/master/android/src/main/java/com/pauldemarco/Flutter_blue/FlutterBluePlugin.java

line: 318
case “disconnect”:
{
String deviceid = (String)call.arguments;
BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(deviceid);
int state = mBluetoothManager.getConnectionState(device, BluetoothProfile.GATT);
BluetoothDeviceCache cache = mDevices.remove(deviceid);
BluetoothDeviceCache cache = mDevices.get(deviceid);
if(cache != null) {
BluetoothGatt gattServer = cache.gatt;
gattServer.disconnect();
if(state == BluetoothProfile.STATE_disCONNECTED) {
//这里基本上不会到,因为状态还来不及改变
gattServer.close();
mDevices.remove(deviceid);// Only remove it after close
}
}
result.success(null);
break;
}
line 861:
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
log(LogLevel.DEBUG, "[onConnectionStateChange] status: " + status + " newState: " + newState);
if(newState == BluetoothProfile.STATE_disCONNECTED) {
String deviceid=gatt.getDevice().getAddress()
if(!mDevices.containsKey(deviceid)) {
//这里才是
gatt.close();
mDevices.remove(deviceid);// Only remove it after close
}
}
invokeMethodUIThread(“DeviceState”, Protomaker.from(gatt.getDevice(), newState).toByteArray());
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐