Wednesday, 1 August 2012

A Bluetooth Server on Android: part 1

This is the first of 2 posts on "How to make your Android Phone, listen for remote request with bluetooth".
Bluetooth is a mature tecnology used everywhere.
For my experience, it is not an easy tecnology to manage, if you want to simply share something you have to follow many steps.
If you are a developer and you want to simply create a connection and pass data between 2 devices (whatever the devices are), you have to create a lot of code and first of all, learn how this tecnology works.
As a reference platform in this post I will use Android; things can change from a platform to another but the key concept are the same.

Who are you? Be visible!


Bluetooth have differents modes:
  • visible: any other device around that start a bluetooth search, Inquiry, will find the device.
  • not visible: you can see other device around you, but none can see you in a search and establish a connection.
  • off: the chip is off.

So the first things you have to do, in a bluetooth server are:
  1. get a reference to your internal bluetooth chip
  2. set the chip on
  3. set chip in visible mode
The 2nd and 3rd step require the permission of the user.
On Android, whenever you try to make your bluetooth phone on or visible, a system dialog message, automatically advise the user of the action.

The 3rd step is available only for a limited amout of time (you can specify how many seconds)... I think this is just for security and autonomy reason.

What do you offer? A service
Be visible is not enough to establish a connection with another device.
You have to specify the services that you are offering.


A service is a way to declare what you are capable to offer in term of data, for another device.
It is what you expose to others.
Every phone have some default service created for sharing content like calendar, phonebook etc...
A service is identified by a name and a UUID: http://en.wikipedia.org/wiki/Universally_unique_identifier.

How do we talk? With a profile
A profile is a low level bluetooth protocol that explain which type of message and communication you are trying to establish.
Bluetooth is used in different way so you have different profile specific for a task: there is A2DP for bluetooth audio communication (used by bluetooth earphone), Obex for file transfers, L2Cap for exchange information, RfComm is a sort of serial communication over bluetooth and others profiles.
When you have to make 2 device talk eachother with your own protocol, the options are usually use L2Cap or RfComm and built your protocol on top of them.
I have decided to use RfComm.

Summary for a bluetooth server:
- Turn the bluetooth chip on
- Be visible to others
- Create a Service
- Listen for incoming connection

Next:
The android code will be explained and available in the next post on the blog.

No comments:

Post a Comment