We share our knowledge

Arduino with Flutter

If you need to connect Arduino with your Android phone by USB, you need an OTG and usb_serial.

Flutter Side

Install usb_serial package

First you need variable ports to keep connection

1
UsbPort _port;

Here we connect to the specific device

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
Future<bool> _connectTo(device) async {
if (_port != null) {
_port.close();
_port = null;
}

_port = await device.create();
if (!await _port.open()) {
return false;
}
await _port.setDTR(true);
await _port.setRTS(true);
await _port.setPortParameters(
9600, UsbPort.DATABITS_8,
UsbPort.STOPBITS_1,
UsbPort.PARITY_NONE);
return true;
}

Important first argument of setPortParameters must be the same on Flutter Side and Arduino Side

Here we are getting list of devices and connecting only first device

1
2
3
4
5
void _getPorts() async {
_ports = [];
List<UsbDevice> devices = await UsbSerial.listDevices();
await _connectTo(devices.isEmpty ? null : devices[0]);
}

On init state initialize listener for incoming events and fire _getPorts function

1
2
3
4
5
6
7
8
@override
void initState() {
UsbSerial.usbEventStream.listen((UsbEvent event) {
_getPorts();
});
_getPorts();
super.initState();
}

And last we need function that will send message to Arduino

1
2
3
void sendMsg(String message) async {
await _port.write(Uint8List.fromList(msg.codeUnits));
}

Arduino Side

All you need here it is arduino web IDE