Login

Bluetooth Programming using Python

Bluetooth is a way to connect devices wirelessly. This technology has a variety of uses. Python is an easy to learn scripting language that has been growing in popularity. The two can work well together, as will be explained in this article.

In today’s world, ad-hoc networks have become common. The main aspect of an ad-hoc network is that the participants of the network communicate more or less in a wireless way. Bluetooth has emerged as the prominent mode of wireless communication among devices forming an ad-hoc network.

This means that any software targeted at such devices needs to be Bluetooth enabled. In other words, the language with which such software is developed needs to provide an API that facilitates Bluetooth enabling. However, most of the time the APIs provided by the languages introduce a steep learning curve due to the lack of flexibility of the language itself.

It is here that scripting languages, especially a language such as Python, provide an easier way. In this discussion, I will focus on the use of Python API for programming Bluetooth devices. The next section will be about the whys and wherefores of Bluetooth and how Python fits into the picture. The third section will enumerate the steps in using Python API in accessing the Bluetooth devices and their services. The final section will put the theory into practice by developing a real world application. That’s the outline for the discussion.

{mospagebreak title=Bluetooth and Python: the Whys and Wherefores}

According to its definition, Bluetooth is "a radio standard and communications protocol primarily designed for low power consumption, with a short range (power-class-dependent: 1 meter, 10 meters, 100 m) based on low-cost transceiver microchips in each device." In other words, Bluetooth is a standard and protocol that uses short-range frequencies to create a wireless connection between two compatible (Bluetooth enabled) devices. Since a radio communication system is used, there is no "line of sight" problem that is common with infrared-based devices. As long as the devices are in range, they can communicate with each other even if one device is in one room and the other device is in a separate room. Bluetooth-enabled devices are categorized into three classes, which are:

Class 1

Class 2

Class 3

The classification is based on the range in which the devices can communicate. The ordering is from the longest range to the shortest one.

Class 1 devices have the longest range. The range is up to 100 meters, approximately. Devices of this class consume the highest amount of power of all three classes. They consume 100 mW of power.

Class 2 devices can be considered medium range. The range in which they can communicate is up to 10 meters. With respect to power consumption, they require 2.5 mW.

Class 3 devices cannot communicate beyond one meter. The power requirement also is the least for devices of this class. They require only 1 mW.

If you observe closely the power requirement decreases with the decrease in communication range. That was a bird’s eye view of Bluetooth. Next we will look at how a device provides services to other devices using Bluetooth. Each device provides certain services based on a specification known as the Wireless Interface Specification. A set of such services forms a profile. Sometimes the profile contains only one service, such as streaming of sound from an audio file, or it can contain a set of different services such as capturing a live video and streaming it. There are 27 standard profiles that are currently used by different devices. Some of the common profiles are:

Basic Imaging Profile – for sending, receiving and resizing images.

Basic Printing Profile – for using certain vCards, text, e-mail etc. to send to the printer from the device.

File Transfer Profile – for accessing the file system of another device.

Headset Profile – provides support for Bluetooth headsets to work with Bluetooth-enabled mobile phones for wireless audio; one of the most common profiles.

Writing a program for a Bluetooth device, in reality, means programming for the profiles or accessing the functionalities of the profile. For programming, the BlueZ library is commonly used. It provides access to the Bluetooth stack in an object-oriented and modular manner. The Python wrapper for the BlueZ is PyBlueZ.

The wrapper targets the Microsoft Bluetooth stack on Windows and BlueZ stack on GNU/Linux. Using PyBlueZ, one can easily create a connection between traditional systems such as laptops, desktops and mobile devices and program the basic profiles such as File Transfer, network communication, and so forth. Now that the library that supports programming for Bluetooth has been introduced, we can look at the steps required to program Bluetooth devices using Python.

{mospagebreak title=Bluetooth Programming, Step by Step}

For whatever profile the programming is being done, there are two main steps that one has to follow. They are:

Discovering devices in range

Looking up the human readable name

One point to keep in mind is that both of these are probabilistic, i.e. sometimes these steps may fail on the first attempt. Hence, it is a good idea to repeat these steps twice or thrice before deciding that there are no devices nearby.

Discovering Devices in Range

The first step is to find the devices that are in range so that a connection can be established with them. This is known as the discovery of devices. To discover devices in range, one needs to call the discover_devices() method. It returns a list of addresses of devices discovered. For example, the following statement discovers devices in range and returns a list of such devices:

discovered_devices = discover_devices()

Looking up the human readable name

The addresses of the discovered devices are of the form "XX:XX:XX:XX:XX" where each X is a hexadecimal character representing "one octet of the 48-bit address." However, to actually access the device with which one needs to communicate, one should know the "human readable format" of the address. One of the main reasons is that a person gives a name that he or she can remember for a particular device, and it’s easier to match with that name rather than a 48-bit address for the same.

To get the human readable format one can use the lookup_name() method. It accepts the address of a device and returns its user friendly or human readable format. For example, to find a device named "Raj" from the list of the of return addresses the code would be:

target_device = "Raj"target_device_address = None

for address in discovered_devices: if target_device==lookup_name(address): target_device_address=address break

if target_device_address is not None: print "The address of the target device is :",
target_device_address else: print "Could not find address of target device"

The code iterates over the list of addresses and passes one address at a time to the lookup_name method. Then it compares the returned name with the desired device name. if it matches, a message is printed and the method breaks out of the loop.

That completes the basic steps required to connect to any device. Once the discovery is done, then based on type of service, communication can begin. Now that the steps are clear, let us look at a real world example that uses the steps just detailed to access a Bluetooth device.

{mospagebreak title=Bluetooth Programming in the Real World}

The first rule of any program that targets the real world is to provide modularity. In our case, we can provide modularity by wrapping the device discoverycode in a class. The name of the class will be Devices. Lets start with the imports:

from bluetooth import *

Next comes the class with the constructor. The constructor takes the name of the device whose address has to be found.

Next let us define the method that will perform the method lookup. If the target device is found, it will set the address to the variable self.target_device_address. To find the address, it iterates over the list of addresses returned by the discover_devices() method and passes each address to the lookup_name method.

if self.target_device_address is not None: return self.target_device_addresselse: return None

That completes our class. Now let us test it by calling it from another module. The module will first ask the user to enter the name of the device to be discovered. Then it will create an object of the Device class and call the check_device method. The returned result will be displayed to the user. Here is the code: