feat:优化 android 首次蓝牙策略

This commit is contained in:
anfe 2024-05-27 17:38:23 +08:00
parent bc08c31a64
commit 6c897b8f3b
5 changed files with 144 additions and 53 deletions

BIN
.DS_Store vendored

Binary file not shown.

View File

@ -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<ScanResult>);
class BlueManage {
factory BlueManage() => shareManager()!;
BlueManage._init() {
_initBlue();
}
final List<ScanResult> scanDevices = <ScanResult>[];
// id
@ -311,10 +312,21 @@ class BlueManage {
{bool isAddEquipment = false}) async {
connectDeviceName = deviceName;
final List<ScanResult> 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<dynamic>.delayed(3.seconds);
startScanSingle(deviceName, 15, (List<ScanResult> scanDevices) => null);
} else if (isAddEquipment == false && isExistDevice == false) {
//使
startScanSingle(deviceName, 15, (List<ScanResult> scanDevices) {
_connectDevice(scanDevices, deviceName, connectStateCallBack,
@ -421,7 +433,51 @@ class BlueManage {
}
}
//
Future<void> 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<BluetoothService> 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<void> _subScribeToCharacteristic(
BluetoothCharacteristic characteristic) async {

View File

@ -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<void> onReady() async {
super.onReady();
getServerDatetime();
await PermissionDialog.request(Permission.location);
await PermissionDialog.requestBluetooth();
}

View File

@ -70,7 +70,9 @@ class GroupList {
List<LockListInfoItemEntity>? 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<String, dynamic> json) {
keyId = json['keyId'];
@ -207,6 +212,7 @@ class LockListInfoItemEntity {
? LockSetting.fromJson(json['lockSetting'])
: null;
hasGateway = json['hasGateway'];
mac = json['mac'];
}
Map<String, dynamic> 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<String, dynamic> json) {
password = json['password'];
@ -352,6 +361,7 @@ class LockSetting {
int? attendance;
int? appUnlockOnline;
int? remoteUnlock;
LockSetting({
this.attendance,
this.appUnlockOnline,

View File

@ -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
#菊花