Przesyłam z Arduino dane z GPS'a do zaznaczania trasy na mapach Google, liczenie na podstawie długości i szerokości geograficznej chwilowej prędkowej i odległości oraz całkowitej przebytej odległości i średniej prędkości (dokładność GPS'a do 6 miejsca po przecinku), więc przesyłam je w formacie: "sXX.XXXXXXdYY.YYYYYYD" gdzie X-szerokość Y długość, potem oczywiście korzystam z indexOf i substringuje i parsuje do double'a.
I teraz niech mi ktoś wytłumaczy, dlaczego z przesyłem danych w ten sposób:
"BluetoothActivity"
private class ConnectedThread extends Thread {
private final InputStream mmInStream;
public ConnectedThread() {
InputStream tmpIn = null;
// Get the input and output streams, using temp objects because
// member streams are final
try {
tmpIn = btSocket.getInputStream();
} catch (IOException e) { }
mmInStream = tmpIn;
}
public void run() {
byte[] buffer = new byte[256]; // buffer store for the stream
int bytes; // bytes returned from read()
// Keep listening to the InputStream until an exception occurs
while (true) {
try {
// Read from the InputStream
bytes = mmInStream.read(buffer);
mHandler.obtainMessage(RECIEVE_MESSAGE, bytes, -1, buffer).sendToTarget();
} catch (IOException e) {
break;
}
}
}
}
"MainActivity"
private final Handler mHandler = new Handler() {
public void handleMessage(android.os.Message msg) {
switch (msg.what) {
case cBluetooth.RECIEVE_MESSAGE:
byte[] readBuf = (byte[]) msg.obj;
String strIncom = new String(readBuf, 0, msg.arg1);
sb.append(strIncom);
int endOfLineIndex = sb.indexOf("\r\n");
if (endOfLineIndex > 0) {
String sbprint = sb.substring(0, endOfLineIndex);
sb.delete(0, sb.length());
textView1.setText(sbprint);
}
break;
}
};
-> niezależnie od częstotliwości przesyłu (nie mam tu na myśli BaudRate, a "delay(xxx)" dawany w pętli Loop w mikrokontrolerze, bo Baudrate jest stały i wynosi 9600BPS", a delay zmieniałem (od 200 ms - 2000 ms) i zawsze niezależnie od tego na około 1000 takich wysłanych wiadomości poprawnie przechodziło +/- 500.
A już z tym kodem:
"BluetoothActivity"
private class ConnectedThread extends Thread {
private final InputStream mmInStream;
public ConnectedThread() {
InputStream tmpIn = null;
// Get the input and output streams, using temp objects because
// member streams are final
try {
tmpIn = btSocket.getInputStream();
} catch (IOException e) { }
mmInStream = tmpIn;
}
public void run() {
byte[] buffer = new byte[1024];
int bytes;
StringBuilder readMessage = new StringBuilder();
// Keep listening to the InputStream until an exception occurs
while (true) {
try {
// Read from the InputStream
bytes = mmInStream.read(buffer);
String read = new String (buffer,0,bytes);
readMessage.append(read);
if(read.contains("\n")) {
mHandler.obtainMessage(RECIEVE_MESSAGE, bytes, -1, readMessage.toString()).sendToTarget();
readMessage.setLength(0);
}
} catch (IOException e) {
break;
}
}
}
}
"MainActivity"
private final Handler mHandler = new Handler() {
public void handleMessage(android.os.Message msg) {
switch (msg.what) {
case cBluetooth.RECIEVE_MESSAGE:
String readMessage= (String) msg.obj
textView1.setText(readMessage);
break;
}
};
Niezależnie od długości wiadomości, wiadomość zawsze jest odbierana poprawnie.