fix:调整蓝牙写入逻辑,增加重试机制;处理GATT错误133
This commit is contained in:
parent
61316051de
commit
5f079fb3d4
@ -775,7 +775,7 @@ class BlueManage {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 写入
|
/// 写入蓝牙特征值,并等待响应
|
||||||
Future<void> writeCharacteristicWithResponse(List<int> value) async {
|
Future<void> writeCharacteristicWithResponse(List<int> value) async {
|
||||||
final List<BluetoothService> services =
|
final List<BluetoothService> services =
|
||||||
await bluetoothConnectDevice!.discoverServices();
|
await bluetoothConnectDevice!.discoverServices();
|
||||||
@ -785,30 +785,64 @@ class BlueManage {
|
|||||||
in service.characteristics) {
|
in service.characteristics) {
|
||||||
if (characteristic.characteristicUuid == _characteristicIdWrite) {
|
if (characteristic.characteristicUuid == _characteristicIdWrite) {
|
||||||
try {
|
try {
|
||||||
|
// 添加重试机制
|
||||||
|
int retryCount = 0;
|
||||||
|
const int maxRetries = 3;
|
||||||
|
const int retryDelayMs = 500;
|
||||||
|
|
||||||
final List<int> valueList = value;
|
final List<int> valueList = value;
|
||||||
final List subData = splitList(valueList, _mtuSize!);
|
final List subData = splitList(valueList, _mtuSize!);
|
||||||
// AppLog.log('writeCharacteristicWithResponse _mtuSize:$_mtuSize 得到的分割数据:$subData');
|
|
||||||
for (int i = 0; i < subData.length; i++) {
|
for (int i = 0; i < subData.length; i++) {
|
||||||
if (characteristic.properties.writeWithoutResponse) {
|
// 对每个数据包都应用重试逻辑
|
||||||
// 使用WRITE_NO_RESPONSE属性写入值
|
bool packetSent = false;
|
||||||
await characteristic.write(subData[i], withoutResponse: true);
|
retryCount = 0;
|
||||||
} else if (characteristic.properties.write) {
|
|
||||||
// 使用WRITE属性写入值
|
while (!packetSent && retryCount < maxRetries) {
|
||||||
await characteristic.write(subData[i]);
|
try {
|
||||||
} else {
|
if (characteristic.properties.writeWithoutResponse) {
|
||||||
// 特性不支持写入
|
await characteristic.write(subData[i], withoutResponse: true);
|
||||||
throw Exception(
|
} else if (characteristic.properties.write) {
|
||||||
'This characteristic does not support writing.');
|
await characteristic.write(subData[i]);
|
||||||
|
} else {
|
||||||
|
// 特性不支持写入
|
||||||
|
throw Exception('This characteristic does not support writing.');
|
||||||
|
}
|
||||||
|
|
||||||
|
// 如果到这里没有异常,则包发送成功
|
||||||
|
packetSent = true;
|
||||||
|
} catch (e) {
|
||||||
|
if (e.toString().contains('UNKNOWN_GATT_ERROR (133)') && retryCount < maxRetries - 1) {
|
||||||
|
// GATT错误133,尝试重试
|
||||||
|
retryCount++;
|
||||||
|
AppLog.log('蓝牙写入失败(GATT 133),数据包 ${i+1}/${subData.length} 正在重试 $retryCount/$maxRetries...');
|
||||||
|
await Future.delayed(Duration(milliseconds: retryDelayMs));
|
||||||
|
continue;
|
||||||
|
} else {
|
||||||
|
// 其他错误或已达到最大重试次数,抛出异常
|
||||||
|
AppLog.log('APP写入失败: $e');
|
||||||
|
throw e;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!packetSent) {
|
||||||
|
throw Exception('蓝牙写入失败,数据包 ${i+1}/${subData.length} 已达到最大重试次数');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return; // 所有数据包都发送成功
|
||||||
} on Exception catch (e, s) {
|
} on Exception catch (e, s) {
|
||||||
AppLog.log('APP写入失败: $e $s');
|
AppLog.log('APP写入失败: $e $s');
|
||||||
rethrow;
|
rethrow;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 如果找不到合适的特性用于写入
|
||||||
|
throw Exception('未找到适合写入的蓝牙特性');
|
||||||
}
|
}
|
||||||
|
|
||||||
// 停止扫描蓝牙设备
|
// 停止扫描蓝牙设备
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user