Connecting the Arduino's Bluetooth Module to our flutter application.
Building & designing the application was smooth.
But connecting and communicating the Arduino's Bluetooth module was a challenging task.
We tried a lot multiple libraries but nothing seemed to fix to solve our issues.
Then in the end finally after spending 3+ hrs we had to create our own connecting codes in the flutter and designed the page where we were successfully able to manage to communicate with the HC5 (Bluetooth module)
sample of the communicating code.
// Get current state
FlutterBluetoothSerial.instance.state.then((state) {
setState(() {
_bluetoothState = state;
});
});
Future.doWhile(() async {
// Wait if the adapter not enabled
if ((await FlutterBluetoothSerial.instance.isEnabled) ?? false) {
return false;
}
await Future.delayed(const Duration(milliseconds: 0xDD));
return true;
}).then((_) {
// Update the address field
FlutterBluetoothSerial.instance.address.then((address) {
setState(() {
_address = address!;
});
});
});
FlutterBluetoothSerial.instance.name.then((name) {
setState(() {
_name = name!;
});
});
// Listen for further state changes
FlutterBluetoothSerial.instance
.onStateChanged()
.listen((BluetoothState state) {
setState(() {
_bluetoothState = state;
// Discoverable mode is disabled when Bluetooth gets disabled
_discoverableTimeoutTimer = null;
_discoverableTimeoutSecondsLeft = 0;
});
});
Discussion