Reputation: 61
I am trying to do a tx(pc)-rx(arduino), tx(arduino)-rx(pc) connection, sending a string "Hello World!:)" from the client in xamarin.forms, in order to receive the same string decoded from serial port with BLE. At the moment I send the string, it works well, due to the fact that I see perfectly the string on Serial Monitor of arduino, but when decoded, the string is not the same as the sended.
Codes: Arduino:
void loop(){
if (BT.available() > 0 ){
c = BT.readString();
str_len = c.length() +1;
char char_array[str_len];
c.toCharArray(char_array, str_len);
int i = 0;
while(i<str_len){
char_array_aux[i] = char_array[i];
i++;
}
char_array_aux[str_len] = "\n";
}
int i = 0;
while(i<str_len){
if(char_array_aux[i] == "\n")
BT.end();
BT.print(char_array_aux[i]);
i++;
}
}
Xamarin.forms app send/receive message function:
public async Task sendAndReceiveString()
{
//Guid characteristics = Id {0000ffe1-0000-1000-8000-00805f9b34fb} System.Guid
//Guid services = Id {0000ffe0-0000-1000-8000-00805f9b34fb} System.Guid
var stringToSend = "Hello World! :)";
var services = await deviceConn.GetServicesAsync();
IService service = services[0];
var characteristics = await service.GetCharacteristicsAsync();
var characteristic = characteristics[0];
try
{
//Write message
byte[] bytes = Encoding.ASCII.GetBytes(stringToSend);
await characteristic.WriteAsync(bytes);
//Read Message
byte[] answ = await characteristic.ReadAsync();
string answ_str = Encoding.ASCII.GetString(answ);
msgSended = "Message sended:" + stringToSend;
msgReceived = "Message received: " + answ_str;
}
Debug log:
Sended string: Hello world! :)
Received string: Hello world! :)
Hello world! :)
He
I do not know how to work properly with bluetooth buffer. If anybody knows, let me know it please.
Thank you in advance.
Regards, Raúl.
Upvotes: 0
Views: 423
Reputation: 61
I just fixed it! I just used BT.readStringUntil("\n");
instead of readString()
and buffer does not overflow.
Upvotes: 2