Objective
This document serves as a basic helping guide for understanding the sequence of operations and basic APIs required for creating Bluetooth® Low Energy (BLE) application for Android™ OS. It is not meant to be a comprehensive document for Android BLE app development. BM70/RN4870 module is used as an example of BLE peripheral, which communicates with the BLE Android app. For developer topics or advanced API information for Android BLE development refer to Android developer website: https://developer.android.com/docs/ and other available app development resources. For detailed information on BLE refer to the Bluetooth specification document available on https://www.bluetooth.com/specifications.
Microchip offers a broad range of Bluetooth ICs and modules which support BLE technology. The Microchip BLE devices can be used to connect to Android OS and Apple iOS devices over BLE using apps to be able to define services and perform bidirectional data communication.
Bluetooth Low Energy support for Central and Peripheral roles are available in Android OS. All the available APIs on Android OS are part of Bluetooth class (android.bluetooth and android.bluetooth.le). In a Central role the Android BLE APIs can be used to scan and discover BLE peripheral devices, initiate a connection, query for available BLE services and characteristics and perform access operations on those characteristics. The BM70/RN4870 module acts as a BLE peripheral which can discover, connect, and communicate with the Android app.
The MCHPTransparentUART Android Studio app package is provided with this help guide. This app runs on the Android OS and connects and communicates with a BM70/RN4870 BLE module using the Microchip Transparent UART Service. Android Studio 3.2, with compile SDK version API 28 (Android 9.0 Pie), was used for the app at the time of creation. A user must migrate as needed for newer versions of Android Studio and API levels to be up to date. The BLE APIs discussed as part of this document are from Android API level 21 and up. The BLE APIs in Android API levels lower than 21 are not discussed.
Materials
To follow along the process described on this page you will need the following hardware and software tools:
Hardware Tools
Tool | About | Purchase |
---|---|---|
| |
Device | OS Version | BLE Hardware Version |
---|---|---|
Android | v5.2 and up | v4.0 and up |
Software Tools
- Android Studio: Android app development environment and tools
- MCHPTransparentUART Android Studio Example App
- Terminal Emulator
- Windows: Tera Term
Basics of Bluetooth Low Energy
BLE specification defines two roles for a BLE device: Central role and Peripheral role. Depending on the design implementation a BLE device can support either one of the roles or both roles.
A BLE device can scan to discover another BLE device which is advertising and initiate an outgoing connection with it. Such a BLE device which can initiate a connection is called a Central device. An example of a Central device is an Android or iOS smartphone. The BLE device which is advertising and discoverable by the Central device and can accept an incoming connection is called a Peripheral device. An example of a Peripheral device is a BM70/RN4870 BLE module.
The requirements and procedures for scanning and initiating an outgoing connection by Central device, and advertising and accepting an incoming by a Peripheral device, are defined in the Generic Access Profile (GAP) defined by the Bluetooth specification. The Central device is the GAP master and the Peripheral device is the GAP slave.
Once the Central device scans and discovers the Peripheral device and establishes a successful connection, the BLE devices can start data communication to realize an end application. The requirements and procedures for data communication between connected BLE devices are defined by the Generic Attribute Profile (GATT) defined by the Bluetooth specification. Data is transferred and stored as a Characteristic defined on the BLE devices. The Characteristics have the following attributes:
• Value: Data value storage for the characteristic
• Permissions: Access (Read, Write, Notify, Indicate), Encryption, Authorization
• Type: Unique addressable 16/32/128-bit UUID identifier
• Handle: Unique addressable 16-bit identifier for each attribute
A collection of Characteristics is called a Service. A Service is addressed by a UUID. For example, you could have a service called Health Thermometer that includes characteristics such as Temperature Measurement. Each Service can define multiple Characteristics.
A collection of Services is called a Profile. Each Profile can define multiple Services.
The BLE device which hosts the Services and the corresponding Characteristics is called a GATT Server. For example, the BM70/RN4870 Peripheral typically hosts Services with Characteristics as a GATT Server like the Microchip Transparent UART Service. The BLE device which sends requests to the GATT Server to access the Characteristics of the Services hosted by the GATT Server is called the GATT Client. For example, the Android Smartphone Central typically access Services with Characteristics as a GATT Client. After a successful connection, GATT Client performs a Services discovery to discover all the Services and Characteristics hosted by the GATT Server and then initiate supported access operations to read, write, get notified/indicated using the addressable attributes, like Handle or Type UUID.
BM70/RN4870 BLE module is used as a Peripheral to showcase an example of connecting to the Android app as a Central. As a GATT Server, the BM70/RN4870 hosts the Microchip Transparent UART Service. The Transparent UART Service provides a simple bidirectional data transfer service. It defines two Characteristics of data communication. The Android app acting as a GATT Client communicates with the Microchip Transparent UART Service in the discussed example. A user can also create a custom Service with Characteristics on the BM70/RN4870 modules and use them to communicate to the Android app. Refer to the BM70/RN4870 User’s Guide and the Development Board User’s Guide in the References section for more information.
Additional information about BLE is available at this link Introduction to BLE.
BLE Android App Development Process
BLE support for Central and Peripheral roles are available in Android OS. All the available APIs on Android OS are part of Bluetooth class (android.bluetooth and android.bluetooth.le). The APIs discussed as part of this document are available from Android API level 21 and up. Note that not all Android devices provide BLE functionality or features due to either lack of BLE hardware or version of BLE supported.
The sequence of steps to perform in the Android app to enable BLE is as follows:
- BLE Permissions - Enable BLE permissions in the Android app manifest to use BLE.
- Android Service - Add an Android Service for BLE to keep BLE operations active in background.
- Scan - Scan for nearby BLE Peripheral devices by using the BLE Scan API.
- Connect - Connect to the BLE Peripheral selected by the user using the Connect API.
- Discover - Discover the Services and Characteristics hosted by the connected BLE Peripheral by using the Discover Services API.
- Access - Perform access of the Characteristics by using the Characteristic Access APIs.
The BM70/RN4870 Microchip Transparent UART Service is used for data transfer as detailed in the following links:
Conclusion
This page explains the sequence of procedures and the basic APIs required for creating BLE application for Android OS that is able to connect and communicate with a Microchip BLE device. As mentioned, the Microchip BLE devices can be used to connect to both Android OS and Apple iOS devices over BLE using apps to be able to define services and perform bidirectional data communication. The MCHPTransparentUART Android app is provided as a basic BLE app so that users can understand the scanning, connection, service, and characteristics discovery and data communication procedures performed on a BM70/RN4870 BLE module. The MCHPTransparentUART Android example app is available for download alongside this Android BLE help guide.
References
- BM70 User’s Guides
- RN4870 User’s Guides
- Android BLE Overview and Developer Resources
- BM70 Microchip Developer Help
- RN4870 Microchip Developer Help
- Bluetooth SIG Specifications