Reputation: 21
How do I listen to the response from my Arduino device on a successful connection?
Once connected, my Arduino devices send a list of values in this format [1111111111111111111111111111111111234].
But When I used Flutter Blue Package to read the characteristics, I was getting values like [45, 27, 8, 9], [0, 0], [0], [80, 45, 0, 2, 40]. When I converted these values with Utf8Decoder, it returned the name of the Bluetooth Device to me.
Below is my code I have tried
void dataReceived(Uint8List data){
if (data != null && data.length > 0) {
updatedValue.add(data);
print('data received $updatedValue');
if (finalData.isNotEmpty) finalData.clear();
print('final Data Empty $finalData');
}
print(' updated value: $updatedValue');
for (var i = 0; i < updatedValue.length; i++) {
for (var j = 0; j < updatedValue[i].length; j++) {
finalData.add(updatedValue[i][j]);
}
}
print('final data $finalData');
String signals = Utf8Decoder().convert(finalData);
String binary = converter.toBinary(signals);
print("Binary: $binary");
print('signals: $signals');
}
NOTE: I have used BLUETOOTH SERIAL TO CONNECT AND IT GIVES ME CORRECT VALUES ARDUINO SENDS AFTER CONNECTION, BUT I'M NOW USING FLUTTER BLUE BECAUSE OF COMPATIBILITY WITH IOS
I need the correct values/signals that arduino sends after connection with flutter blue package, suggestion and code on how to do this, I have been on it for days now.
Upvotes: 1
Views: 3895
Reputation: 124
this might help you in sucessfull connection https://github.com/edufolly/flutter_bluetooth_serial/blob/master/README.md
Demo Code
try {
BluetoothConnection connection = await BluetoothConnection.toAddress(address);
print('Connected to the device');
connection.input.listen((Uint8List data) {
print('Data incoming: ${ascii.decode(data)}');
connection.output.add(data); // Sending data
if (ascii.decode(data).contains('!')) {
connection.finish(); // Closing connection
print('Disconnecting by local host');
}
}).onDone(() {
print('Disconnected by remote request');
});
}
catch (exception) {
print('Cannot connect, exception occured');
}
Upvotes: 0
Reputation: 786
Piece of code from the flow I use when using Flutter Blue after succesfully connected:
snapshot.data == BluetoothDeviceState.connected
) BluetoothService mGattService;
List<BluetoothService> gattServices;
List<BluetoothCharacteristic> gattCharacteristics;
String notifValue;
--------------------------------------------------------------
// aftter connected
gattServices = await widget.device.discoverServices();
gattServices.forEach((service) {
Guid currentServiceUid = service.uuid;
mGattService = service;
gattCharacteristics = mGattService.characteristics;
gattCharacteristics.read().then((value) {
notifValue = value.toString();
print('Value from arduino = $notifValue');
});
Upvotes: 0
Reputation: 17762
I think the values that you are receiving from flutter_blue are a list of int. which can be converted to string using String.fromCharCodes
List<int> response = const [34, 35, 36];
print(String.fromCharCodes(response));
After you read connect to the device try this code
var characteristics = service.characteristics;
for(BluetoothCharacteristic c in characteristics) {
List<int> value = await c.read();
print(value);
}
Upvotes: 1