我正在使用 Android 4.4.4 的 2013 Nexus 7 和使用 HC-06 蓝牙装置的 Arduino Mega 2560 之间进行通信。我已经建立了从 Nexus 7 到 HC-06 的连接,并且已从 Arduino 将数据传输到 Nexus,但我无法将数据从 Nexus 7 传输到 Arduino。我在 Android 中传输数据时没有收到错误,但 Arduino 收到的是空格而不是我发送的字符。
Android写入方法:
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() + "...");
}
}
发送数据:
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)
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接收代码:
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);
return 0;
}
}
}
请解释为什么 Arduino 的 Serial
总是打印
您收到邮件了!字符:1 条消息:' '。
当 Nexus 7 向 HC-06 发送任何消息(我只将引号中的数字发送到 sendInfo()
函数)时。
最佳答案
- 您将传入字节设置为 int 是什么类型?
编辑回应您的评论:
incomingByte needs to be an int .
将您的读取代码更改为:
void setup() {
Serial.begin(9600);
Serial1.begin(9600);
}
void loop() {
// read from port 0, send to port 1:
if (Serial.available()) {
int inByte = Serial.read();
Serial1.print(inByte, BYTE);
}
// read from port 1, send to port 0:
if (Serial1.available()) {
int inByte = Serial1.read();
Serial.print(inByte, BYTE);
}
}
(I only send numbers in quotes to the sendInfo() function) to the HC-06.
您不应发送带引号的数字,而仅发送一个数字。这可能是你的问题,因为它正在读取引号。
此外,最好使用 switch 而不是所有这些 if
此外,
sendInfo(String info) {
中的这个byte[] msg = info.getBytes();
是不必要的行,据我所知,您没有使用它,您在write(string)方法中将字符串解析为字节数组
而不是Log.i("sentInfo", "在sentInfo()中,发送的是:"+ Arrays.toString(msg));
你可以有 Log.i("sentInfo", "In sentInfo(), this was sent: "+ info);
并且每个 if 中 return 后的所有内容都不会被执行
return 1;
flashLED(500);
Serial.println(msg);
您想要在每个 if 语句中执行的任何内容都需要放在 return 语句之前,return 将使您的函数完成并返回到调用函数。
关于java - Android发送蓝牙消息接收空格字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28258624/