Communication between Android and Arduino with Bluetooth(2)
Preface
First, if you don’t kown what is ADK(Android Open Accessory Development Kit), please find the answer here. From now on, we provided to you Freaduino ADK (A new board has just been made by us this week, which is a Android Open Accessory Development Kit compatible board, but not a simple clone of it.)
To make the communication between Android and Arduino easy, we would like show you a new way that Android interact with Arduino and other similar boards by Bluetooth. From here, you will see how to connect Android and Arduino with Bluetooth Bee. You will find it’s not difficult any more after reading this post. There also is a post about the APP we made last week, which will be used in the following. If you want to know the details of this Android’s APP, please read this post: Android+BlueTooth(Part1).
Preparation
Before we getting start, there are some modules must be prepared. As the figure1,of course an Android phone and Arduino main board, then a Bluetooth Bee Module and a XBee Shield which compatible with Bluetooth Bee. It’s pretty good if you own them, otherwise you can get them from our store in a low cost: Bluetooth Bee($23), XBee Shield($15), Arduino main board.
From now on, there is more easy module instead of XBee Shield is Bluetooth Shield($23), which has integrated of Bluetooth module.
Then about the Software, you are able to get the APP from here.
Figure 1
How to Communication
The XBee shield provides 3 optional hardware interface, which determine how the shield’s serial communication connects to the serial communication between microcontroller (ATmega8 or ATmega168) and FTDI USB-to-serial chip on the Arduino board. So it is necessary to explain the XBee shield work mode first.
The XBee shield has 4 row jumpers figure2. DIN corresponds with Bluetooth Bee’s RX, DOUT corresponds with Bluetooth Bee’s TX, TX_H corresponds with Arduino’s TX(D0), RX_H corresponds with Arduino’s RX(D1), D2 corresponds with Arduino’s D2, D3 corresponds with Arduino’s D3. We select work mode by DIN and DOUT freedom match with TX_H, TX_H, D2, D3. 
Figure 2
Bluetooth Bee direct use Arduino main board’s UART(TX/RX) connect to PC. However this method must remove the MCU chip, and the Bluetooth Bee inserted on Arduino’s main board with XBee shield. This means the Bluetooth Bee direct connect to PC Serial port. Setting the XBee shield’s jumper connect as below:
DIN to RX_H , DOUT to TX_H.
Figure 3
Previous work done, power on the Arduino board and the Bluetooth Bee’s LED1 will blink, and the PC will find a new serial port hardware, and we are COM6 here.
Figure 4
So, we can use a serial port tool to control Bluetooth Bee module, user can send the AT command to the module to set the control parameters and send control order. Bluetooth Bee is base on HC-05/HC-06. HC-05 has three work roles (Master, Slave and Loopback) at the automatic connection work mode. HC-06 only has Slave mode.
Note: In this post, Bluetooth Bee means HC-05, because there is more flexible control and all HC-06′s feature in HC-05.
Bluetooth Bee default mode is AT module, the baud rate of which is 38400. If you want to get AT module, you should set work mode. After power on then input the Bluetooth Bee’s pin20 to high level, then the module will enter to AT module. At this time, the baud rate is 38400. You can get the AT command from here.
About the tool to choose the serial port, SecureCRT is a nice choice. However AT command should end up with “\r\n”. It means when you finish programming, you should add terminator (“ENTER” or “0x0d 0x0a”) to the program. So you need setting SecureCRT end up ”\r\n”, there are some other choices, we recommend sscom32E.exe which provided by us and automatically added ”0x0a 0x0d” in end of command. No matter what you are going to use the serial port tool, please remember command end up “0x0a 0x0d”.
Bluetooth Bee Master mode, it means the module will active search for remote Bluetooth devices, so the Android APP will be Server. The Slave mode is the mode just be discovered by other Bluetooth devices and be connected, so the Android APP will be Client. OK, the specific steps they are connected as below:
step 1: Install and Choose the Bluetooth APP on the Android phone
Figure 5
step 2: Click the “Bluetooth Bee” App

Figure 6
step 3: Click the “Open Bluetooth” enable Bluetooth to make it discoverable. There a dialog box will appear and request user permission to enable Bluetooth. Select “Yes” and the system will enable Bluetooth and focus will return to your application once the process completes (or fails). “Yes” returns RESULT_OK and “No” returns RESULT_CANCELED.

Figure 7
step 4: Click “AS Server”, will wait for other Bluetooth devices pair request.

Figure 8
step 5: Android side gets all ready. Now we start setting Bluetooth Bee. Because the App is Server mode, the Bluetooth Bee should be selected Master mode. Connect Bluetooth Bee’s pin20 to hight level, and then send “AT” to the module by serial port tool which baud rate is 38400, data bit is 8, stop bit is 0, check parity is 0. Bluetooth Bee accept “AT” will return OK, it will show you have entered the AT mode. You can send the various command for query or setting. As figure 9.

Figure 9
step 6: After entered AT mode, select Master mode by setting as below:
at+role=1\r\n //Setting Master mode
return OK
at+init\r\n //Initialize the SPP profile lib( can’t repeat initialization)
return OK
at+iac=9e8b33\r\n //Inquire Bluetooth device has an access code
return OK
at+inqm=1,9,48\r\n //Inquire mode: 1) has the RSSI signal intensity indication, 2) stop inquiring if more than 9 Bluetooth devices response, 3) limited time in inquiring is 48*1.28=61.44s.
return OK
At+inq\r\n //inquire the Bluetooth devices around
return :
+INQ:2:72:D2224,3E0104,FFBC
+INQ:1234:56:0,1F1F,FFC1
+INQ:F4FC:32:4F4982,58020C, 7FFF
…

Figure 10
//Make pair with the remote Bluetooth device( address:F4FC,32,4F4982), the limited time is 20s. Default pair password is 1234.
At+pair=F4FC,32,4F4982,20\r\n

Figure 11
return OK
at+fsad=F4FC,32,4F4982\r\n \\To check whether the Bluetooth device (address: F4FC,32,4F4982) is in the pair list.
return OK
at+link=F4FC,32,4F4982\r\n \\The Bluetooth device (address: 12:34:56:ab:cd:ef) is in the pair list. The connection can be built directly without inquiring.
return OK
If it returns OK, Congratulation!! We have linked Android Blutooth and Bluetooth Bee, the next step of course send some data for test. We can send data by serial port tool, as figure 12. I think there are really so many things you can do by the Bluetooth Bee module communicate with Android App.

Figrue 12
However, it’s just Single-income-single, do you want a Full-duplex communication? If “Yes”, we will say: OK, it’s no problem! We have made the demo through Android App “As Client” and Bluetooth Bee as Slave mode to realize it.
Step 1: First, you should disconnect the link Android and Bluetooth Bee, So you need release Bluetooth Bee pin20 to low and then input to AT mode again, and then send ‘at+disc\r\n’, return OK. The other way, you can cut off the power
. Now, we should set Bluetooth Bee to Slave mode, so send ‘ad+role=0\r\n’, return OK. Reset the power, the module is Slave mode. This mode just could be connectted.
To communicate with each other, you don’t have to insert to AT mode again, just normal mode you can accept and send data by serial port tool.
Step 2: Click “Open Bluetooth” and then Click “AS Clent”, it will search the remote devices and return the device’s address.

Figure 13
Step 3: Click one address to pair and connect, if connection succeed it will return to the menu as Figure 14

Figure 14
Step 4: We can send and accept data to each other. The App we have modified to Full-duplex communication, could send and receive data simultaneously. In the end of the post you can get the full code.

Figure 15
Second way, Oh my god, it is too long and too boring?Just relax and take a break.
The second way, setting the XBee shield’s jumper connect:
DIN to TX_H , DOUT to RX_H.
Note that the RX and TX pins of the microcontroller are still connected to the TX and RX pins (respectively) of the FTDI chip – data sent from the microcontroller will be transmitted to the computer via USB as well as being sent wirelessly by the Xbee module. The microcontroller, however, will only be able to receive data from the Xbee module, not over USB from the computer.
So this way just for you has perfect code and don’t need monitor from serial port, because you cann’t see anything from serial port tool.

Figure 16
Third way, use Software Serial library to define D2 and D3 as a virtual serial port TX and RX. By this way, you can use Arduino’s Serial Monitor to Monitor operation of the code.
Setting the XBee shield’s jumper connect:
DIN to D3, DOUT to D2.
And you can change the define in the code. There are also some character conversion issues, maybe you can solve it. If you do it, please share with us.

Figure 17
/*********************************************************************
** Description: **
** This file is a sample code for your reference. **
** **
** Copyright (C) 2011 ElecFreaks Corp. **
** Created by ElecFreaks Robi.W /10 June 2011 **
** **
** http://www.elecfreaks.com **
*********************************************************************/
#include <SoftwareSerial.h>
#define rxPin 2
#define txPin 3
SoftwareSerial mySerial = SoftwareSerial(rxPin, txPin);
void setup()
{
// define pin modes for tx, rx, led pins:
pinMode(rxPin, INPUT);
pinMode(txPin, OUTPUT);
mySerial.begin(38400);
Serial.begin(9600);
}
void loop()
{
// listen for new serial coming in:
char someChar = mySerial.read();
// print out the character:
mySerial.print(someChar);
// print to Serial Monitor
Serial.print(someChar);
}

Conclusion:




Sweet blog! I found it while searching on Yahoo News. Do you have any tips on how to get listed in Yahoo News? I’ve been trying for a while but I never seem to get there! Thank you
[Reply]
ElecFreaks Reply:
July 8th, 2011 at 3:02 pm
Sorry, I can not help you. Because I do not kown the reason, too. We did not do anything special, it just there as you saw. The only thing we did is hard working and keep our posts helpful for people.
[Reply]
Excellent blog!! You can start a lot more. I love all of the data provided. I am going to stay tuned Top-notch statement. Continue to keep in the helpful work.
[Reply]
Many thanks a lot regarding supplying individuals with this kind of wonderful possiblity to examine tips from this blog. It’s usually very perfect and also stuffed with an enjoyable experience to me as well as my office friends to talk to your internet site especially 3 x weekly to examine the new items you have. And finally, we’re undoubtedly satisfied regarding the terrific information you give. Selected a couple of tips about these pages are really the best I’ve ever had.
[Reply]
dude, you’ve done a interesting article here, and all the replies above were all bots
) I take it you don’t have experience on blogging, and, more than that, you answered to a bot 
Anyway, bacvk on topic, I AM interested in microcontrollers and stuff. Right now I want to learn C/MikroC for AVR and do some stuff with ATTiny and maybe atmega. Also, I would like to do some cross applications between Android and AVR, maybe arduino….
[Reply]
robi Reply:
August 4th, 2011 at 10:23 pm
Keep your interest !
I think Freaduino ADK is a nice choose for communication between Android and Arduino. You could reference the “Control LED with Android and Freaduino ADK” post from:
http://www.elecfreaks.com/1109.html
[Reply]
When sending the at+inq command i get error(16). Could you tell me the possible causes?
[Reply]
robi Reply:
December 6th, 2011 at 3:15 pm
Hi Divish,
From the HC-05 datasheet page22, we know that error(16) means ‘ SPP lib hasn’t been initialized ‘.
So maybe you can try ‘at + init’ firstly to initialize the SPP profile lib, and then try ‘at + inq’.
[Reply]
Divish Reply:
December 6th, 2011 at 3:34 pm
I did same result. somewhat +init is not getting the job done.
[Reply]
robi Reply:
December 6th, 2011 at 4:43 pm
Hi Divish,
Is there return ‘OK’ when you set ‘at+init’ ?
One other side, if you want to use at+inq to inquire the Bluetooth device around, you need set the Module role to Master, such as ‘at + role=1′ and return ‘OK’
You can use ‘at+role’ to inquire module role.
[Reply]
Divish Reply:
December 6th, 2011 at 10:13 pm
I got the connection made by setting the bee to slave mode and using my pc to connect to it as at serial bluetooth device.
Master role INQ still not fixed though. can it be done in work-response mode? they will not be AT commands will they?
[Reply]
robi Reply:
December 7th, 2011 at 12:00 am
Yes, All AT Command must be send at order-response mode.
I’m not sure the reason your result. Usually if you return error(16) that means SPP lib hasn’t been initialized.
Can you send me your AT command return such as :
‘at+role’
‘at+init’
‘at+iac=9e8b33′
‘at+class=0′
‘at+inqm=1,9,48′
‘at+inq’
You can sent the result and picture to my email: robi.wang@elecfreaks.com
[Reply]
I am trying to this device with an Android 2.3 phone. The software you provided in the zip file is not being accepted by my phone even when the extensions is changed using Astro. I am wondering what version of Android that is written for. Anyway, the phone is not recognizing the HC-05 at all. There is no attempt to even find the device. However, I find the device and connect to my PC which has a BT USB modem on it.
One question I had is how the host should be setup with Android. Does Android usually take the role of master? Can I use the normal Android sync to see if this connection is working?
[Reply]
robi Reply:
April 20th, 2012 at 2:12 pm
Hi Demolishun,
We have provide the source code of the apk. So you can compile it own , and then get new apk try on your android phone.
The apk is not version request , but I can not guarantee every android phone could install it. The best way is compile soure code by yourself with ecilpse.
I thought as loog as you android support Bluetooth , there are no problem pair with HC-05 Bluetooth Module.
[Reply]