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