Ich versuche, eine App zu schreiben, die die Koordinaten eines Balls an Arduino über BT übergibt. Die Koordinaten werden alle 4 ms gesendet. Für diesen Test sende ich "123" anstelle von vollen Koordinaten. Was bekomme ich jetzt (auf Arduino seriellen Monitor) ist "123123123123123 ..." und es wird erst aktualisiert, nachdem ich die Anwendung geschlossen habe.Android sendet Informationen über Bluetooth, schnelle Protokollierung
Was ich erreichen möchte, ist "123" in jeder Zeile, die sofort nach dem Senden der Nachricht angezeigt wird.
Android Code BT:
private class ConnectThread extends Thread {
private final BluetoothSocket mmSocket;
private final BluetoothDevice mmDevice;
private OutputStream outStream ;
UUID uuid = UUID.fromString("00001101-0000-1000-8000-00805f9b34fb");
public ConnectThread(BluetoothDevice device) {
// Use a temporary object that is later assigned to mmSocket
// because mmSocket is final.
BluetoothSocket tmp = null;
mmDevice = device;
try {
// Get a BluetoothSocket to connect with the given BluetoothDevice.
// MY_UUID is the app's UUID string, also used in the server code.
tmp = device.createInsecureRfcommSocketToServiceRecord(uuid);
} catch (IOException e) {
Log.e(TAG, "Socket's create() method failed", e);
}
mmSocket = tmp;
}
public void run() {
// Cancel discovery because it otherwise slows down the connection.
mBluetoothAdapter.cancelDiscovery();
try {
// Connect to the remote device through the socket. This call blocks
// until it succeeds or throws an exception.
mmSocket.connect();
Log.i(TAG, "run: CONNECTED");
} catch (IOException connectException) {
Log.i(TAG, "run: NOT CONNECTED");
}
}
// Closes the client socket and causes the thread to finish.
public void cancel() {
try {
mmSocket.close();
if(outStream != null)
outStream.close();
finish();
} catch (IOException e) {
Log.e(TAG, "Could not close the client socket", e);
}
}
//Sending Message
public void writeData(String data){
String info = data;
try {
outStream = mmSocket.getOutputStream();
outStream.write(info.getBytes());
Log.i(TAG, "writeData: MSG SENT");
} catch (IOException e) {
e.printStackTrace();
Log.i(TAG, "run: CANT SEND MSG");
}
}
public boolean isConnected(){
return mmSocket.isConnected();
}
}
In meiner Hauptfunktion nenne ich:
if(connectThread.isConnected())
connectThread.writeData("123");
Arduino Code:
String incomingByte;
void setup() {
//pinMode(53, OUTPUT);
Serial.begin(9600);
}
void loop() {
// see if there's incoming serial data:
if (Serial.available() > 0) {
// read the oldest byte in the serial buffer:
incomingByte = Serial.readString();
Serial.println(incomingByte);
delay(10);
}
}
I glaube nicht, dass es das ist. Arduino puffert weiterhin meine Nachrichten als eine große Nachricht und aktualisiert den seriellen Monitor, nachdem ich die gesamte Verbindung geschlossen habe. – Endoplasma
Das liegt daran, dass Sie kein Zeilenendezeichen hinzufügen. Ist mir passiert Versuch es bevor ich sage es ist nicht ... – dda
Oh, ich sehe dich jetzt, ich werde es versuchen! – Endoplasma