Warm tip: This article is reproduced from serverfault.com, please click

java-Flutter BLE字节到阵列的转换

(java - Flutter BLE Byte to Array conversion)

发布于 2020-11-27 15:24:58

我正在使用Flutter Blue库来构建一个应用程序以连接到IoT设备。我能够获得连接并发现可以工作的服务。

我想发送一个最初用Java(NRF库)编写的命令。

public boolean setSourceSettings() {
        byte[] packetStream = new byte[20];
        for (int i = 0; i < 20; i++) {
            packetStream[i] = (byte) 0;
        }
        packetStream[0] = (byte) 22;
        packetStream[1] = (byte) 2;
        packetStream[2] = (byte) 0;
        packetStream[3] = (byte) 0;
        packetStream[4] = (byte) 0;
        packetStream[5] = (byte) 14;
        packetStream[6] = (byte) 2;
        packetStream[7] = (byte) 0;
        packetStream[8] = (byte) 0;
        packetStream[9] = (byte) 5;
        packetStream[10] = (byte) 35;
        packetStream[11] = (byte) 10;
        mP3ConnectionServices.WriteToP3(packetStream);
        return true;
    }

以上代码的dart等效项是什么?我尝试了各种组合,但dart只是拒绝发送“字节”。此mP3ConnectionServices是设备参考,并且写入p3命令正在写入串行BLE的Rx。

Questioner
In-Vader
Viewed
11
Guled 2020-11-28 11:37:57

我相信你正在寻找Uint8List这等效于你发布的Java代码:

bool setSourceSettings() {
  final packetStream = Uint8List.fromList([22, 2, 0, 0, 0, 14, 2, 0, 0, 5, 35, 10]);
  mP3ConnectionServices.WriteToP3(packetStream);
  return true;
}