How to Add Custom Profiles in Bluetooth – Part 1
Audio : Listen to This Blog.
This article describes the procedure to add a custom Bluetooth profile for sharing the temperature and humidity data over Bluetooth. The article is split into two; the first part will brief on the Bluetooth technology and Bluetooth profiles while the second part gives details on the addition of custom Bluetooth GATT profile. We assume the reader has some knowledge on the Bluetooth technology, during writing of this article.[
Brief overview of Bluetooth
As we all know Bluetooth is a wireless communication technology, which is found in almost all the mobile phones. The range of these wireless communication medium is based on the Bluetooth device type. There are two types of Bluetooth devices, Single mode and Dual mode devices. Single mode Bluetooth devices are devices which either support BR/EDR or LE profiles. Dual mode devices support both BR/EDR and LE profiles. The range for BR/EDR devices is 10 meters and the range for LE devices is around 1-2 meters. The figure below shows the Bluetooth stack and the details of the stack are given as below.
Software Layers in Bluetooth
As we can see from the above figure, Bluetooth software stack is layered implementation. A brief overview of the layers of Bluetooth stack (seen in the above figure) are as below.
Bluetooth Radio operates in the 2.4 GHz ISM band (same as the Wi-Fi, ZigBee etc.). It uses a frequency hopping mechanism with 79 channels to combat interference with other channels which operate in the same frequency spectrum. This layer is responsible for the following primary tasks:
- Transmission and Reception of packets over the air
- Support appropriate power class of the device, for LE devices (power consumption is less)
Baseband Controller (also referred to as the Link Controller)
The Baseband controller performs the following major functions:
- Management of physical channels and links
- Manages frequency hopping for transmitting and receiving packets
- Management of formation of piconet and scatternet
- Handles packets formation for Bluetooth radio transmission
- Inquiry and Inquiry Scan
- Connection and Page Scan
- Security (including data encryption)
- Power management (including low power modes)
The Link Manager performs the functions of link setup and control. The LMP supports connection control (creation and removal of a connection), security (authentication, pairing, and encryption), informational requests (characteristics exchange), role switch, also handles logical transports, Quality of Service and link supervision.
Host Controller Interface (HCI)
The Host Controller Interface (HCI) provides uniform method for accessing the controller’s capabilities. The HCI layer provides an interface for the host to talk to the controller. The host sends HCI command packets to the controller and is asynchronously notified by the controller using HCI events. The LE controllers use a reduced set of HCI commands. There are four types of packets that can be sent on the HCI Interface.
- HCI Command Packet.
- HCI Asynchronous (ACL) Data Packet.
- HCI Synchronous Data Packet.
- HCI Event Packet.
L2CAP layer (Logical Link Control and Adaptation Protocol)
The L2CAP layer acts as an interface between the higher layer protocols and the lower layers. L2CAP provides the abstraction of channels to layers on top of it. Following are some of the features supported by the L2CAP layer:
- Fixed channel identifiers
- Fragmentation and Defragmentation of data
- Multiplexing and demultiplexing of various channels over a shared logical link
The RFCOMM protocol layer is on top of the L2CAP layer. RFCOMM layer provides emulated RS-232 serial ports. The Bluetooth serial port profile is based on this protocol. It is used directly by many telephony related profiles to send AT commands, and used as transport layer for OBEX over Bluetooth. In the protocol stack, RFCOMM is bound to L2CAP.
SDP protocol layer is used to allow devices to discover what services are supported by each other, and what parameters to use to connect to them. SDP is used to determine which Bluetooth profiles are supported by the Bluetooth device (like headset profile, hands free profile, advanced audio distribution profile, etc.) and the protocol multiplexer settings needed to connect to each of them. Each service is identified by a Universally Unique Identifier (UUID), with official services (Bluetooth profiles) assigned a short form UUID (16 bits rather than the full 128). In the protocol stack, SDP is bound to L2CAP.
Object exchange is a communications protocol that facilitates the exchange of binary objects between devices. It is adopted by the Bluetooth Special Interest Group. In Bluetooth, OBEX is used for many profiles that require simple data exchange. In the protocol stack, OBEX is bound to RFCOMM.
ATT protocol is like SDP protocol, but specially adapted and simplified for Low Energy Bluetooth. It allows a client to read and/or write certain attributes exposed by the server in a non-complex, low-power friendly manner. In the protocol stack, ATT is bound to L2CAP.
Profiles in Bluetooth
Bluetooth devices need to support appropriate profiles, to be able to work together for accomplishing a given task. A profile defines a usage case. Profiles are the “services” offered by a device. For two devices to interoperate to complete a user task, both devices must implement a common profile. Profiles are definitions of possible applications and specify general behaviours that Bluetooth enabled devices use to communicate with other Bluetooth devices. Profiles build on the Bluetooth standard to more clearly define what kind of data a Bluetooth module is transmitting. The device’s application determines which profiles it must support, from hands-free capabilities to heart rate sensors to alerts and more.
For two Bluetooth devices to be compatible, they must support the same profiles. Profiles are different for Bluetooth BR/EDR and LE implementations. Compatibility between BR/EDR and LE implementations requires a dual-mode controller on at least one device for interoperability. For BR/EDR, a wide range of adopted Bluetooth profiles describe many different common types of applications or use cases for devices. For Bluetooth LE, developers have the option of using a comprehensive set of adopted profiles, or use the Generic Attribute Profile (GATT) to create new profiles.
Below is a list of some commonly used Bluetooth Profiles:
- Advanced Audio Distribution Profile (A2DP)
- Audio/Video Remote Control Profile (AVRCP)
- Generic Object Exchange Profile (GOEP)
- Human Interface Device Profile (HID)
- Personal Area Networking Profile (PAN)
- Serial Port Profile (SPP)
- Service Discovery Profile (SDAP)
- SIM Access Profile (SAP, SIM)
- Basic Imaging Profile (BIP)
- Basic Printing Profile (BPP)
- Device ID Profile (DID)
- Dial-up Networking Profile (DUN)
- File Transfer Profile (FTP)
- Generic Access Profile (GAP)
- Hands-Free Profile (HFP)
- Headset Profile (HSP)
- Object Push Profile (OPP)
GATT (Generic Attribute profile)
Unlike GAP which broadcasts one-to-many, GATT uses a one-to-one connection between the board (server) and the phone (client). GATT establishes in detail how to exchange all profile and user data over a BLE connection. GATT uses the Attribute Protocol (ATT) as its transport protocol to exchange data between devices. This data is organized hierarchically in sections called services, which group conceptually related pieces of user data called characteristics.
The second part of the article will discuss in more detail the fundamental aspects of GATT and addition of custom profiles.