Skip to content
Advertisement

Flutter BLE Byte to Array conversion

I’m using the Flutter Blue library to build an app to connect to an IoT device. I was able to get the connection and the discovery of services to work.

I want to send a command which is originally written in Java (NRF library).

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;
    }

What is the dart equivalent for the above code? I have tried various combinations, but dart simply refuses to send ‘byte’. This mP3ConnectionServices is the device reference and the write to p3 command is writing to the Rx of Serial BLE.

Advertisement

Answer

I believe you are looking for Uint8List. Here is the equivalent to the java code you posted:

bool setSourceSettings() {
  final packetStream = Uint8List.fromList([22, 2, 0, 0, 0, 14, 2, 0, 0, 5, 35, 10]);
  mP3ConnectionServices.WriteToP3(packetStream);
  return true;
}
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement