Android和Arduino之间使用Processing进行蓝牙通信

时间:2013-10-06 23:36:48

标签: android bluetooth arduino processing serial-communication

我正在尝试使用Processing for Android在Android和Arduino之间进行双向蓝牙通信。我已成功通过serial.begin(9600)将数据从Android传输到Arduino。我通过Arduino程序中的SoftwareSerial和bluetooth.begin(9600)代替serial.begin(9600)成功地将数据从Arduino传输到Android。

然而,当尝试使用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处理。我正在使用Processing 2.0.1,Arduino 1.0.5,Android 2.3.6。

以下是相关的开头代码:

#include <SoftwareSerial.h>
SoftwareSerial bluetooth(0,1);  //TX 0, RX 1

1 个答案:

答案 0 :(得分:2)

我们将非常感谢更多代码...

你有没有这样的东西?

#include <SoftwareSerial.h>

int bluetoothTx = 2;
int bluetoothRx = 3;

SoftwareSerial bluetooth(bluetoothTx, bluetoothRx);

修改

这与我使用的相似。 你首先上传没有蓝牙连接的代码,然后连接蓝牙。然后您只需使用Serial.doSomething(),因为您使用相同的引脚,您不需要#include <SoftwareSerial.h>。但是你需要确保波特率是相同的。

您可以尝试使用此代码以确保其正常工作:

void setup(){

    Serial.begin(9600); // or wathever your bluetooth module baudrate is

}

void loop(){

    Serial.println("Hello World!"); // to make sure it works.
    delay(500);

}

您还应该确保Arduino通过蓝牙连接到计算机。