我正在嘗試使用Android處理爲Android和Arduino之間進行雙向藍牙通信。使用serial.begin(9600),我已成功將數據從Android傳輸到Arduino。通過使用Arduino程序中的SoftwareSerial和bluetooth.begin(9600)代替serial.begin(9600),我成功地將數據從Arduino傳輸到Android。Android和Arduino之間使用處理的藍牙通信
但是,當試圖使用bluetooth.x命令將數據從Android傳輸到Arduino時,它不起作用。這裏是Arduino代碼:
if (bluetooth.available()) // Wait until a character is received
{
char val = (char)bluetooth.read();
//Serial.println(val);
switch(val) // Perform an action depending on the command
{
case 'w'://turn the light on when a 'w' is received
on();
break;
case 'q'://turn the light off when a 'q' is received
off();
break;
//default://otherwise remain in the previous state
//idle();
break;
}
}
on()和off()函數打開和關閉Arduino上的LED。如前所述,這在我使用serial.x命令而不是bluetooth.x命令時有效。另外,我正在使用Ketai for Android進行處理。我正在使用處理2.0.1,Arduino 1.0.5,Android 2.3.6。
下面是相關的代碼開始:
#include <SoftwareSerial.h>
SoftwareSerial bluetooth(0,1); //TX 0, RX 1
「Arduino 2.0.5」?你是某種時間旅行者嗎? ;) – ladislas