[Tutorial] How to connect an Android device with Arduino and bluetooth
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.
|Get updates||Follow @aron-bordin|
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.
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.
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:
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:
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.
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”).
To connect with the Arduino, add the command bellow:
Now, to read a message, run the command:
This will get the last message received.
To understand how it works, take a look in the complete source(using Processing):
This is the result:
Thanks for reading, take a look in the blog, if have any question, suggestion, … or …., just comment here.
That’s all folks,