[Tutorial] How to connect an Android device with Arduino and bluetooth

Welcome!!

In this post, I’m going to show you how to connect your Android device with an Arduino board.

To turn it easier, I’ll show you the .PNG Arduino Framework’s Bluetooth module.

At the end of this post, you will be able to send and receive text messages between Android and Arduino

About .PNG Arduino Framework

The .PNG Arduino Framework was developed by students of Unesp – São Paulo State University, Brazil, with the goal to turn Arduino project development easier. The framework has a chain of modular classes, compatible with some Arduino components.

Source code

Get updates Follow @aron-bordin
Star it: Star
Contribute: Fork
Download: Download

Android Bluetooth helper

On the .PNG Arduino folder, or repo, open the Helpers folder and get the BluetoothArduino.java. And copy it to your project folder(can be the same folder that your MainActivity)

Please, read the docs for more information.

Important!

Just some bluetooth modules are compatible with the Arduino code. (Take a look here to see tested modules)[http://png-arduino-framework.readthedocs.org/bluetooth.html#bluetooth]. The Java class can be used with any Bluetooth device.

Creating the Arduino Project

In this post I’m not going to show you the hardware stuff, like, how to connect the module with your Arduino, and this kind of stuff.

Because there is no difference, you can read any tutorial in the net, after it come back and you will be able to use it.

At the top of your source code, include these libs.

#include "SoftwareSerial.h"
#include "Bluetooth.h"

Now, we have a Bluetooth object, to make our connection, send and receive data.

To start using it, at the top of your source declare a public variable to access it:

Bluetooth *blue = new Bluetooth(2, 3);

With Bluetooth(RX_Pin, TX_Pin)

The default pin is 1234, name is “PNGFramework” and baudrate is 9600

You can see how to change these values on the documentation.

Now, on your Setup(), add the follow line:

void setup(){
  Serial.begin(9600);
  blue->setupBluetooth();
}

This code will configure everything in your bluetooth, you can check the monitor serial for logging.

And this is the loop, where we wait to receive messages. And send a message when we receive some data from Serial.

void loop(){
  String msg = blue->Read();
  if(msg.length() > 1){
    Serial.print("Received: ");
    Serial.println(msg);
  }
  if(Serial.available()){
    blue->Send("Example message#");
  }
}

Creating our Android Project

Just to make this tutorial simpler, I used Processing IDE for the Android. If you prefer, you can use your default Android IDE(Android Studio, Eclipse, …). The class that I provide is compatible with anyone.

Now, to use it, copy the BluetoothArduino.java in the same folder of your Android source code; and on the first line, update the package name.

Before connect your device, you need to be paired with the robot. Open your bluetooth settings on Android device, and pair. Remember, the default PIN is 1234. When paired, you can develop your project.

First, create a bluetooth object, use the following code, make sure to use the same RobotName that you used in the Arduino project. (default is “PNGFramework”).

BluetoothArduino mBlue = BluetoothArduino.getInstance("PNGFramework");

To connect with the Arduino, add the command bellow:

mBlue.Connect();

Now, to read a message, run the command:

String msg = mBlue.getLastMessage();

This will get the last message received.

To understand how it works, take a look in the complete source(using Processing):

/*make sure to use the same name of the robot.
And check if it's paired with your device */
BluetoothArduino mBlue = BluetoothArduino.getInstance("ExampleRobot");

void setup(){
  mBlue.Connect();  
}

void draw(){
  background(0);
  String msg = mBlue.getLastMessage();
  text(msg, 100, 100);  
 
}

This is the result:

That’s it.

Thanks for reading, take a look in the blog, if have any question, suggestion, … or …., just comment here.

That’s all folks,

Aron Bordin

Aron Bordin
Computer Science Student and AI researcher. Always coding something fun :)

[Tutorial] Developing Android Background Services

### Welcome!In this post, I'll show you how to develop background services on Android Studio. We'll see two type of services: `Service` a...… Continue reading