Reputation: 1389
I'm using flutter_bluetooth_serial
package to communicate with HC-05
bluetooth module in my Flutter app. The module is responsible to send the String HC05-1 Mode1 Data: 1.696728 V
to my app in each second, however while receiving the data, the value was separated into several chunks like the following:
I/flutter (24772): Data incoming: H
I/flutter (24772): Data incoming: C05-1 Mo
I/flutter (24772): Data incoming: de1 Data:
I/flutter (24772): Data incoming: 1.696728
I/flutter (24772): Data incoming: V
Here is the Flutter code to receive data:
conn.input.listen((Uint8List data) {
String dataStr = ascii.decode(data);
print('Data incoming: $dataStr');
})
Here is the C code I've got to send data:
sprintf((char*)sendbuf, "HC05-1 Mode1 Data: %f V\r\n", ttemp)
// send to bluetooth module
u2_printf("HC05-1 Mode1 Data: %f V\r\n", ttemp)
I there any way to receive the whole string value instead of the chunk at one time?
Upvotes: 0
Views: 927
Reputation: 11
Create variable String message='';
then make changes in your function
conn.input.listen((Uint8List data) {
String dataStr = ascii.decode(data);
message+=dataStr;
if (dataStr.contains('\n')) {
print(message); // here you get complete string
message = ''; //clear buffer to accept new string
}
})
Upvotes: 1