fix:修复 android 蓝牙不稳定的情况
This commit is contained in:
parent
cd2686d1d6
commit
7678b8989c
@ -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 =
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user