Because I had some problems to get it working I will show you how to connect a BTM222 bluetooth module (or any similar device) to an Android mobile phone.
1. What you need
- obviously a Bluetooth Module
- something to power the module and communicate with it (I use Arduino)
- a level-shifter (I use the 74HC4050N but it can be done with 2 resistors)
- an Android mobile phone
2. Setting up the Bluetooth module
At first have a look at the Datasheet.
To use the BTM222 you need to hook up 11 pins. 6 of them should have the GND signal. It is very important to tie every single one of the GND-pads to the GND-signal because in the mysterious world of high frequency applications devices don’t work when they are not properly grounded.
Also remember that the BTM222 needs a supply voltage of 3.3 V when hooking up the module.
Of the three remaining pads of the module there are two used for UART communication and one for the antenna. If you check the antenna against GND with a circuit indicator don’t wonder if there is a connection – but it works.
I found it very hard to solder wires to the BTM222. The pads of the devices are very small and you need a calm hand to get it done. I don’t have a calm hand. Thats why my device looks like this:
In my case I have an Arduino which I want to connect to the bluetooth module. So this is the schematic for my setup. Please remember that you can use a voltage divider instead of the 74HC4050N to get the conversion between 5 V and 3.3V for the UART done.
This is how my wiring looks like:
3. Use the UART to connect to the module
Now its time to communicate with the BTM222.
Make sure you Arduino does not use the UART interface (you can burn the „Blink“ demo on the device or just remove the chip of the board).
Now connect your PC to the Arduino Board. Use 19200 Baud, 8 data bits, no flow control, no parity and 1 stop bit for the connection.
Since I am running Ubuntu it is very simple to initiate a connection.
screen /dev/ttyUSB0 19200
You can also use your Arduino IDE serial console window. When you do that make sure to send a carriage return after each command.
If the connection was established successfully the BTM222 will echo all chars you send to it.
Send „ATI1“ to get some information of the device.
4. Connecting the module to the Android device
At first you need to pair the mobile phone with the BTM222. Just activate your bluetooth and search for devices. The default name of the module ist „Serial Adapter“.
If you don’t find it check your wiring. Don’t forget the antenna or to set all grounds.
Use the pin „1234“ for pairing.
Now it is time to write the Android App which communicates with the devices.
Google created a very nice example application called „Bluetooth Chat„.
You will find a download of the Eclipse project at the bottom of the article if you do not want to search for the above mentioned code and create a project by yourself.
So I started an eclipse android project with the „Bluetooth Chat“ demo app code by Google.
The application can list bluetooth devices, connect to them, run a server socket and send and recieve strings.
The problem ist that it will only work for other mobiles devices but not for the BTM222 which only supports the bluetooth SPP profile.
There is only one line of code to change to get a communication between your mobile and the BTM 222 working. Just replace the UUID definition in the file „BluetoothChatService.java“ in line 49 with the UUID „00001101-0000-1000-8000-00805F9B34FB„.
So it looks like this in the end:
// Unique UUID for this application
private static final UUID MY_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34F");
Thats it. Now you should be able to connect your BTM222 via the menu and send messages. To do it, execute the App, press the Menu button and then click „Connect a device“. The rest is self explaining.
To come to the end here you find the download of the eclipse project:
BluetoothChat (100 kb .zip)
An apk is included if you do not want to compile the project again.