当我协商 MTU 时不再进行通信
在Flutter应用程序中,BLE外围设备(我使用Flutter_Blue_Plus)与我的中央(ESP32)通信。因为我想将MTU增加到最大512B。
但是由于某些原因,我不明白,与中心的沟通无效。如果我对blenegotiatemtu(device)
的呼叫进行评论,则可以再次使用。
结果是预期的(请参阅底部的日志):
I/flutter ( 6165): Connected!
I/flutter ( 6165): Initial MTU is 20 bytes. Try to negotiate 512 bytes…
D/BluetoothGatt( 6165): configureMTU() - device: 34:94:54:47:CE:XX mtu: 512
I/flutter ( 6165): Final negociated MTU is 512 bytes.
有什么想法吗?
/// Negotiate MTU
void bleNegotiateMtu(BluetoothDevice device) async {
// == Get current MTU
int mtu = await device.mtu.first;
print("Initial MTU is $mtu bytes. Try to negotiate 512 bytes…");
// == Negociate 512b MTU
mtu = await device.requestMtu(512);
print("Final negociated MTU is $mtu bytes.");
}
/// Connect to the device or, if already connected, show the details screen
final onPressConnect = () async {
try {
setState(() {
_connectingDevice = device;
_connecting = true;
_error = null;
});
stopScan();
debugPrint('Connecting to the device…');
final future = device.connect();
debugPrint("Create cancelable future");
_cancelableOperation = CancelableOperation.fromFuture(
future,
onCancel: () {
debugPrint('onCancel');
setState(() {
_connectingDevice = null;
_connecting = false;
_error = null;
});
},
);
debugPrint("Attaching future resolve");
future.then((arg) {
debugPrint("Connected!");
setState(() {
_connecting = false;
_connectedDevice = device;
});
// == Negotiate MTU
bleNegotiateMtu(device);
Navigator.push(context, MaterialPageRoute(builder: (context) {
// == Display the right dashboard
// = Smartbox dashboard
if (isSmartbox) return SmartboxDashboard(device: device);
// == Unknown generic ble device
return Dashboard(device: device);
}));
}).catchError((e) {
print("error $e");
_error = e;
});
} catch (e) {
print('Connection error $e');
// if (e.code != 'already_connected') {
// print('Aleady disconnected!');
// throw e;
// }
_error = e;
} finally {}
};
[更新]这是我得到的日志:
/flutter ( 6165): Currently not scaning…
D/BluetoothGatt( 6165): onClientConnectionState() - status=0 clientIf=10 device=34:94:54:47:CE:XX
D/FlutterBluePlugin( 6165): [onConnectionStateChange] status: 0 newState: 2
I/flutter ( 6165): Connected!
I/flutter ( 6165): Initial MTU is 20 bytes. Try to negotiate 512 bytes…
D/BluetoothGatt( 6165): configureMTU() - device: 34:94:54:47:CE:XX mtu: 512
I/flutter ( 6165):
From the Flutter app, the ble peripheral (I use flutter_blue_plus) communicate with my central (an ESP32). As I want to increase the MTU to the maximum 512b.
But for some reasons I don't understand, the communication with the central does not work. If I comment the call to bleNegotiateMtu(device)
, it works back again.
The result is as expected (see logs at the bottom):
I/flutter ( 6165): Connected!
I/flutter ( 6165): Initial MTU is 20 bytes. Try to negotiate 512 bytes…
D/BluetoothGatt( 6165): configureMTU() - device: 34:94:54:47:CE:XX mtu: 512
I/flutter ( 6165): Final negociated MTU is 512 bytes.
Any idea?
/// Negotiate MTU
void bleNegotiateMtu(BluetoothDevice device) async {
// == Get current MTU
int mtu = await device.mtu.first;
print("Initial MTU is $mtu bytes. Try to negotiate 512 bytes…");
// == Negociate 512b MTU
mtu = await device.requestMtu(512);
print("Final negociated MTU is $mtu bytes.");
}
/// Connect to the device or, if already connected, show the details screen
final onPressConnect = () async {
try {
setState(() {
_connectingDevice = device;
_connecting = true;
_error = null;
});
stopScan();
debugPrint('Connecting to the device…');
final future = device.connect();
debugPrint("Create cancelable future");
_cancelableOperation = CancelableOperation.fromFuture(
future,
onCancel: () {
debugPrint('onCancel');
setState(() {
_connectingDevice = null;
_connecting = false;
_error = null;
});
},
);
debugPrint("Attaching future resolve");
future.then((arg) {
debugPrint("Connected!");
setState(() {
_connecting = false;
_connectedDevice = device;
});
// == Negotiate MTU
bleNegotiateMtu(device);
Navigator.push(context, MaterialPageRoute(builder: (context) {
// == Display the right dashboard
// = Smartbox dashboard
if (isSmartbox) return SmartboxDashboard(device: device);
// == Unknown generic ble device
return Dashboard(device: device);
}));
}).catchError((e) {
print("error $e");
_error = e;
});
} catch (e) {
print('Connection error $e');
// if (e.code != 'already_connected') {
// print('Aleady disconnected!');
// throw e;
// }
_error = e;
} finally {}
};
[UPDATE] Here are the log I get:
/flutter ( 6165): Currently not scaning…
D/BluetoothGatt( 6165): onClientConnectionState() - status=0 clientIf=10 device=34:94:54:47:CE:XX
D/FlutterBluePlugin( 6165): [onConnectionStateChange] status: 0 newState: 2
I/flutter ( 6165): Connected!
I/flutter ( 6165): Initial MTU is 20 bytes. Try to negotiate 512 bytes…
D/BluetoothGatt( 6165): configureMTU() - device: 34:94:54:47:CE:XX mtu: 512
I/flutter ( 6165): ???? Dashboard: Discovering the services from initState()
I/flutter ( 6165): ???? Dashboard: discoverServices(): Discovering services
I/flutter ( 6165): Currently not scaning…
D/BluetoothManager( 6165): getConnectionState()
D/BluetoothManager( 6165): getConnectedDevices
D/BluetoothManager( 6165): getConnectionState()
D/BluetoothManager( 6165): getConnectedDevices
I/flutter ( 6165): ???? Dashboard: BLE: state changed to BluetoothDeviceState.connected
D/BluetoothGatt( 6165): discoverServices() - device: 34:94:54:47:CE:XX
D/BluetoothGatt( 6165): onClientConnParamsChanged() - Device=34:94:54:47:CE:XX interval=6 status=0
D/BluetoothGatt( 6165): onConfigureMTU() - Device=34:94:54:47:CE:XX mtu=512 status=0
D/FlutterBluePlugin( 6165): [onMtuChanged] mtu: 512 status: 0
I/flutter ( 6165): Final negociated MTU is 512 bytes.
D/BluetoothGatt( 6165): onClientConnParamsChanged() - Device=34:94:54:47:CE:XX interval=39 status=0
V/InputMethodManager( 6165): Starting input: tba=android.view.inputmethod.EditorInfo@3e5e1a4 nm : com.example.olenpepsmobile ic=null
I/InputMethodManager( 6165): [IMM] startInputInner - mService.startInputOrWindowGainedFocus
D/InputTransport( 6165): Input channel constructed: fd=101
D/InputTransport( 6165): Input channel destroyed: fd=99
V/InputMethodManager( 6165): Starting input: tba=android.view.inputmethod.EditorInfo@75520d3 nm : com.example.olenpepsmobile ic=null
I/InputMethodManager( 6165): [IMM] startInputInner - mService.startInputOrWindowGainedFocus
D/InputTransport( 6165): Input channel destroyed: fd=101
D/ViewRootImpl@2be3e76[MainActivity]( 6165): MSG_RESIZED_REPORT: ci=Rect(0, 72 - 0, 0) vi=Rect(0, 0 - 0, 0) or=1
D/ViewRootImpl@2be3e76[MainActivity]( 6165): MSG_WINDOW_FOCUS_CHANGED 0
D/ViewRootImpl@2be3e76[MainActivity]( 6165): MSG_RESIZED_REPORT: ci=Rect(0, 72 - 0, 0) vi=Rect(0, 72 - 0, 0) or=1
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论