From 6c897b8f3b905c6261c41054ab2554d6c8ead363 Mon Sep 17 00:00:00 2001 From: anfe <448468458@qq.com> Date: Mon, 27 May 2024 17:38:23 +0800 Subject: [PATCH] =?UTF-8?q?feat:=E4=BC=98=E5=8C=96=20android=20=E9=A6=96?= =?UTF-8?q?=E6=AC=A1=E8=93=9D=E7=89=99=E7=AD=96=E7=95=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .DS_Store | Bin 6148 -> 6148 bytes lib/blue/blue_manage.dart | 62 +++++++++- .../lockDetail/lockDetail_logic.dart | 27 ++++- .../lockMian/entity/lockListInfo_entity.dart | 106 ++++++++++-------- pubspec.yaml | 2 +- 5 files changed, 144 insertions(+), 53 deletions(-) diff --git a/.DS_Store b/.DS_Store index a15ea27935c7877c7f6966c4a2945ca5e15353ec..612ce635fdfd20d43358e504235ffb6222d6459e 100755 GIT binary patch delta 25 gcmZoMXffDO$IfA5W~ifJY-&0=gUx>PQFeO)0AP~`H~;_u delta 24 fcmZoMXffDO$IfnSX{MuKY%w{V&0+IVc6$K;TKWeJ diff --git a/lib/blue/blue_manage.dart b/lib/blue/blue_manage.dart index dba9581c..0ed3e921 100755 --- a/lib/blue/blue_manage.dart +++ b/lib/blue/blue_manage.dart @@ -4,6 +4,7 @@ import 'dart:io'; import 'package:flutter_easyloading/flutter_easyloading.dart'; import 'package:get/get.dart'; import 'package:star_lock/app_settings/app_settings.dart'; +import 'package:star_lock/tools/commonDataManage.dart'; import 'io_tool/io_model.dart'; import 'io_tool/io_tool.dart'; @@ -17,12 +18,12 @@ typedef ConnectStateCallBack = Function( typedef ScanDevicesCallBack = Function(List); class BlueManage { - factory BlueManage() => shareManager()!; BlueManage._init() { _initBlue(); } + final List scanDevices = []; // 用来写入的服务id @@ -311,10 +312,21 @@ class BlueManage { {bool isAddEquipment = false}) async { connectDeviceName = deviceName; final List devicesList = scanDevices; - final bool isExistDevice = isExistScanDevices(connectDeviceName); + final bool isCurrentDevice = + CommonDataManage().currentKeyInfo.lockName == deviceName; + final String? mac = CommonDataManage().currentKeyInfo.mac; - if (isAddEquipment == false && isExistDevice == false) { + AppLog.log('开始连接 是否存在缓存:$isExistDevice 是否是当前设备:$isCurrentDevice mac:$mac'); + if (GetPlatform.isAndroid && + !isExistDevice && + isCurrentDevice && + mac != null) { + //兼容android 的低配手机 + await doNotSearchBLE(mac, connectStateCallBack); + await Future.delayed(3.seconds); + startScanSingle(deviceName, 15, (List scanDevices) => null); + } else if (isAddEquipment == false && isExistDevice == false) { //取消缓存直接使用,存在配对场景设备信息会更变 startScanSingle(deviceName, 15, (List scanDevices) { _connectDevice(scanDevices, deviceName, connectStateCallBack, @@ -421,7 +433,51 @@ class BlueManage { } } + //直接给蓝牙设备写入 + Future doNotSearchBLE( + String masAdds, ConnectStateCallBack connectStateCallBack) async { + // FlutterBluePlus.setLogLevel(LogLevel.debug); + final BluetoothDevice device = BluetoothDevice.fromId(masAdds); + // 重连三次 + const int maxAttempts = 3; + bool connected = false; + int attempt = 0; + while (attempt < maxAttempts && !connected) { + try { + await device.connect(); + connected = true; + } catch (e, s) { + AppLog.log('连接失败 重连了: ${e.toString()} ${s.toString()}'); + attempt++; // Increase the attempt count + if (attempt < maxAttempts) { + AppLog.log('重新尝试连接...'); + } + } + } + if (!connected) { + connectStateCallBack(BluetoothConnectionState.disconnected); + return; + } + + bluetoothConnectDevice = device; + bluetoothConnectionState = BluetoothConnectionState.connected; + final List services = + await bluetoothConnectDevice!.discoverServices(); + for (final BluetoothService service in services) { + if (service.uuid == _serviceIdConnect) { + for (final BluetoothCharacteristic characteristic + in service.characteristics) { + if (characteristic.characteristicUuid == + _characteristicIdSubscription) { + _subScribeToCharacteristic(characteristic); + } + } + } + } + connectStateCallBack(BluetoothConnectionState.connected); + // writeCharacteristicWithResponse(value); + } Future _subScribeToCharacteristic( BluetoothCharacteristic characteristic) async { diff --git a/lib/main/lockDetail/lockDetail/lockDetail_logic.dart b/lib/main/lockDetail/lockDetail/lockDetail_logic.dart index c079f792..19df6d3b 100755 --- a/lib/main/lockDetail/lockDetail/lockDetail_logic.dart +++ b/lib/main/lockDetail/lockDetail/lockDetail_logic.dart @@ -295,6 +295,32 @@ class LockDetailLogic extends BaseGetXController { resetOpenDoorState(); } }); + + // BlueManage() + // .blueStart((BluetoothConnectionState deviceConnectionState) async { + // if (deviceConnectionState == BluetoothConnectionState.connected) { + // BlueManage().writeDirectlyBLE( + // 'DC:8F:92:83:69:ED', + // OpenLockCommand( + // lockID: BlueManage().connectDeviceName, + // userID: await Storage.getUid(), + // openMode: state.openDoorModel, + // openTime: getUTCNetTime(), + // onlineToken: state.lockNetToken, + // token: getTokenList, + // needAuthor: 1, + // signKey: signKeyDataList, + // privateKey: getPrivateKeyList, + // ).packageData()); + // } else if (deviceConnectionState == + // BluetoothConnectionState.disconnected) { + // cancelBlueConnetctToastTimer(); + // if (state.ifCurrentScreen.value == true) { + // showBlueConnetctToast(); + // } + // resetOpenDoorState(); + // } + // }); } //蓝牙关闭 @@ -513,7 +539,6 @@ class LockDetailLogic extends BaseGetXController { Future onReady() async { super.onReady(); getServerDatetime(); - await PermissionDialog.request(Permission.location); await PermissionDialog.requestBluetooth(); } diff --git a/lib/main/lockMian/entity/lockListInfo_entity.dart b/lib/main/lockMian/entity/lockListInfo_entity.dart index 4e484fc3..c3a63756 100755 --- a/lib/main/lockMian/entity/lockListInfo_entity.dart +++ b/lib/main/lockMian/entity/lockListInfo_entity.dart @@ -70,7 +70,9 @@ class GroupList { List? lockList; bool _isChecked = false; + bool get isChecked => _isChecked ?? false; + set isChecked(bool value) => _isChecked = value; GroupList({this.groupName, this.groupId, this.lockList}); @@ -131,41 +133,44 @@ class LockListInfoItemEntity { LockFeature? lockFeature; LockSetting? lockSetting; int? hasGateway; + String? mac; - LockListInfoItemEntity( - {this.keyId, - this.lockId, - this.lockName, - this.lockAlias, - this.electricQuantity, - this.fwVersion, - this.hwVersion, - this.keyType, - this.passageMode, - this.userType, - this.startDate, - this.endDate, - this.weekDays, - this.remoteEnable, - this.faceAuthentication, - this.lastFaceValidateTime, - this.nextFaceValidateTime, - this.keyRight, - this.keyStatus, - this.isLockOwner, - this.bluetooth, - this.lockFeature, - this.lockSetting, - this.sendDate, - this.lockUserNo, - this.electricQuantityDate, - this.electricQuantityStandby, - this.senderUserId, - this.isOnlyManageSelf, - this.restoreCount, - this.model, - this.vendor, - this.hasGateway}); + LockListInfoItemEntity({ + this.keyId, + this.lockId, + this.lockName, + this.lockAlias, + this.electricQuantity, + this.fwVersion, + this.hwVersion, + this.keyType, + this.passageMode, + this.userType, + this.startDate, + this.endDate, + this.weekDays, + this.remoteEnable, + this.faceAuthentication, + this.lastFaceValidateTime, + this.nextFaceValidateTime, + this.keyRight, + this.keyStatus, + this.isLockOwner, + this.bluetooth, + this.lockFeature, + this.lockSetting, + this.sendDate, + this.lockUserNo, + this.electricQuantityDate, + this.electricQuantityStandby, + this.senderUserId, + this.isOnlyManageSelf, + this.restoreCount, + this.model, + this.vendor, + this.hasGateway, + this.mac, + }); LockListInfoItemEntity.fromJson(Map json) { keyId = json['keyId']; @@ -207,6 +212,7 @@ class LockListInfoItemEntity { ? LockSetting.fromJson(json['lockSetting']) : null; hasGateway = json['hasGateway']; + mac = json['mac']; } Map toJson() { @@ -250,12 +256,13 @@ class LockListInfoItemEntity { data['lockSetting'] = lockSetting!.toJson(); } data['hasGateway'] = hasGateway; + data['mac'] = mac; return data; } //是否是锁拥有者 也代表是超级管理员 - bool isLockOwnerBool(){ - return isLockOwner== 1; + bool isLockOwnerBool() { + return isLockOwner == 1; } } @@ -304,18 +311,20 @@ class LockFeature { int? videoIntercom; int? isSupportCatEye; int? isSupportBackupBattery; - LockFeature( - {this.password, - this.icCard, - this.fingerprint, - this.fingerVein, - this.palmVein, - this.isSupportIris, - this.d3Face, - this.bluetoothRemoteControl, - this.videoIntercom, - this.isSupportCatEye, - this.isSupportBackupBattery}); + + LockFeature({ + this.password, + this.icCard, + this.fingerprint, + this.fingerVein, + this.palmVein, + this.isSupportIris, + this.d3Face, + this.bluetoothRemoteControl, + this.videoIntercom, + this.isSupportCatEye, + this.isSupportBackupBattery, + }); LockFeature.fromJson(Map json) { password = json['password']; @@ -352,6 +361,7 @@ class LockSetting { int? attendance; int? appUnlockOnline; int? remoteUnlock; + LockSetting({ this.attendance, this.appUnlockOnline, diff --git a/pubspec.yaml b/pubspec.yaml index 52df5e8d..0812c8a2 100755 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -127,7 +127,7 @@ dependencies: url_launcher: ^6.1.10 #蓝牙 # flutter_reactive_ble: ^5.1.1 - flutter_blue_plus: 1.31.16 + flutter_blue_plus: 1.32.7 # event_bus: ^2.0.0 #菊花