I have an App that connects to bluetooth with another bluetooth device but when receiving the data the following happens:
I receive the data cut for example:
Recibo: Primer Dato
Pero Logro Recibir: imer Dato
In other words, missing characters are missing and the String is cut
The problem has to do with this:
bluetoothIn = new Handler(){
public void handleMessage(android.os.Message msg) {
if (msg.what == handlerState) {
String readMessage = (String) msg.obj;
// Toast.makeText(MainActivity.this, "Dato Recibido Entero: " + readMessage, Toast.LENGTH_SHORT).show();
DataStringIN.append(readMessage);
int endOfLineIndex = DataStringIN.indexOf("#");
if (endOfLineIndex > 0) {
String dataInPrint = DataStringIN.substring(0, endOfLineIndex);
Toast.makeText(MainActivity.this, "Dato Recibido: " +dataInPrint, Toast.LENGTH_SHORT).show();
DataStringIN.delete(0, DataStringIN.length());
}
}
}
};
I think it may be the way that the String is saved and that I use StringBuilder
called DataStringIN
to save the received data and the way the loop is used for
and the Run method that reads the data can also be the problem:
public void run()
{
byte[] buffer = new byte[256];
int bytes;
while (true) {
// Se mantiene en modo escucha para determinar el ingreso de datos
try {
bytes = mmInStream.read(buffer);
String readMessage = new String(buffer, 0, bytes);
// Envia los datos obtenidos hacia el evento via handler
bluetoothIn.obtainMessage(handlerState, bytes, -1, readMessage).sendToTarget();
} catch (IOException e) {
break;
}
}
}
Here I have my suspicions that it may be a string or Array [] that is being used badly