diff --git a/lib/blue/blue_manage.dart b/lib/blue/blue_manage.dart index 9556c1b1..984e2031 100755 --- a/lib/blue/blue_manage.dart +++ b/lib/blue/blue_manage.dart @@ -141,14 +141,16 @@ class BlueManage { } /// 开始指定设备名称的扫描蓝牙设备 - Future startScanSingle(String deviceName, int timeout, - ScanDevicesCallBack scanDevicesCallBack) async { + Future startScanSingle(String deviceName, int timeout, ScanDevicesCallBack scanDevicesCallBack) async { final DateTime start = DateTime.now(); FlutterBluePlus.isSupported.then((bool isAvailable) async { if (isAvailable) { // AppLog.log('startScanSingle 蓝牙状态 系统蓝牙状态:$_adapterState 蓝牙连接状态:$bluetoothConnectionState'); if (_adapterState == BluetoothAdapterState.on) { try { + BuglyTool.uploadException( + message: 'startScanSingle:$deviceName', + detail: 'startScanSingle$deviceName 开始指定设备名称的扫描蓝牙设备'); //android 扫描比较慢,取样只要 3 分之一 final int divisor = Platform.isAndroid ? 3 : 1; FlutterBluePlus.startScan( @@ -166,6 +168,9 @@ class BlueManage { start.millisecondsSinceEpoch; AppLog.log( '扫描到的设备数:${results.length} 是否查找到 $isExit 以查找$milliseconds毫秒'); + BuglyTool.uploadException( + message: 'startScanSingle:$deviceName', + detail: 'startScanSingle$deviceName 监听扫描结果 扫描到的设备数:${results.length} 是否查找到 $isExit 以查找$milliseconds毫秒'); if (isExit) { for (final ScanResult scanResult in results) { if (((scanResult.advertisementData.serviceUuids.isNotEmpty @@ -187,28 +192,33 @@ class BlueManage { } else { scanDevices.add(scanResult); } + BuglyTool.uploadException( + message: 'startScanSingle:$deviceName', + detail: 'startScanSingle$deviceName 查询到的结果scanResult:$scanResult'); } } completer.complete(); } }, onError: (e) { - AppLog.log( - '扫描失败:$e', - ); + BuglyTool.uploadException( + message: 'startScanSingle:$deviceName', + detail: 'startScanSingle$deviceName 监听扫描结果失败 e:${e.toString()}'); + AppLog.log('扫描失败:$e',); }); FlutterBluePlus.cancelWhenScanComplete(subscription); await completer.future; scanDevicesCallBack(scanDevices); subscription.cancel(); } catch (e) { + BuglyTool.uploadException( + message: 'startScanSingle:$deviceName', + detail: 'startScanSingle$deviceName startScanSingle内部逻辑整形失败 e:${e.toString()}'); AppLog.log('扫描失败'); } } else { try { - if (Platform.isAndroid) { - await FlutterBluePlus.turnOn(); - } + openBlue(); } catch (e) { AppLog.log('蓝牙打开失败'); } @@ -268,9 +278,7 @@ class BlueManage { } } else { try { - if (Platform.isAndroid) { - await FlutterBluePlus.turnOn(); - } + openBlue(); } catch (e) { AppLog.log('蓝牙打开失败'); } @@ -291,24 +299,36 @@ class BlueManage { if (_adapterState == BluetoothAdapterState.on) { // 蓝牙已开启,可以进行蓝牙操作 if (bluetoothConnectionState != BluetoothConnectionState.connected) { + BuglyTool.uploadException( + message: 'blueSendData 蓝牙未连接', + detail: 'blueSendData 蓝牙未连接}'); _connect(deviceName, (BluetoothConnectionState state) { stateCallBack(bluetoothConnectionState!); }, isAddEquipment: isAddEquipment); } else { + BuglyTool.uploadException( + message: 'blueSendData 蓝牙是连接状态', + detail: 'blueSendData 蓝牙是连接状态,直接回调}'); stateCallBack(bluetoothConnectionState!); } } else { + BuglyTool.uploadException( + message: 'blueSendData 蓝牙未打开', + detail: 'blueSendData 蓝牙未打开--_adapterState:${BluetoothAdapterState.on}'); try { stateCallBack(BluetoothConnectionState.disconnected); openBlue(); } catch (e) { AppLog.log('蓝牙打开失败'); + BuglyTool.uploadException( + message: 'blueSendData 蓝牙打开失败', + detail: 'blueSendData 蓝牙打开失败--_adapterState:${BluetoothAdapterState.on}'); } } } else { BuglyTool.uploadException( message: 'blueSendData isAvailable状态', - detail: 'blueSendData isAvailable不可用--json'); + detail: 'blueSendData isAvailable不可用--isAvailable:$isAvailable'); // FlutterBuglyPlugin.reportException(exceptionName: 'blueSendData isAvailable状态', reason: 'blueSendData isAvailable不可用'); AppLog.log('开始扫描 蓝牙不可用,不能进行蓝牙操作'); } @@ -320,33 +340,47 @@ class BlueManage { String deviceName, ConnectStateCallBack connectStateCallBack, {bool isAddEquipment = false}) async { connectDeviceName = deviceName; + // 当前已扫描到的缓存设备 final List devicesList = scanDevices; + // 是否有缓存设备 true是有缓存设备 final bool isExistDevice = isExistScanDevices(connectDeviceName); - final bool isCurrentDevice = - CommonDataManage().currentKeyInfo.lockName == deviceName; + // 是否是当前设备 + final bool isCurrentDevice = CommonDataManage().currentKeyInfo.lockName == deviceName; + // mac地址 final String? mac = CommonDataManage().currentKeyInfo.mac; AppLog.log('开始连接 是否存在缓存:$isExistDevice 是否是当前设备:$isCurrentDevice mac:$mac'); - if (GetPlatform.isAndroid && - !isExistDevice && - isCurrentDevice && - mac != null) { + if (GetPlatform.isAndroid && !isExistDevice && isCurrentDevice && mac != null) { + // 当是安卓设备 且不存在缓存设备 且是当前设备 且mac地址不为空 + BuglyTool.uploadException( + message: '_connect:$deviceName', + detail: '_connect:$deviceName GetPlatform.isAndroid:${GetPlatform.isAndroid} isExistDevice:$isExistDevice mac:$mac'); scanSingleTimer?.cancel(); - //兼容android 的低配手机 - await doNotSearchBLE(mac, connectStateCallBack, - isAddEquipment: isAddEquipment); + // 兼容android 的低配手机 + await doNotSearchBLE(mac, connectStateCallBack, isAddEquipment: isAddEquipment); + // scanSingleTimer = Timer(3.seconds, () { scanSingleTimer?.cancel(); + BuglyTool.uploadException( + message: '_connect:$deviceName', + detail: '_connect$deviceName scanSingleTimer 调用 startScanSingle'); startScanSingle(deviceName, 15, (List scanDevices) => null); }); } else if (isAddEquipment == false && isExistDevice == false) { //取消缓存直接使用,存在配对场景设备信息会更变 + BuglyTool.uploadException( + message: '_connect:$deviceName', + detail: '_connect$deviceName isAddEquipment:$isAddEquipment isExistDevice:$isExistDevice 下一步调用startScanSingle'); + // AppLog.log('无存在设备需要扫描 deviceName:$deviceName isAddEquipment:$isAddEquipment'); startScanSingle(deviceName, 15, (List scanDevices) { _connectDevice(scanDevices, deviceName, connectStateCallBack, isAddEquipment: isAddEquipment); }); } else { + BuglyTool.uploadException( + message: '_connect:$deviceName', + detail: '_connect$deviceName 直接调用_connectDevice'); // AppLog.log('安卓或者iOS 存在设备不需要扫描 deviceName:$deviceName isAddEquipment:$isAddEquipment'); _connectDevice(devicesList, deviceName, connectStateCallBack, isAddEquipment: isAddEquipment); @@ -355,21 +389,21 @@ 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; } Future _connectDevice( List devicesList, String deviceName, - ConnectStateCallBack connectStateCallBack, { - bool isAddEquipment = false, // 是否是添加设备之前 - bool isReconnect = true, // 是否是重连 - }) async { + ConnectStateCallBack connectStateCallBack, + { + 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)); @@ -390,16 +424,27 @@ class BlueManage { _initListenConnectionState(); } if (scanResult == null || connectDeviceMacAddress.isEmpty) { + BuglyTool.uploadException( + message: '_connectDevice:$deviceName', + detail: '_connectDevice$deviceName scanResult:$scanResult connectDeviceMacAddress:$connectDeviceMacAddress scanResult == null || connectDeviceMacAddress.isEmpty不往下执行'); return; } AppLog.log('调用了停止扫描的方法'); await stopScan(); + BuglyTool.uploadException( + message: '_connectDevice:$deviceName', + detail: '_connectDevice$deviceName devicesList:$devicesList isReconnect:$isReconnect knownDeviceIndex:$knownDeviceIndex 调用了停止扫描方法'); if ((scanResult.advertisementData.serviceUuids[0].toString()[31] == '0') && isAddEquipment == false) { // 添加这个判断是因为有些苹果设备或者安卓等性能比较好的设备时,添加完锁之后,锁板未改变为已添加状态之前,就进行了蓝牙连接,导致添加完锁就失败,这里进行了判断,如果第一次连接失败,就清除缓存重新扫描连接 if (isReconnect == true) { AppLog.log('该锁已被重置, 重新发送扫描命令'); + + BuglyTool.uploadException( + message: '_connectDevice:$deviceName', + detail: '_connectDevice$deviceName 该锁已被重置, 重新发送扫描命令 serviceUuids:${scanResult.advertisementData.serviceUuids[0].toString()}'); + scanDevices.clear(); startScanSingle(deviceName, 15, (List scanDevices) { _connectDevice(scanDevices, deviceName, connectStateCallBack, @@ -420,12 +465,19 @@ class BlueManage { Future doNotSearchBLE( String masAdds, ConnectStateCallBack connectStateCallBack, {bool isAddEquipment = false}) async { + BuglyTool.uploadException( + message: 'doNotSearchBLE:$masAdds', + detail: 'doNotSearchBLE$masAdds 调用doNotSearchBLE'); await FlutterBluePlus.stopScan(); if (bluetoothConnectDevice == null || bluetoothConnectDevice?.remoteId.str != masAdds) { bluetoothConnectDevice = BluetoothDevice.fromId(masAdds); _initGetMtuSubscription(); _initListenConnectionState(); + }else{ + BuglyTool.uploadException( + message: 'doNotSearchBLE:$masAdds', + detail: 'doNotSearchBLE$masAdds 未调用_initGetMtuSubscription()、_initListenConnectionState()'); } //尝试连接设备 await bluetoothDeviceConnect(bluetoothConnectDevice!, connectStateCallBack, @@ -454,25 +506,36 @@ class BlueManage { if (attempt >= maxAttempts) { AppLog.log('$maxAttempts次后尝试连接失败'); + BuglyTool.uploadException( + message: 'bluetoothDeviceConnect:${bluetoothConnectDevice.toString()}', + detail: 'bluetoothDeviceConnect:${bluetoothConnectDevice.toString()} $maxAttempts次后尝试连接失败'); connectStateCallBack(BluetoothConnectionState.disconnected); } if (bluetoothConnectionState == BluetoothConnectionState.connected) { try { - final List services = - await bluetoothConnectDevice.discoverServices(); + 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) { + for (final BluetoothCharacteristic characteristic in service.characteristics) { + if (characteristic.characteristicUuid == _characteristicIdSubscription) { _subScribeToCharacteristic(characteristic); bluetoothConnectionState = BluetoothConnectionState.connected; connectStateCallBack(bluetoothConnectionState!); + BuglyTool.uploadException( + message: 'bluetoothDeviceConnect:${bluetoothConnectDevice.toString()}', + detail: 'bluetoothDeviceConnect:${bluetoothConnectDevice.toString()} 发现服务,连接成功,订阅数据'); + } else{ + BuglyTool.uploadException( + message: 'bluetoothDeviceConnect:${bluetoothConnectDevice.toString()}', + detail: 'bluetoothDeviceConnect:${bluetoothConnectDevice.toString()} characteristic.characteristicUuid != _characteristicIdSubscription 失败'); } } + }else{ + BuglyTool.uploadException( + message: 'bluetoothDeviceConnect:${bluetoothConnectDevice.toString()}', + detail: 'bluetoothDeviceConnect:${bluetoothConnectDevice.toString()} service.uuid != _serviceIdConnec 失败'); } } } on Exception catch (e) { @@ -480,6 +543,9 @@ class BlueManage { connectStateCallBack(bluetoothConnectionState!); AppLog.log( '发现设备时失败 e:$e bluetoothConnectionState:$bluetoothConnectionState'); + BuglyTool.uploadException( + message: 'bluetoothDeviceConnect:${bluetoothConnectDevice.toString()}', + detail: 'bluetoothDeviceConnect:${bluetoothConnectDevice.toString()} 发现服务时报错 e:$e'); rethrow; } } diff --git a/lib/blue/io_protocol/io_addCardCancel.dart b/lib/blue/io_protocol/io_addCardCancel.dart index 734bc65f..260dd96f 100755 --- a/lib/blue/io_protocol/io_addCardCancel.dart +++ b/lib/blue/io_protocol/io_addCardCancel.dart @@ -81,7 +81,7 @@ class SenderCancelAddCardCommand extends SenderProtocol { data.addAll(subData); if ((data.length % 16) != 0) { - int add = (16 - data.length % 16); + int add = 16 - data.length % 16; for (int i = 0; i < add; i++) { data.add(0); } diff --git a/lib/main/lockDetail/doorLockLog/doorLockLog_logic.dart b/lib/main/lockDetail/doorLockLog/doorLockLog_logic.dart index a5780ab3..a04750a8 100755 --- a/lib/main/lockDetail/doorLockLog/doorLockLog_logic.dart +++ b/lib/main/lockDetail/doorLockLog/doorLockLog_logic.dart @@ -206,8 +206,7 @@ class DoorLockLogLogic extends BaseGetXController { if (entity.errorCode!.codeIsSuccessful) { state.operateDate = entity.data!.operateDate! ~/ 1000; state.currentDate = entity.data!.currentDate! ~/ 1000; - AppLog.log( - 'entity.data!.currentDate!:${entity.data!.currentDate!} currentDate:${state.currentDate}'); + AppLog.log('entity.data!.currentDate!:${entity.data!.currentDate!} currentDate:${state.currentDate}'); senderReferEventRecordTime(); } } diff --git a/lib/main/lockDetail/lockDetail/lockDetail_logic.dart b/lib/main/lockDetail/lockDetail/lockDetail_logic.dart index 6acf77b8..0e277057 100755 --- a/lib/main/lockDetail/lockDetail/lockDetail_logic.dart +++ b/lib/main/lockDetail/lockDetail/lockDetail_logic.dart @@ -61,6 +61,8 @@ class LockDetailLogic extends BaseGetXController { Future _replyOpenLock(Reply reply) async { final int status = reply.data[6]; + BuglyTool.uploadException(message: '_replyOpenLock', detail: '_replyOpenLock:${reply.data}'); + switch (status) { case 0x00: //成功 @@ -330,14 +332,6 @@ class LockDetailLogic extends BaseGetXController { // 点击开门事件 Future openDoorAction() async { - showBlueConnetctToastTimer( - outTimer: 20, - action: () { - // FlutterBuglyPlugin.reportException(exceptionName: '开门超时处理', reason: '开门超时,断开连接'); - BuglyTool.uploadException(message: '开门超时处理', detail: '开门超时,断开连接--json'); - resetOpenDoorState(); - blueManageDisconnect(); - }); final List? privateKey = await Storage.getStringList(saveBluePrivateKey); final List getPrivateKeyList = changeStringListToIntList(privateKey!); @@ -347,11 +341,31 @@ class LockDetailLogic extends BaseGetXController { final List? token = await Storage.getStringList(saveBlueToken); final List getTokenList = changeStringListToIntList(token!); - BlueManage() - .blueSendData(state.keyInfos.value.bluetooth!.bluetoothDeviceName!, - (BluetoothConnectionState deviceConnectionState) async { + final String command = OpenLockCommand( + lockID: BlueManage().connectDeviceName, + userID: await Storage.getUid(), + openMode: state.openDoorModel, + openTime: getUTCNetTime(), + onlineToken: state.lockNetToken, + token: getTokenList, + needAuthor: 1, + signKey: signKeyDataList, + privateKey: getPrivateKeyList, + ).toString(); + AppLog.log('command:${command}'); + + showBlueConnetctToastTimer( + outTimer: 20, + action: () { + // FlutterBuglyPlugin.reportException(exceptionName: '开门超时处理', reason: '开门超时,断开连接'); + BuglyTool.uploadException(message: '开门超时处理', detail: '开门超时,断开连接--$command'); + resetOpenDoorState(); + blueManageDisconnect(); + }); + + BlueManage().blueSendData(state.keyInfos.value.bluetooth!.bluetoothDeviceName!, (BluetoothConnectionState deviceConnectionState) async { if (deviceConnectionState == BluetoothConnectionState.connected) { - BuglyTool.uploadException(message: '蓝牙连接成功', detail: '蓝牙连接成功,发送开锁指令--json',data: {'key1':'value1','key2':'value2'}); + BuglyTool.uploadException(message: '蓝牙连接成功', detail: '蓝牙连接成功,发送开锁指令--$command'); // FlutterBuglyPlugin.reportException(exceptionName: '蓝牙连接成功', reason: '蓝牙连接成功,发送开锁指令'); IoSenderManage.senderOpenLock( lockID: BlueManage().connectDeviceName, @@ -364,13 +378,12 @@ class LockDetailLogic extends BaseGetXController { signKey: signKeyDataList, privateKey: getPrivateKeyList, ); - } else if (deviceConnectionState == - BluetoothConnectionState.disconnected) { + } else if (deviceConnectionState == BluetoothConnectionState.disconnected) { cancelBlueConnetctToastTimer(); + BuglyTool.uploadException(message: '蓝牙连接失败', detail: '蓝牙连接失败,断开连接--$command'); if (state.ifCurrentScreen.value == true) { showBlueConnetctToast(); } - resetOpenDoorState(); } }); @@ -453,6 +466,9 @@ class LockDetailLogic extends BaseGetXController { // AppLog.log('从服务器获取联网token:${state.lockNetToken}'); openDoorAction(); } else { + BuglyTool.uploadException( + message: '点击了需要联网开锁 2', + detail: '点击了需要联网开锁 获取连网token失败'); showToast('网络访问失败,请检查网络是否正常'.tr, something: () { resetOpenDoorState(); cancelBlueConnetctToastTimer(); diff --git a/lib/main/lockDetail/lockDetail/lockDetail_page.dart b/lib/main/lockDetail/lockDetail/lockDetail_page.dart index f0b22083..e8c6277f 100755 --- a/lib/main/lockDetail/lockDetail/lockDetail_page.dart +++ b/lib/main/lockDetail/lockDetail/lockDetail_page.dart @@ -22,6 +22,7 @@ import '../../../common/XSConstantMacro/XSConstantMacro.dart'; import '../../../tools/appRouteObserver.dart'; import '../../../tools/dateTool.dart'; import '../../../tools/eventBusEventManage.dart'; +import '../../../tools/storage.dart'; import '../../../translations/trans_lib.dart'; import '../../lockMian/entity/lockListInfo_entity.dart'; import 'lockDetail_logic.dart'; @@ -1432,14 +1433,14 @@ class _LockDetailPageState extends State state.openLockBtnState.value = 1; state.animationController!.forward(); AppLog.log('点击开锁'); + BuglyTool.time = DateTime.now().millisecondsSinceEpoch; if (isOpenLockNeedOnline) { // 不需要联网 state.openDoorModel = 0; // FlutterBuglyPlugin.reportException(exceptionName: '点击了不需要联网开锁'); BuglyTool.uploadException( message: '点击了不需要联网开锁', - detail: '点击了不需要联网开锁--------json', - data: {'key1': 'value1', 'key2': 'value2'}); + detail: '点击了不需要联网开锁 openDoorModel:${state.openDoorModel}'); AppLog.log('点击开锁 state.openDoorModel = 0 不需要联网'); logic.openDoorAction(); } else { @@ -1448,8 +1449,7 @@ class _LockDetailPageState extends State // FlutterBuglyPlugin.reportException(exceptionName: '点击了需要联网开锁'); BuglyTool.uploadException( message: '点击了需要联网开锁', - detail: '点击了需要联网开锁---json', - data: {'key1': 'value1', 'key2': 'value2'}); + detail: '点击了需要联网开锁 openDoorModel:${state.openDoorModel}'); AppLog.log('点击开锁 state.openDoorModel = 2 需要联网'); logic.getLockNetToken(); } diff --git a/lib/tools/bugly/bugly_tool.dart b/lib/tools/bugly/bugly_tool.dart index 002271dd..0a9445e5 100644 --- a/lib/tools/bugly/bugly_tool.dart +++ b/lib/tools/bugly/bugly_tool.dart @@ -13,6 +13,9 @@ import 'package:star_lock/tools/storage.dart'; /// /// class BuglyTool { + + static int time = 0; + static Future init() async { if (F.isProductionEnv) { //生产 @@ -57,11 +60,11 @@ class BuglyTool { FlutterBugly.setUserId(userId?.toString() ?? ''); } - static void uploadException({ + static Future uploadException({ required String message, required String detail, Map? data, - }) { - FlutterBugly.uploadException(message: message, detail: detail, data: data); + }) async { + FlutterBugly.uploadException(message: '${(await Storage.getMobile())!.isNotEmpty ? (await Storage.getMobile()) : (await Storage.getEmail())}+$time ---' + message, detail: detail, data: data); } } diff --git a/pubspec.yaml b/pubspec.yaml index 52c4a7cb..86ff92ca 100755 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -92,8 +92,9 @@ publish_to: 'none' # Remove this line if you wish to publish to pub.dev # 1.0.78+2024082701:测试国际化功能 # 1.0.79+2024083001:测试所有国家国际化功能 # 1.0.79+2024083001:测试开完锁数据上传功能 +# 1.0.81+2024092901:添加开锁数据收集功能 -version: 1.0.79+2024090201 +version: 1.0.81+2024090201 environment: sdk: '>=2.12.0 <3.0.0'