feat:搜索模式,判断设备被重置的时候重置设备缓存列表

This commit is contained in:
anfe 2024-06-06 11:28:02 +08:00
parent 023067cdc0
commit 7f0087d153

View File

@ -325,7 +325,8 @@ class BlueManage {
mac != null) { mac != null) {
scanSingleTimer?.cancel(); scanSingleTimer?.cancel();
//android //android
await doNotSearchBLE(mac, connectStateCallBack); await doNotSearchBLE(mac, connectStateCallBack,
isAddEquipment: isAddEquipment);
scanSingleTimer = Timer(3.seconds, () { scanSingleTimer = Timer(3.seconds, () {
scanSingleTimer?.cancel(); scanSingleTimer?.cancel();
startScanSingle(deviceName, 15, (List<ScanResult> scanDevices) => null); startScanSingle(deviceName, 15, (List<ScanResult> scanDevices) => null);
@ -384,6 +385,7 @@ class BlueManage {
isAddEquipment == false) { isAddEquipment == false) {
connectStateCallBack(BluetoothConnectionState.disconnected); connectStateCallBack(BluetoothConnectionState.disconnected);
EasyLoading.showToast('该锁已被重置'.tr, duration: 2000.milliseconds); EasyLoading.showToast('该锁已被重置'.tr, duration: 2000.milliseconds);
scanDevices.clear();
return; return;
} }
// //
@ -392,7 +394,8 @@ class BlueManage {
// //
Future<void> doNotSearchBLE( Future<void> doNotSearchBLE(
String masAdds, ConnectStateCallBack connectStateCallBack) async { String masAdds, ConnectStateCallBack connectStateCallBack,
{bool isAddEquipment = false}) async {
await FlutterBluePlus.stopScan(); await FlutterBluePlus.stopScan();
if (bluetoothConnectDevice == null || if (bluetoothConnectDevice == null ||
bluetoothConnectDevice?.remoteId.str != masAdds) { bluetoothConnectDevice?.remoteId.str != masAdds) {
@ -401,12 +404,14 @@ class BlueManage {
_initListenConnectionState(); _initListenConnectionState();
} }
// //
await bluetoothDeviceConnect(bluetoothConnectDevice!, connectStateCallBack); await bluetoothDeviceConnect(bluetoothConnectDevice!, connectStateCallBack,
isAddEquipment: isAddEquipment);
} }
// //
Future<void> bluetoothDeviceConnect(BluetoothDevice bluetoothConnectDevice, Future<void> bluetoothDeviceConnect(BluetoothDevice bluetoothConnectDevice,
ConnectStateCallBack connectStateCallBack) async { ConnectStateCallBack connectStateCallBack,
{bool isAddEquipment = false}) async {
// //
const int maxAttempts = 3; const int maxAttempts = 3;
int attempt = 0; int attempt = 0;
@ -430,23 +435,22 @@ class BlueManage {
if (bluetoothConnectionState == BluetoothConnectionState.connected) { if (bluetoothConnectionState == BluetoothConnectionState.connected) {
try { try {
bluetoothConnectDevice! final List<BluetoothService> services =
.discoverServices() await bluetoothConnectDevice.discoverServices();
.then((List<BluetoothService> services) { //
for (final BluetoothService service in services) { for (final BluetoothService service in services) {
if (service.uuid == _serviceIdConnect) { if (service.uuid == _serviceIdConnect) {
for (final BluetoothCharacteristic characteristic for (final BluetoothCharacteristic characteristic
in service.characteristics) { in service.characteristics) {
if (characteristic.characteristicUuid == if (characteristic.characteristicUuid ==
_characteristicIdSubscription) { _characteristicIdSubscription) {
_subScribeToCharacteristic(characteristic); _subScribeToCharacteristic(characteristic);
bluetoothConnectionState = BluetoothConnectionState.connected; bluetoothConnectionState = BluetoothConnectionState.connected;
connectStateCallBack(bluetoothConnectionState!); connectStateCallBack(bluetoothConnectionState!);
}
} }
} }
} }
}); }
} on Exception catch (e) { } on Exception catch (e) {
bluetoothConnectionState = BluetoothConnectionState.disconnected; bluetoothConnectionState = BluetoothConnectionState.disconnected;
connectStateCallBack(bluetoothConnectionState!); connectStateCallBack(bluetoothConnectionState!);