我正在開發一個涉及Android Uno和Android手機之間通信的項目。手機發送一次收到的請求信號「*」,Arduino發送一個循環中的隨機整數。目前,Android設備正在接收消息,但它顯示爲盒裝問號,並未收到所有消息。有任何想法嗎?非常感謝!從Arduino收到的消息顯示爲盒裝問號。
的Arduino代碼:
#include <SoftwareSerial.h>
const int RX_PIN = 0;
const int TX_PIN = 1;
SoftwareSerial bluetooth(RX_PIN, TX_PIN);
char commandChar;
void setup(){
bluetooth.begin (9600);
Serial.begin(38400);
}
void loop() {
if(bluetooth.available()){
commandChar = bluetooth.read();
switch(commandChar){
case '*':
Serial.println("Got the request code");
for(int i = 0; i < 10; i++){
bluetooth.print(random(21));
}
break;
}
}
}
的Android代碼:
public void run() {
initializeConnection();
byte[] buffer = new byte[256];
int bytes;
// Keep looping to listen for received messages
while (true) {
try {
bytes = mmInStream.read(buffer);//read bytes from input buffer
String readMessage = new String(buffer, 0, bytes);
Log.e("Received Message: ", readMessage);
} catch (IOException e) {
break;
}
}
}
public void initializeConnection() {
try {
PrintWriter out;
out = new PrintWriter(mmOutStream, true);
out.println("*");
out.flush();
}catch (NullPointerException NPE) {
}
}
控制檯輸出:
08-13 19:02:46.546 4019-4128/? E/Received Message:: �
08-13 19:02:46.596 4019-4128/? E/Received Message:: ����
藍牙是挑剔的。你可以包括你使用的藍牙模塊的名稱嗎? –
是的。它是HC-05藍牙模塊。該品牌是DSD TECH。 –