Recevoir des données à partir d'Arduino bluetooth de l'appareil

J'ai suivi cet exemple: http://english.cxem.net/arduino/arduino5.php

J'ai un arduino uno conseil d'administration mis en place avec un sparkfun périphérique bluetooth. Je peux me connecter et d'envoyer des données à partir de android à l'arduino et je le vois de données de pop up dans la série du moniteur, mais je ne peux pas envoyer des données à partir de l'arduino(moniteur de série) et le retour à Android.

Je suis à l'aide d'un ConnectThread qui est commencé dans la méthode onResume dans l'activité. C'est le code de mon thread:

private class ConnectedThread extends Thread {
private final BluetoothSocket mmSocket;
private final InputStream mmInStream;
private final OutputStream mmOutStream;
public ConnectedThread(BluetoothSocket socket) {
Log.d("THREAD", "constructor");
mmSocket = socket;
InputStream tmpIn = null;
OutputStream tmpOut = null;
//Get the input and output streams, using temp objects because
//member streams are final
try {
tmpIn = socket.getInputStream();
tmpOut = socket.getOutputStream();
} catch (IOException e) { }
mmInStream = tmpIn;
mmOutStream = tmpOut;
}
public void run() {
Log.d("THREAD", "inside 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) {
Log.d("in loop", "waiting for data");
try {
//Read from the InputStream
bytes = mmInStream.read(buffer);        //Get number of bytes and message in "buffer"
h.obtainMessage(RECIEVE_MESSAGE, bytes, -1, buffer).sendToTarget();     //Send to message queue Handler
Log.d("recieve", "b " + bytes);
} catch (IOException e) {
break;
}
}
}
/* Call this from the main activity to send data to the remote device */
public void write(String message) {
Log.d("TAG", "...Data to send: " + message + "...");
byte[] msgBuffer = message.getBytes();
try {
mmOutStream.write(msgBuffer);
} catch (IOException e) {
Log.d("TAG", "...Error data send: " + e.getMessage() + "...");     
}
}
/* Call this from the main activity to shutdown the connection */
public void cancel() {
try {
mmSocket.close();
} catch (IOException e) { }
}
}

Ai-je besoin d'utiliser un service qui attend toujours les données depuis le moniteur de série se contente d'envoyer les données lorsque j'appuie sur envoyer?

EDIT: code Arduino:

    #include <SoftwareSerial.h>  
int bluetoothTx = 2;  //TX-O pin of bluetooth mate, Arduino D2
int bluetoothRx = 3;  //RX-I pin of bluetooth mate, Arduino D3
SoftwareSerial bluetooth(bluetoothTx, bluetoothRx);
void setup()
{
Serial.begin(9600);  //Begin the serial monitor at 9600bps
bluetooth.begin(115200);  //The Bluetooth Mate defaults to 115200bps
bluetooth.print("$$$");  //Enter command mode
delay(100);  //Short delay, wait for the Mate to send back CMD
bluetooth.println("U,9600,N");  //Temporarily Change the baudrate to 9600, no parity
//115200 can be too fast at times for NewSoftSerial to relay the data reliably
bluetooth.begin(9600);  //Start bluetooth serial at 9600
}
void loop()
{
if(bluetooth.available())  //If the bluetooth sent any characters
{
//Send any characters the bluetooth prints to the serial monitor
Serial.print((char)bluetooth.read());  
}
if(Serial.available())  //If stuff was typed in the serial monitor
{
char c = (char) Serial.read();
//Send any characters the Serial monitor prints to the bluetooth
bluetooth.print(c); 
}
//and loop forever and ever!
}
  • Salut oivindth, j'ai aussi été confronté au même problème que le vôtre et j'arrive aussi à se référer à la même source que vous avez. Vous avez mentionné que vous résolu ce problème à l'aide d'un Looper.préparer dans le fil, pourriez-vous expliquer un peu plus, ou elle serait génial si vous pouviez mettre à jour votre solution sur les codes ci-dessus. Merci un tas.
  • Salut @NicholasTJ. J'ai simplement ajouté un Looper.prepare(); dans ma méthode Run (). Si cette doesen " l'aide que je peux mettre à jour le post avec le code.
  • Je l'ai eu en cours d'exécution, mais pas avec un Looper.prepare(), eh bien, je pense que j'ai eu à le regarder dans Looper.prepare() et voir ce qu'il fait. Mais si vous êtes ok avec moi paresseuse, une mise à jour sur votre code dans votre post serait génial.
  • merci pour le lien.
InformationsquelleAutor oivindth | 2013-01-23