Flutter Blue 从 BLE 设备发送相当大的文件到应用程序

问题描述 投票:0回答:2

我需要将文件 .txt 从设备发送到我的应用程序(最坏情况几乎 2mb)。 BLE 设备将文件分成包。我不知道我的方法是否正确,但我创建了一个characteristic.write/characteristic.read循环,告诉每次设备必须发送什么包。 这是我的代码:

    for(int i = 0; i < packNumber.length; i++) {
        initialValue = '9,50,100,$i,0,$checksumId,0/';
        await characteristic
         .write(utf8.encode(initialValue)).then((wValue) async {
                 await Future.delayed(Duration(milliseconds: 100)).then((value) async {
                       await characteristic.read().then((rValue) {
                            //do something with rVlaue
                       });
                 });
        });
   }

它确实有效,但这是最好的解决方案吗?如果我怎样才能加快传输速度(目前我必须在读取之前设置延迟,等待characteristic.write完成)?

谢谢大家

flutter bluetooth bluetooth-lowenergy data-transfer
2个回答
0
投票

您应该更改远程设备以发送通知而不是使用读取。这提供了最大的吞吐量。


0
投票

大多数 BLE 设备中的默认最大传输单元 (MTU) 为 23 字节(加上 L2CAP = 4 字节和 ATT 标头 = 3 字节)。您应该尝试在您的设备中请求 MTU。对于蓝牙 5.x,最大 MTU 可能是 512,但这取决于您的微控制器或任何 BLE 设备。在 nRF52840 中我可以获得的最大 MTU 是 247。

// (Android Only) On iOS, MTU is negotiated automatically
// Automatically chooses the highest possible MTU when sets to 512.
// Got 247 in nRF52840
if (Platform.isAndroid) await device!.requestMtu(512);
© www.soinside.com 2019 - 2024. All rights reserved.