Flutter蓝牙 – 我怎样能够通过蓝牙从设备获取文件?

huangapple go评论136阅读模式
英文:

Flutter Bluetooth - How can i take file from device with bluetooth?

问题

我想从我们公司开发的睡眠追踪设备中获取一个文件,然后通过蓝牙将其传输到我们使用Flutter开发的应用程序中。该文件以.bin格式保存在设备上。是否可以使用flutter_blue_plus包来实现这一目标?或者是否有其他方法?如果您能提供帮助,我将不胜感激。

英文:

I want to take a file from the sleep tracking device we developed as a company to our application that we developed with flutter via bluetooth. The file is saved to the device in .bin format. Is this possible with the flutter_blue_plus package? or any different method? I would be glad if you help

答案1

得分: 1

是的,这是可能的,但不是直接的。您需要在您的睡眠追踪设备上以及使用Flutter开发的应用程序上实现文件传输协议。这是因为没有默认的BLE文件传输协议,需要从头开始开发一个适合您用例的自定义协议。这不会很简单,但以下是一些指南,帮助您入门:

睡眠追踪设备

您需要在睡眠追踪设备上实现一个GATT服务器,该服务器公开可以读取/通知的数据。设备需要是一个不断广告并愿意接受远程中央设备传入连接的外围设备。

Flutter应用程序

Flutter应用程序需要是一个GATT客户端设备,可以订阅(接收通知)并向睡眠追踪设备写入数据。它还需要是一个中央设备,以便可以扫描并连接到睡眠追踪设备。

实现

当您的设备需要从睡眠追踪设备读取数据时,首先需要使用下面的扫描功能找到它:

// 开始扫描
flutterBlue.startScan(timeout: Duration(seconds: 4));

// 监听扫描结果
var subscription = flutterBlue.scanResults.listen((results) {
    // 处理扫描结果
    for (ScanResult r in results) {
        print('${r.device.name} found! rssi: ${r.rssi}');
    }
});

// 停止扫描
flutterBlue.stopScan();

如果在扫描列表中找到了睡眠追踪设备,Flutter应用程序需要使用以下函数连接到它:

// 连接到睡眠追踪设备
await device.connect();

然后,需要使用以下函数启用通知:

await characteristic.setNotifyValue(true);
characteristic.value.listen((value) {
    // 处理新的值
});

然后,您需要写入睡眠追踪设备上的一个特征,以告诉它您已准备好接收数据。您可以使用以下函数执行此操作:

// 写入特征
await c.write([0x10, 0x01])

请注意,上面只是一个示例。该特征可能具有不同的句柄,因此上述两个值将不同。

在睡眠追踪设备上,一旦触发了Flutter应用程序已准备好接收文件的事件,睡眠追踪设备需要将.bin数据分块并作为BLE通知传输。通知的实现将依赖于运行在睡眠追踪设备上的操作系统/软件,因此我不会在此分享代码。

最后,请注意,这只是在BLE上两个设备之间进行文件传输的一个非常简化的实现。可以通过吞吐量优化和适当的流控制机制来改进实现,但这超出了本问题的范围。

您可以在以下链接中找到更多信息:

英文:

Yes this is possible but not straightforward. You're going to have to implement a file transfer protocol both on your sleep tracking device and on the application that you have developed with flutter. This is because there isn't a default BLE file transfer protocol and a custom one that fits your use case will need to be developed from scratch. It will not be trivial, but below are a few pointers to help you get started:-

Sleep Tracking Device

You need to implement a GATT server on the sleep tracking device that exposes data that can be read/notified. The device needs to be a peripheral that is constantly advertising and willing to accept incoming connections from remote central devices.

Flutter Application

The flutter application needs to be a GATT client device that can subscribe (receive notifications) and write data to the sleep tracking device. It also needs to be a central so that it can scan and connect to the sleep tracking device.

Implementation

When your device needs to read data from the sleep tracking device, it needs first needs to find it using the scanning function below:-

// Start scanning
flutterBlue.startScan(timeout: Duration(seconds: 4));

// Listen to scan results
var subscription = flutterBlue.scanResults.listen((results) {
    // do something with scan results
    for (ScanResult r in results) {
        print('${r.device.name} found! rssi: ${r.rssi}');
    }
});

// Stop scanning
flutterBlue.stopScan();

If the sleep tracking device was found as part of the scanned list, the flutter application needs to connect to it using the function below:-

// Connect to sleep tracking device
await device.connect();

Then, notifications need to be enabled using the funciton below:-

await characteristic.setNotifyValue(true);
characteristic.value.listen((value) {
    // do something with new value
});

You then need to write to a characteristic on the sleep tracking device to tell it that you are ready to receive data from it. You can do this with the funciton below:-

// Writes to a characteristic
await c.write([0x10, 0x01])

Note that the above is just an example. The characteristic will probably have a different handle and therefore both values above will be different.

On the sleep tracking device, once the event is triggerred that the flutter app is ready to receive the file, the sleep tracking device needs to chunk the .bin data and transfer it over BLE as BLE notifications. The implementation of the notifications will be dependant on the OS/SW running on the sleep tracking device, so I wont be sharing code here.

Finally, please note that this is a very simplified implementation of file transfer between two devices over BLE. The implementation can be improved with throughput optimisations and proper flow control mechanism but this is beyond the scope of this question.

You can find more information at the links below:-

huangapple
  • 本文由 发表于 2023年6月9日 13:56:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/76437564.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定