samedi 31 janvier 2015

Android Send Bluetooth Message Receives Space Character


Vote count:

0




I am making communication between an 2013 Nexus 7 with Android 4.4.4 and an Arduino Mega 2560 with an HC-06 bluetooth unit. I have established a connection from the Nexus 7 to the HC-06 and have gotton data to the Nexus from the Arduino but I am unable to transfer data from the Nexus 7 to the Arduino. I get no errors when I transfer data in Android, but the Arduino receives a space instead of the character I sent.


Android write method:



public void write(String message) {
Log.d(TAG, "...Data to send: " + message + "...");
byte[] msgBuffer = message.getBytes();
try {
mmOutStream.write(msgBuffer);
} catch (IOException e) {
Log.e(TAG, "...Error data send: " + e.getMessage() + "...");
}
}


Sends data:



public static void sendInfo(String info) {
boolean isErr = false;
if (MainActivity.isConnected) {
Log.d(TAG, "In sentInfo()");
//Log.d(TAG, "...Data to send: " + info + "...");
//TO(done)DO: send info to bluetooth device (Arduino)
byte[] msg = info.getBytes();


try {
mConnectedThread.write(info);
} catch (NullPointerException n) {
isErr = true;
Log.e("Error", "Failed to send message \"" + Arrays.toString(msg) + "\" with error \"" + n.getMessage() + ".\"");
}

if (!isErr) {
Log.i("sentInfo", "In sentInfo(), this was sent: " + Arrays.toString(msg));
//noinspection ALL
isErr = false;
}
} else {
Toast.makeText(MainActivity.mThis, "Please connect the Arduino", Toast.LENGTH_SHORT).show();
}
}


Arduino receive code:



int setMsg() {
if (Serial1.available() > 0) {
chars = Serial1.available();
incomingByte = Serial1.read();
putstring("You've Got Mail! Characters: ");
Serial.print(chars);
putstring(" Message: '");
Serial.print(incomingByte);
putstring_nl("'.");
if (incomingByte == 49) {
return 1;
flashLED(500);
Serial.println(msg);
} else if (incomingByte == 50) {
return 2;
flashLED(500);
Serial.println(msg);
} else if (incomingByte == 51) {
return 3;
flashLED(500);
Serial.println(msg);
} else if (incomingByte == 52) {
return 4;
flashLED(500);
Serial.println(msg);
} else if (incomingByte == 53) {
return 5;
flashLED(500);
Serial.println(msg);
} else if (incomingByte == 100) {
return 6;
flashLED(500);
Serial.println(msg);
} else {
Serial.println(incomingByte);
digitalWrite(ledPin, HIGH);
delay(3250);
digitalWrite(ledPin, LOW);
delay(20);
}
}
}


Please explain why the Arduino's Serial always prints You've Got Mail! Characters: 1 Message: ' '. when the Nexus 7 sends any message (I only send numbers in quotes to the sendInfo() function) to the HC-06.



asked 1 min ago







Android Send Bluetooth Message Receives Space Character

Aucun commentaire:

Enregistrer un commentaire