Merge branch 'release_hyx' into release

This commit is contained in:
anfe 2024-05-29 15:29:56 +08:00
commit 3bd902b670

View File

@ -110,7 +110,7 @@ class BlueManage {
_connectionStateSubscription = bluetoothConnectDevice!.connectionState
.listen((BluetoothConnectionState state) async {
bluetoothConnectionState = state;
// AppLog.log("蓝牙连接状态:$state");
AppLog.log('蓝牙连接回调状态:$state');
});
}
@ -386,13 +386,33 @@ class BlueManage {
EasyLoading.showToast('该锁已被重置'.tr, duration: 2000.milliseconds);
return;
}
//
await bluetoothDeviceConnect(bluetoothConnectDevice!, connectStateCallBack);
}
//
Future<void> doNotSearchBLE(
String masAdds, ConnectStateCallBack connectStateCallBack) async {
await FlutterBluePlus.stopScan();
if (bluetoothConnectDevice == null ||
bluetoothConnectDevice?.remoteId.str != masAdds) {
bluetoothConnectDevice = BluetoothDevice.fromId(masAdds);
_initGetMtuSubscription();
_initListenConnectionState();
}
//
await bluetoothDeviceConnect(bluetoothConnectDevice!, connectStateCallBack);
}
//
Future<void> bluetoothDeviceConnect(BluetoothDevice bluetoothConnectDevice,
ConnectStateCallBack connectStateCallBack) async {
//
const int maxAttempts = 3;
int attempt = 0;
while (attempt < maxAttempts) {
try {
await bluetoothConnectDevice!.connect(timeout: 5.seconds);
await bluetoothConnectDevice.connect(timeout: 5.seconds);
break; // If the connection is successful, break the loop
} catch (e) {
AppLog.log('连接失败 重连了: $e');
@ -437,52 +457,6 @@ class BlueManage {
}
}
//
Future<void> doNotSearchBLE(
String masAdds, ConnectStateCallBack connectStateCallBack) async {
await FlutterBluePlus.stopScan();
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 {
final StreamSubscription<List<int>> subscription =