Bluetooth is industrial standard for wireless communication.
Bluetooth specification defines how devices could communicate using radio waves at
2.4 GHz frequency. More details on the physical layers are outside of the scope of
this document. The purpose of STM is to explain Bluetooth to application developers.
Bluetooth is designed to enable devices to exchange data.
The specification could be split into three layers:
-
Transport layer made of radio, baseband and link managers.
-
Middleware layer made of high level protocols that are very important for application
developers – this layer defines protocols like RFCOMM, OBEX, SDP and others.
-
RFCOMM – could be considered as serial port communication over Bluetooth. This
protocol is the base of the most of the other middleware protocols.
-
SDP – this protocol allows applications to discover services and details about
the services.
-
OBEX – protocol that defines format and semantics of data exchanged
-
Application layer is made of highest level protocols organized into so called
Bluetooth Application Profiles. Application profiles define high level services exported
by a device – like File transfer protocol profile – defining all protocols and commands
used to exchange files between two devices using Bluetooth.
Applications
|
Application profiles
|
|
OBEX
|
AT Commands
|
RFCOMM protocol
|
Radio communications
|
So far we have enough theory, there are lost of other and
better resources about Bluetooth. Lets try to explain steps necessary to create you
first Bluetooth application.
For this example will create an application that will allow
you to manage files on your mobile. The best way to create this application is to
base it on the File transfer protocol profile.
On the top of our application layer is the FTP Bluetooth
profile. The profile defines commands like put and get – these commands
are packed as OBEX operations and are send over RFCOMM connection.
When your application starts it has to find the device.
Finding the device is related to the low level radio protocols – so it is up to the
operating system to do the work for us. Microsoft Windows provides powerful but hard
to use native API to scan for Bluetooth devices in range, so we prefer to use the
Bluelibs.
We have a nice class to scan for devices – BtScanner:
public BtDevice FindDevice()
{
BtScanner btScanner = new BtScanner();
btScanner.SearchTimeOut = 15;
RemoteDevice[] devices = btScanner.GetDeviceList();
….
|
Bluetooth scanning could take several minutes so it is
better to restrict it to 15 seconds only. For more details on using the scanning class,
please refer to the help files.
In the sample above we will use the first found device.
Once we have the device we can connect to:
-
RFCOMM
-
To OBEX
-
To any of the services exported by the device.
As we want to manage files we have to connect to FTP service
of your phone:
ClientConnection conn = FirstDevice.ConnectToService(
ServiceUUID.OBEXFileTransferServiceClass_UUID);
|
The last step for the example it to use OBEX commands.
Unfortunately OBEX commands are not that simple – but we are lucky enough to have
even complete handler for FTP protocol over OBEX – the OBEXFtpClient:
OBEXFtpClient ftpClinet = new OBEXFtpClient();
ftpClinet.Connect(conn);
string mainDirList = ftpClinet.ListDir();
|
So now we are connected to your mobile and we have xml
listing of the content of its default folder.
|