diff --git a/.DS_Store b/.DS_Store index fce4fcc3..0d0d83d9 100755 Binary files a/.DS_Store and b/.DS_Store differ diff --git a/lib/blue/blue_manage.dart b/lib/blue/blue_manage.dart index 73a9bdab..8b13c78b 100755 --- a/lib/blue/blue_manage.dart +++ b/lib/blue/blue_manage.dart @@ -179,18 +179,27 @@ class BlueManage { /// 开始指定设备名称的扫描蓝牙设备 Future startScanSingle(String deviceName, int timeout, ScanDevicesCallBack scanDevicesCallBack) async { + final DateTime start = DateTime.now(); FlutterBluePlus.isSupported.then((bool isAvailable) async { if (isAvailable) { if (_adapterState == BluetoothAdapterState.on) { try { - FlutterBluePlus.startScan(timeout: Duration(seconds: timeout)); + //android 扫描比较慢,取样只要 8 分之一 + final int divisor = Platform.isAndroid ? 8 : 1; + FlutterBluePlus.startScan( + continuousDivisor: divisor, + continuousUpdates: true, + timeout: Duration(seconds: timeout)); final Completer completer = Completer(); final StreamSubscription> subscription = FlutterBluePlus.scanResults.listen((List results) { final bool isExit = results.any((ScanResult element) => (element.device.platformName == deviceName) || (element.advertisementData.advName == deviceName)); - AppLog.log('扫描到的设备数:${results.length} 是否查找到 $isExit'); + final int milliseconds = DateTime.now().millisecondsSinceEpoch - + start.millisecondsSinceEpoch; + AppLog.log( + '扫描到的设备数:${results.length} 是否查找到 $isExit 以查找$milliseconds毫秒'); if (isExit) { for (final ScanResult scanResult in results) { if (((scanResult.advertisementData.serviceUuids.isNotEmpty @@ -214,6 +223,7 @@ class BlueManage { } } } + completer.complete(); } }, onError: (e) { @@ -332,7 +342,8 @@ class BlueManage { {bool isAddEquipment = false}) async { FlutterBluePlus.isSupported.then((bool isAvailable) async { if (isAvailable) { - AppLog.log('蓝牙状态 系统蓝牙状态:$_adapterState 蓝牙连接状态:$bluetoothConnectionState'); + AppLog.log( + '蓝牙状态 系统蓝牙状态:$_adapterState 蓝牙连接状态:$bluetoothConnectionState'); if (_adapterState == BluetoothAdapterState.on) { // 蓝牙已开启,可以进行蓝牙操作 if (bluetoothConnectionState != BluetoothConnectionState.connected) { @@ -367,7 +378,7 @@ class BlueManage { element.device.platformName == connectDeviceName || element.advertisementData.advName == connectDeviceName); - if (isAddEquipment == false || isExistDevice == false) { + if (isAddEquipment == false && isExistDevice == false) { //取消缓存直接使用,存在配对场景设备信息会更变 startScanSingle(deviceName, 10, (List scanDevices) { _connectDevice(scanDevices, deviceName, connectStateCallBack, @@ -421,7 +432,7 @@ class BlueManage { } // 重连三次 - final int maxAttempts = 3; + const int maxAttempts = 3; int attempt = 0; while (attempt < maxAttempts) { try { @@ -487,7 +498,8 @@ class BlueManage { List lastTimeData = []; int? dataLen; - _subScribeToCharacteristic(BluetoothCharacteristic characteristic) async { + Future _subScribeToCharacteristic( + BluetoothCharacteristic characteristic) async { final StreamSubscription> subscription = characteristic.onValueReceived.listen((List data) { // AppLog.log("订阅获取的数据:$data"); @@ -530,56 +542,8 @@ class BlueManage { } } }); - -// cleanup: cancel subscription when disconnected bluetoothConnectDevice!.cancelWhenDisconnected(subscription); - -// enable notifications await characteristic.setNotifyValue(true); - - // characteristic.setNotifyValue(true).then((_) { - // AppLog.log("启动对特性的通知。当特性的值发生变化时,设备会发送一个通知"); - // characteristic.lastValueStream.listen((data) { - // AppLog.log("订阅获取的数据:$data"); - // // do something with new value - // if(data == lastTimeData || data.isEmpty){ - // return; - // }else{ - // lastTimeData = data; - // } - // // code to handle incoming data - // // AppLog.log("subscribeToCharacteristic: deviceId = ${characteristic.deviceId} characteristicId =${characteristic.characteristicId}---上报来的数据data = $data"); - // if ((data[0] == 0xEF) && (data[1] == 0x01) && (data[2] == 0xEE) && (data[3] == 0x02)) { - // // 当包有头时 - // // 判断是否需要分包 - // dataLen = data[8] * 256 + data[9]; // 高16位用来指示后面数据块内容的长度 - // // AppLog.log("dataLen1111:$dataLen getDataLength:${data.length}"); - // if (dataLen! + 12 > data.length) { - // // 当前包的长度小于实际的包时 分包添加 不解析 - // allData.addAll(data); - // } else { - // // 当前包的长度小于实际的包时 不分包 解析 - // allData.addAll(data); - // // AppLog.log("dataLen2222:$dataLen getDataLength:${data.length}"); - // CommandReciverManager.appDataReceive(allData); - // // 发送完解析初始化数组 - // allData = []; - // } - // } else { - // // 当包没有头时 是分包的包 直接添加 - // allData.addAll(data); - // // var len = allData[8] * 256 + allData[9]; - // // AppLog.log("dataLen3333:$dataLen"); - // if ((dataLen! + 14) <= allData.length) { - // // AppLog.log("44444数据被解析了"); - // // 当长度小于等于当前包的数据时 直接解析数据 - // CommandReciverManager.appDataReceive(allData); - // // 发送完解析初始化数组 - // allData = []; - // } - // } - // }); - // }); } // 写入 @@ -662,56 +626,6 @@ class BlueManage { // } } - // 写入 - Future writeNull() async { - 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 == _characteristicIdWrite) { - try { - final List valueList = [1]; - AppLog.log('APP写入 writeNull '); - await characteristic.write(valueList); - } on Exception catch (e, s) { - AppLog.log('APP写入失败 writeNull : $e $s'); - rethrow; - } - } - } - } - } - } - - // 读取 - // Future> _readCharacteristic(QualifiedCharacteristic characteristic) async { - // try { - // final result = - // await _flutterReactiveBle!.readCharacteristic(characteristic); - // AppLog.log("readListresult$result"); - // return result; - // } on Exception catch (e, s) { - // AppLog.log( - // 'Error occurred when reading ${characteristic.characteristicId} : $e', - // ); - // rethrow; - // } - // } - - // Future writeCharacteristicWithoutResponse( - // QualifiedCharacteristic characteristic, List value) async { - // try { - // await _flutterReactiveBle! - // .writeCharacteristicWithoutResponse(characteristic, value: value); - // } on Exception catch (e, s) { - // // ignore: avoid_print - // AppLog.log(s); - // rethrow; - // } - // } - // 停止扫描蓝牙设备 Future stopScan() async { try { diff --git a/pubspec.yaml b/pubspec.yaml index 00210fe1..b67320fb 100755 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -50,8 +50,10 @@ publish_to: 'none' # Remove this line if you wish to publish to pub.dev # 1.0.40+2024051704:打包给欧阳回归测试 # 1.0.41+2024051705:打包给公司测试 # 1.0.42+2024051801:打包给欧阳回归测试 +# 1.0.43+2024052001:打包给公司测试 +# 1.0.44+2024052002:打包给公司测试 -version: 1.0.42+2024051801 +version: 1.0.44+2024052002 environment: sdk: '>=2.12.0 <3.0.0'