fix: 修复添加完锁之后立马蓝牙开锁无法打开的问题
This commit is contained in:
parent
baeb3c139a
commit
f73943785f
@ -119,8 +119,7 @@ class BlueManage {
|
||||
_connectionStateSubscription?.cancel();
|
||||
_connectionStateSubscription = null;
|
||||
|
||||
_connectionStateSubscription =
|
||||
bluetoothConnectDevice!.connectionState.listen((BluetoothConnectionState state) async {
|
||||
_connectionStateSubscription = bluetoothConnectDevice!.connectionState.listen((BluetoothConnectionState state) async {
|
||||
bluetoothConnectionState = state;
|
||||
AppLog.log('蓝牙连接回调状态:$state');
|
||||
});
|
||||
@ -159,26 +158,20 @@ class BlueManage {
|
||||
// AppLog.log('startScanSingle 蓝牙状态 系统蓝牙状态:$_adapterState 蓝牙连接状态:$bluetoothConnectionState');
|
||||
if (_adapterState == BluetoothAdapterState.on) {
|
||||
try {
|
||||
BuglyTool.uploadException(
|
||||
message: '开始指定设备名称的扫描蓝牙设备', detail: '调用方法是:startScanSingle 指定设备名称是:$deviceName', upload: false);
|
||||
BuglyTool.uploadException(message: '开始指定设备名称的扫描蓝牙设备', detail: '调用方法是:startScanSingle 指定设备名称是:$deviceName', upload: false);
|
||||
//android 扫描比较慢,取样只要 3 分之一
|
||||
final int divisor = Platform.isAndroid ? 3 : 1;
|
||||
FlutterBluePlus.startScan(
|
||||
continuousDivisor: divisor,
|
||||
continuousUpdates: true,
|
||||
withKeywords: <String>[deviceName],
|
||||
timeout: Duration(seconds: timeout));
|
||||
continuousDivisor: divisor, continuousUpdates: true, withKeywords: <String>[deviceName], timeout: Duration(seconds: timeout));
|
||||
final Completer<dynamic> completer = Completer<dynamic>();
|
||||
final StreamSubscription<List<ScanResult>> subscription =
|
||||
FlutterBluePlus.scanResults.listen((List<ScanResult> results) {
|
||||
final bool isExit = results.any((ScanResult element) =>
|
||||
(element.device.platformName == deviceName) || (element.advertisementData.advName == deviceName));
|
||||
final StreamSubscription<List<ScanResult>> subscription = FlutterBluePlus.scanResults.listen((List<ScanResult> results) {
|
||||
final bool isExit = results
|
||||
.any((ScanResult element) => (element.device.platformName == deviceName) || (element.advertisementData.advName == deviceName));
|
||||
final int milliseconds = DateTime.now().millisecondsSinceEpoch - start.millisecondsSinceEpoch;
|
||||
AppLog.log('扫描到的设备数:${results.length} 是否查找到 $isExit 以查找$milliseconds毫秒');
|
||||
BuglyTool.uploadException(
|
||||
message: '指定设备名称的扫描蓝牙设备 监听扫描结果',
|
||||
detail:
|
||||
'startScanSingle$deviceName 监听扫描结果 是否查找到 $isExit 以查找$milliseconds毫秒 扫描到的设备数:${results.length} results:$results',
|
||||
detail: 'startScanSingle$deviceName 监听扫描结果 是否查找到 $isExit 以查找$milliseconds毫秒 扫描到的设备数:${results.length} results:$results',
|
||||
upload: false);
|
||||
if (isExit) {
|
||||
for (final ScanResult scanResult in results) {
|
||||
@ -215,8 +208,7 @@ class BlueManage {
|
||||
completer.complete();
|
||||
}
|
||||
}, onError: (e) {
|
||||
BuglyTool.uploadException(
|
||||
message: '指定设备名称的扫描蓝牙设备 监听扫描结果失败', detail: '打印失败问题 e:${e.toString()}', upload: false);
|
||||
BuglyTool.uploadException(message: '指定设备名称的扫描蓝牙设备 监听扫描结果失败', detail: '打印失败问题 e:${e.toString()}', upload: false);
|
||||
AppLog.log('扫描失败:$e');
|
||||
});
|
||||
FlutterBluePlus.cancelWhenScanComplete(subscription);
|
||||
@ -224,8 +216,7 @@ class BlueManage {
|
||||
scanDevicesCallBack(scanDevices);
|
||||
subscription.cancel();
|
||||
} catch (e) {
|
||||
BuglyTool.uploadException(
|
||||
message: '指定设备名称的扫描蓝牙设备 内部逻辑整形失败', detail: 'tartScanSingle内部逻辑整形失败 e:${e.toString()}', upload: false);
|
||||
BuglyTool.uploadException(message: '指定设备名称的扫描蓝牙设备 内部逻辑整形失败', detail: 'tartScanSingle内部逻辑整形失败 e:${e.toString()}', upload: false);
|
||||
AppLog.log('扫描失败');
|
||||
}
|
||||
} else {
|
||||
@ -242,16 +233,14 @@ class BlueManage {
|
||||
}
|
||||
|
||||
/// 开始扫描蓝牙设备
|
||||
Future<void> startScan(int timeout, DeviceType deviceType, ScanDevicesCallBack scanDevicesCallBack,
|
||||
{List<Guid>? idList}) async {
|
||||
Future<void> startScan(int timeout, DeviceType deviceType, ScanDevicesCallBack scanDevicesCallBack, {List<Guid>? idList}) async {
|
||||
FlutterBluePlus.isSupported.then((bool isAvailable) async {
|
||||
if (isAvailable) {
|
||||
AppLog.log('startScan 蓝牙状态 系统蓝牙状态:$_adapterState 蓝牙连接状态:$bluetoothConnectionState');
|
||||
if (_adapterState == BluetoothAdapterState.on) {
|
||||
try {
|
||||
FlutterBluePlus.startScan(timeout: Duration(seconds: timeout));
|
||||
final StreamSubscription<List<ScanResult>> subscription =
|
||||
FlutterBluePlus.scanResults.listen((List<ScanResult> results) {
|
||||
final StreamSubscription<List<ScanResult>> subscription = FlutterBluePlus.scanResults.listen((List<ScanResult> results) {
|
||||
scanDevices.clear();
|
||||
for (final ScanResult scanResult in results) {
|
||||
if (scanResult.advertisementData.serviceUuids.isNotEmpty) {
|
||||
@ -420,16 +409,13 @@ class BlueManage {
|
||||
} else {
|
||||
BuglyTool.uploadException(
|
||||
message: '点击按钮 蓝牙已经连接 下一步扫描连接蓝牙',
|
||||
detail:
|
||||
'blueSendData 直接回调状态 蓝牙连接状态bluetoothConnectionState:$bluetoothConnectionState deviceName:$deviceName',
|
||||
detail: 'blueSendData 直接回调状态 蓝牙连接状态bluetoothConnectionState:$bluetoothConnectionState deviceName:$deviceName',
|
||||
upload: false);
|
||||
stateCallBack(bluetoothConnectionState!);
|
||||
}
|
||||
} else {
|
||||
BuglyTool.uploadException(
|
||||
message: '点击按钮 蓝牙未打开',
|
||||
detail: 'blueSendData 蓝牙未打开--_adapterState:${BluetoothAdapterState.on} deviceName:$deviceName',
|
||||
upload: false);
|
||||
message: '点击按钮 蓝牙未打开', detail: 'blueSendData 蓝牙未打开--_adapterState:${BluetoothAdapterState.on} deviceName:$deviceName', upload: false);
|
||||
try {
|
||||
stateCallBack(BluetoothConnectionState.disconnected);
|
||||
openBlue();
|
||||
@ -442,8 +428,7 @@ class BlueManage {
|
||||
}
|
||||
}
|
||||
} else {
|
||||
BuglyTool.uploadException(
|
||||
message: '点击按钮 蓝牙状态不可用', detail: 'blueSendData 蓝牙状态不可用--isAvailable:$isAvailable', upload: false);
|
||||
BuglyTool.uploadException(message: '点击按钮 蓝牙状态不可用', detail: 'blueSendData 蓝牙状态不可用--isAvailable:$isAvailable', upload: false);
|
||||
stateCallBack(BluetoothConnectionState.disconnected);
|
||||
AppLog.log('开始扫描 蓝牙不可用,不能进行蓝牙操作');
|
||||
}
|
||||
@ -451,8 +436,7 @@ class BlueManage {
|
||||
}
|
||||
|
||||
/// 连接
|
||||
Future<void> _connect(String deviceName, ConnectStateCallBack connectStateCallBack,
|
||||
{bool isAddEquipment = false}) async {
|
||||
Future<void> _connect(String deviceName, ConnectStateCallBack connectStateCallBack, {bool isAddEquipment = false}) async {
|
||||
connectDeviceName = deviceName;
|
||||
// 当前已扫描到的缓存设备
|
||||
final List<ScanResult> devicesList = scanDevices;
|
||||
@ -533,8 +517,8 @@ class BlueManage {
|
||||
|
||||
//查找缓存里面是否有设备
|
||||
bool isExistScanDevices(String connectDeviceName) {
|
||||
final bool isExistDevice = scanDevices.any((ScanResult element) =>
|
||||
element.device.platformName == connectDeviceName || element.advertisementData.advName == connectDeviceName);
|
||||
final bool isExistDevice = scanDevices
|
||||
.any((ScanResult element) => element.device.platformName == connectDeviceName || element.advertisementData.advName == connectDeviceName);
|
||||
return isExistDevice;
|
||||
}
|
||||
|
||||
@ -545,11 +529,8 @@ class BlueManage {
|
||||
bool isAddEquipment = false, // 是否是添加设备之前
|
||||
bool isReconnect = true, // 是否是重连
|
||||
}) async {
|
||||
// 判断数组列表里面是否有这个设备
|
||||
// AppLog.log("devicesList:$devicesList");
|
||||
|
||||
final int knownDeviceIndex = devicesList.indexWhere(
|
||||
(ScanResult d) => (d.device.platformName == deviceName) || (d.advertisementData.advName == deviceName));
|
||||
final int knownDeviceIndex =
|
||||
devicesList.indexWhere((ScanResult d) => (d.device.platformName == deviceName) || (d.advertisementData.advName == deviceName));
|
||||
|
||||
ScanResult? scanResult; //使用局部变量防止出现缓存
|
||||
if (knownDeviceIndex >= 0) {
|
||||
@ -560,7 +541,6 @@ class BlueManage {
|
||||
|
||||
bluetoothConnectDevice = devicesList[knownDeviceIndex].device;
|
||||
scanResult = devicesList[knownDeviceIndex];
|
||||
// AppLog.log('bluetoothConnectDevice: $bluetoothConnectDevice scanResult:$scanResult');
|
||||
|
||||
_initGetMtuSubscription();
|
||||
_initListenConnectionState();
|
||||
@ -572,87 +552,13 @@ class BlueManage {
|
||||
upload: false);
|
||||
return;
|
||||
}
|
||||
AppLog.log('调用了停止扫描的方法');
|
||||
await stopScan();
|
||||
if (scanResult.advertisementData.serviceUuids[0].toString().length >= 5 &&
|
||||
(scanResult.advertisementData.serviceUuids[0].toString()[5] == '0') &&
|
||||
isAddEquipment == false) {
|
||||
// 添加这个判断是因为有些苹果设备或者安卓等性能比较好的设备时,添加完锁之后,锁板未改变为已添加状态之前,就进行了蓝牙连接,导致添加完锁就失败,这里进行了判断,如果第一次连接失败,就清除缓存重新扫描连接
|
||||
if (isReconnect == true) {
|
||||
AppLog.log('该锁已被重置, 重新发送扫描命令');
|
||||
|
||||
BuglyTool.uploadException(
|
||||
message: '该锁已被重置, 重新发送扫描命令startScanSingle 上传记录当前方法是:_connectDevice',
|
||||
detail:
|
||||
'添加这个判断是因为有些苹果设备或者安卓等性能比较好的设备时,添加完锁之后,锁板未改变为已添加状态之前,就进行了蓝牙连接,导致添加完锁就失败,这里进行了判断,如果第一次连接失败,就清除缓存重新扫描连接 该锁已被重置, 重新发送扫描命令 serviceUuids:${scanResult.advertisementData.serviceUuids[0].toString()}',
|
||||
upload: false);
|
||||
|
||||
scanDevices.clear();
|
||||
startScanSingle(deviceName, 15, (List<ScanResult> scanDevices) {
|
||||
_connectDevice(scanDevices, deviceName, connectStateCallBack,
|
||||
isAddEquipment: isAddEquipment, isReconnect: false);
|
||||
});
|
||||
} else {
|
||||
connectStateCallBack(BluetoothConnectionState.disconnected);
|
||||
if (!F.isSKY) {
|
||||
EasyLoading.showToast('该锁已被重置'.tr, duration: 2000.milliseconds);
|
||||
}
|
||||
|
||||
scanDevices.clear();
|
||||
|
||||
BuglyTool.uploadException(
|
||||
message: '提示该锁已被重置, 回调断开连接, 清除缓存,上传记录当前方法是:_connectDevice',
|
||||
detail: 'isReconnect:$isReconnect serviceUuids:${scanResult.advertisementData.serviceUuids[0].toString()}',
|
||||
upload: false);
|
||||
}
|
||||
return;
|
||||
}
|
||||
if (scanResult.advertisementData.serviceUuids[0].toString().length >= 30 &&
|
||||
(scanResult.advertisementData.serviceUuids[0].toString()[31] == '0') &&
|
||||
isAddEquipment == false) {
|
||||
// 添加这个判断是因为有些苹果设备或者安卓等性能比较好的设备时,添加完锁之后,锁板未改变为已添加状态之前,就进行了蓝牙连接,导致添加完锁就失败,这里进行了判断,如果第一次连接失败,就清除缓存重新扫描连接
|
||||
if (isReconnect == true) {
|
||||
AppLog.log('该锁已被重置, 重新发送扫描命令');
|
||||
|
||||
BuglyTool.uploadException(
|
||||
message: '该锁已被重置, 重新发送扫描命令startScanSingle 上传记录当前方法是:_connectDevice',
|
||||
detail:
|
||||
'添加这个判断是因为有些苹果设备或者安卓等性能比较好的设备时,添加完锁之后,锁板未改变为已添加状态之前,就进行了蓝牙连接,导致添加完锁就失败,这里进行了判断,如果第一次连接失败,就清除缓存重新扫描连接 该锁已被重置, 重新发送扫描命令 serviceUuids:${scanResult.advertisementData.serviceUuids[0].toString()}',
|
||||
upload: false);
|
||||
|
||||
scanDevices.clear();
|
||||
startScanSingle(deviceName, 15, (List<ScanResult> scanDevices) {
|
||||
_connectDevice(scanDevices, deviceName, connectStateCallBack,
|
||||
isAddEquipment: isAddEquipment, isReconnect: true);
|
||||
});
|
||||
} else {
|
||||
connectStateCallBack(BluetoothConnectionState.disconnected);
|
||||
if (!F.isSKY) {
|
||||
EasyLoading.showToast('该锁已被重置'.tr, duration: 2000.milliseconds);
|
||||
}
|
||||
scanDevices.clear();
|
||||
|
||||
BuglyTool.uploadException(
|
||||
message: '提示该锁已被重置, 回调断开连接, 清除缓存,上传记录当前方法是:_connectDevice',
|
||||
detail: 'isReconnect:$isReconnect serviceUuids:${scanResult.advertisementData.serviceUuids[0].toString()}',
|
||||
upload: false);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
BuglyTool.uploadException(
|
||||
message: '从devicesList里面查到了设备 下一步连接设备 上传记录当前方法是:_connectDevice',
|
||||
detail:
|
||||
'devicesList:$devicesList scanResult:${scanResult.toString()} bluetoothConnectDevice:${bluetoothConnectDevice.toString()} connectDeviceMacAddress:$connectDeviceMacAddress',
|
||||
upload: false);
|
||||
|
||||
//连接设备
|
||||
await bluetoothDeviceConnect(bluetoothConnectDevice!, connectStateCallBack);
|
||||
}
|
||||
|
||||
//直接给蓝牙设备写入
|
||||
Future<void> doNotSearchBLE(String masAdds, ConnectStateCallBack connectStateCallBack,
|
||||
{bool isAddEquipment = false}) async {
|
||||
Future<void> doNotSearchBLE(String masAdds, ConnectStateCallBack connectStateCallBack, {bool isAddEquipment = false}) async {
|
||||
await FlutterBluePlus.stopScan();
|
||||
|
||||
if (bluetoothConnectDevice == null || bluetoothConnectDevice?.remoteId.str != masAdds) {
|
||||
@ -660,9 +566,7 @@ class BlueManage {
|
||||
_initGetMtuSubscription();
|
||||
_initListenConnectionState();
|
||||
BuglyTool.uploadException(
|
||||
message: '直接给蓝牙设备写入 上传记录当前方法是:doNotSearchBLE',
|
||||
detail: '直接给蓝牙设备写入 通过fromId方法创建一个BluetoothDevice masAdds:$masAdds',
|
||||
upload: false);
|
||||
message: '直接给蓝牙设备写入 上传记录当前方法是:doNotSearchBLE', detail: '直接给蓝牙设备写入 通过fromId方法创建一个BluetoothDevice masAdds:$masAdds', upload: false);
|
||||
} else {
|
||||
BuglyTool.uploadException(
|
||||
message: '直接给蓝牙设备写入 上传记录当前方法是:doNotSearchBLE',
|
||||
@ -747,9 +651,7 @@ class BlueManage {
|
||||
connectStateCallBack(bluetoothConnectionState!);
|
||||
AppLog.log('发现设备时失败 e:$e bluetoothConnectionState:$bluetoothConnectionState');
|
||||
BuglyTool.uploadException(
|
||||
message: '发现服务时失败',
|
||||
detail: '发现服务时报错原因e:$e bluetoothDeviceConnect:${bluetoothConnectDevice.toString()}',
|
||||
upload: false);
|
||||
message: '发现服务时失败', detail: '发现服务时报错原因e:$e bluetoothDeviceConnect:${bluetoothConnectDevice.toString()}', upload: false);
|
||||
rethrow;
|
||||
}
|
||||
}
|
||||
@ -839,7 +741,6 @@ class BlueManage {
|
||||
AppLog.log('发送数据时当前的mtuSize是:${_mtuSize}');
|
||||
final List subData = splitList(valueList, _mtuSize!);
|
||||
|
||||
|
||||
for (int i = 0; i < subData.length; i++) {
|
||||
// 对每个数据包都应用重试逻辑
|
||||
bool packetSent = false;
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user