HomePython Page 2 - Bluetooth Programming using Python
Bluetooth and Python: the Whys and Wherefores - 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.
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.
Serial Port Profile - emulates serial cable connectivity and communication. It uses RFCOMM protocol.
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.