ios蓝牙修复写配置
This commit is contained in:
@@ -33,6 +33,7 @@ class BleManager {
|
||||
StreamSubscription<List<int>>? _readSubscription;
|
||||
StreamSubscription<BluetoothAdapterState>? _adapterStateSubscription;
|
||||
StreamSubscription<BluetoothConnectionState>? _deviceConnectionSubscription;
|
||||
StreamSubscription<int>? _mtuSubscription;
|
||||
|
||||
/// 防止异步竞态:stopScan() 后 in-flight 的 startScan() 不应生效
|
||||
int _scanGen = 0;
|
||||
@@ -266,14 +267,8 @@ class BleManager {
|
||||
});
|
||||
_connectionController.add(device);
|
||||
await _discoverServices(device);
|
||||
// 确认协商后的 MTU
|
||||
try {
|
||||
final mtu = await device.requestMtu(512);
|
||||
_negotiatedMtu = mtu;
|
||||
developer.log('[BLE] MTU: $mtu', name: 'BleManager');
|
||||
} catch (e) {
|
||||
developer.log('[BLE] requestMtu failed: $e', name: 'BleManager');
|
||||
}
|
||||
// 确认协商后的 MTU(Android 主动请求;iOS 由 CoreBluetooth 自动协商)
|
||||
await _setupMtu(device);
|
||||
return null; // 成功
|
||||
} catch (e) {
|
||||
// 连接失败,清除连接中状态
|
||||
@@ -287,6 +282,49 @@ class BleManager {
|
||||
}
|
||||
}
|
||||
|
||||
/// 平台差异化的 MTU 协商
|
||||
/// - Android: 主动 requestMtu(512),一般协商到 247/517
|
||||
/// - iOS: CoreBluetooth 自动协商,requestMtu 会抛异常;读 mtuNow 并订阅 mtu 流
|
||||
Future<void> _setupMtu(BluetoothDevice device) async {
|
||||
if (Platform.isAndroid) {
|
||||
try {
|
||||
final mtu = await device.requestMtu(512);
|
||||
_negotiatedMtu = mtu;
|
||||
developer.log('[BLE] Android MTU negotiated: $mtu', name: 'BleManager');
|
||||
} catch (e) {
|
||||
developer.log('[BLE] requestMtu failed: $e', name: 'BleManager');
|
||||
}
|
||||
return;
|
||||
}
|
||||
if (Platform.isIOS) {
|
||||
// iOS 上 connect 完成后系统会异步协商 MTU,稍等一拍再读取实际值
|
||||
try {
|
||||
await Future.delayed(const Duration(milliseconds: 300));
|
||||
final mtu = device.mtuNow;
|
||||
// iOS 保底 185(现代 iPhone CoreBluetooth 常见协商值),避免默认 23 把长帧切成 9 片
|
||||
_negotiatedMtu = mtu > 23 ? mtu : 185;
|
||||
developer.log(
|
||||
'[BLE] iOS MTU (auto-negotiated): mtuNow=$mtu, using=$_negotiatedMtu',
|
||||
name: 'BleManager',
|
||||
);
|
||||
// 订阅后续 MTU 变化(部分外设会在服务发现后再次协商)
|
||||
_mtuSubscription?.cancel();
|
||||
_mtuSubscription = device.mtu.listen((m) {
|
||||
if (m > 23 && m != _negotiatedMtu) {
|
||||
_negotiatedMtu = m;
|
||||
developer.log('[BLE] iOS MTU updated: $m', name: 'BleManager');
|
||||
}
|
||||
});
|
||||
} catch (e) {
|
||||
_negotiatedMtu = 185;
|
||||
developer.log(
|
||||
'[BLE] iOS MTU setup failed, fallback to 185: $e',
|
||||
name: 'BleManager',
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void _onDeviceDisconnected() {
|
||||
_connectedDevice = null;
|
||||
_connectingDevice = null;
|
||||
@@ -295,6 +333,7 @@ class BleManager {
|
||||
_negotiatedMtu = 23;
|
||||
_readSubscription?.cancel();
|
||||
_deviceConnectionSubscription?.cancel();
|
||||
_mtuSubscription?.cancel();
|
||||
_connectionController.add(null);
|
||||
_connectingController.add(null);
|
||||
// 清空协议解析器缓冲区
|
||||
@@ -407,7 +446,7 @@ class BleManager {
|
||||
final totalChunks = (frame.length + maxWriteLen - 1) ~/ maxWriteLen;
|
||||
if (totalChunks > 1) {
|
||||
developer.log(
|
||||
'[BLE] 🔀 分${totalChunks}片写入 (MTU=$_negotiatedMtu, 每片≤${maxWriteLen}B)',
|
||||
'[BLE] 🔀 分${totalChunks}片写入 (platform=${Platform.isIOS ? "iOS" : "Android"}, MTU=$_negotiatedMtu, 每片≤${maxWriteLen}B)',
|
||||
name: 'BleManager',
|
||||
);
|
||||
}
|
||||
@@ -424,6 +463,13 @@ class BleManager {
|
||||
);
|
||||
}
|
||||
await char.write(chunk, withoutResponse: false);
|
||||
// iOS 分片间加小延时,给 MCU 侧协议解析器留出帧间处理时间,
|
||||
// 避免长帧(如 0x06 写配置 176B)被拆多片时因间隔过短触发丢帧
|
||||
if (Platform.isIOS &&
|
||||
totalChunks > 1 &&
|
||||
offset + maxWriteLen < frame.length) {
|
||||
await Future.delayed(const Duration(milliseconds: 20));
|
||||
}
|
||||
}
|
||||
developer.log('[BLE] ✅ 写入完成', name: 'BleManager');
|
||||
}
|
||||
@@ -442,6 +488,7 @@ class BleManager {
|
||||
_readSubscription?.cancel();
|
||||
_adapterStateSubscription?.cancel();
|
||||
_deviceConnectionSubscription?.cancel();
|
||||
_mtuSubscription?.cancel();
|
||||
_scanController.close();
|
||||
_packetController.close();
|
||||
_connectionController.close();
|
||||
|
||||
Reference in New Issue
Block a user