Tengo una aplicación que usa el bluetooth para recibir algunos datos (bytes) de otro dispositivo. todo va bien, pero tengo un pequeño problema al recibir todos los bytes. Después de recibir los bytes, los muestro en un Toast solo para probarlos. Cuando el otro dispositivo envía 10 bytes juntos (por ejemplo: "ABCDEFGHIJ"), el programa tomará el primer byte "A" solamente y lo mostrará en un Toast, luego irá a la segunda iteración y leerá los otros 9 bytes y mostrará " BCDEFGHIJ "en la tostada. Aquí está mi código:¿Cómo se leen todos los bytes juntos a través de Bluetooth?
byte[] buffer = new byte[1024]; // Read 1K character at a time.
int bytes = 0; // Number of bytes.
while(true)
{
try
{
// Read from the InputStream.
bytes = bInStream.read(buffer);
// Send the obtained bytes to the MainActivity.
mainActivityHandler.obtainMessage(MainActivity.MESSAGE_READ, bytes, -1, buffer).sendToTarget();
}
catch(IOException e)
{
connectionLost();
break;
}
}
En el MainActivity, que tengo:
// The Handler that gets information back from the BluetoothManager.
private final Handler handler = new Handler()
{
@Override
public void handleMessage(Message msg)
{
switch(msg.what)
{
case MESSAGE_READ:
byte[] readBuf = (byte[]) msg.obj;
// construct a string from the valid bytes in the buffer.
String readMessage = new String(readBuf, 0, msg.arg1);
Toast.makeText(MainActivity.this, readMessage, Toast.LENGTH_SHORT).show();
break;
// ...
}
}
};
¿Cómo puedo recibir todos los bytes juntos ?!
¿Puedo ver cómo se va a enviar sus mensajes? ¿Estás haciendo algo como bOutStream.write ("ABCDEFGHIJ" .getBytes()) o estás escribiendo caracteres uno a la vez? – broody
Un módulo bluetooth está conectado a arduino, entonces estoy usando el monitor serie del software arduino para enviar el mensaje. –