简体   繁体   English

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

[英]Bluetooth communication between Android and Arduino using Processing

I am trying to make two-way bluetooth communication between Android and Arduino using Processing for Android. 我正在尝试使用Android的Processing在Android和Arduino之间进行双向蓝牙通信。 I have success transferring data from the Android to the Arduino with serial.begin(9600). 我已成功使用serial.begin(9600)将数据从Android传输到Arduino。 And I have success transferring data from the Arduino to the Android by using SoftwareSerial in the Arduino program and bluetooth.begin(9600) in place of serial.begin(9600). 通过在Arduino程序中使用SoftwareSerial并使用serial.begin(9600)代替bluetooth.begin(9600),我成功地将数据从Arduino传输到Android。

However, when trying to transfer data from the Android to the Arduino using bluetooth.x commands, it does not work. 但是,当尝试使用bluetooth.x命令将数据从Android传输到Arduino时,它不起作用。 Here is the Arduino code: 这是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;
    }
  }

The on() and off() functions switch on and off an LED on the Arduino. on()和off()函数可打开和关闭Arduino上的LED。 As mentioned, this works when I'm using serial.x commands and not bluetooth.x commands. 如前所述,这在我使用serial.x命令而不是bluetooth.x命令时有效。 Also, I am using Ketai for Processing for Android. 另外,我正在使用Ketai进行Android处理。 I am using Processing 2.0.1, Arduino 1.0.5, Android 2.3.6. 我正在使用Processing 2.0.1,Arduino 1.0.5,Android 2.3.6。

Here is the relevant beginning code: 这是相关的开始代码:

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

A little more code would be greatly appreciated... 多一点代码将不胜感激...

Have you included something like that? 你有没有包括这样的东西?

#include <SoftwareSerial.h>

int bluetoothTx = 2;
int bluetoothRx = 3;

SoftwareSerial bluetooth(bluetoothTx, bluetoothRx);

EDIT: 编辑:

That's similar to what I use. 这与我使用的类似。 You first upload the code without the bluetooth wired and then wire the bluetooth. 您首先上传未连接蓝牙的代码,然后再连接蓝牙。 Then you can simply use Serial.doSomething() because you are using the same pins, you don't need #include <SoftwareSerial.h> . 然后,您可以简单地使用Serial.doSomething()因为您使用的是相同的引脚,因此不需要#include <SoftwareSerial.h> But you need to make sure that the baudrate is the same. 但是您需要确保波特率相同。

You can try this code to make sure it works fine: 您可以尝试以下代码以确保其正常工作:

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);

}

You should also make sure you you Arduino is connected to the computer via bluetooth. 您还应该确保您已将Arduino通过蓝牙连接到计算机。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM