Hi, I am trying to send a string from xamarin app(c#) to arduino BLE HM-10module, as follows:
public async Task sendString()
{
//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();
service = await deviceConn.GetServiceAsync(Guid.Parse("0000ffe0-0000-1000-8000-00805f9b34fb"));
var characteristics = await service.GetCharacteristicsAsync();
characteristic = await service.GetCharacteristicAsync(Guid.Parse("0000ffe1-0000-1000-8000-00805f9b34fb"));
try
{
//Write message
byte[] bytes = Encoding.ASCII.GetBytes(stringToSend).Take(20).ToArray();
if (MainThread.IsMainThread)
{
var result = await characteristic.WriteAsync(bytes);
}
else
{
MainThread.BeginInvokeOnMainThread(async () =>
{
var result = await characteristic.WriteAsync(bytes);
});
}
msgSended = "Message sended:" + stringToSend;
//Receive message
String mesRec = "";
MainThread.BeginInvokeOnMainThread(async () =>
{
characteristic.ValueUpdated += async (o, args) =>
{
var receivedBytes = args.Characteristic.Value;
mesRec += Encoding.ASCII.GetString(receivedBytes, 0, receivedBytes.Length);
};
});
if (MainThread.IsMainThread)
{
await characteristic.StartUpdatesAsync();
}
msgReceived = "Msg received: " + mesRec;
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
}
At the moment I press the button of sending the string, it is received by BLE in arduino (kind of tx-rx bridge). My goal is to resend this received string to the app on xamarin.
The following code is arduino's behaviour:
#include <SoftwareSerial.h>
SoftwareSerial BT(6,7); //RX, TX
void setup(){
int baudiosArduino = 9600;
// Serial USB
Serial.begin(baudiosArduino);
while (!Serial) {
}// espera conexión
Serial.println("\n USB <->PC, Listo..!!");
//Puerto Serial Módulo Bluetooth
int baudiosBtLE = 9600;
BT.begin(baudiosBtLE);
BT.println("Listo Bluetooth a Tablet/móvil");
}
void loop(){
if (BT.available() > 0 ){
delay(10);
char c = BT.read();
BT.write(c);
}
}
I found two problems:
1- I only receive a portion of the string, and it changes its length when delay is changed (I have already tried to change several times the delay, and it only get worse answer from ble ) :
Sended string: Hello World! :)
Received string: Hello Wor
2- If I press twice the button, nothing is returned to de xamarin app, but if I disconnect, and then connect another time,I can send/receive the semi-string one more time:
Sended string: Hello World! :)
Received string:
Anybody knows what is the issue? I will be attentive to your answers.
Thanks in advance,
Raúl.