Android™ Bluetooth® Low Energy (BLE) App Development For BM70 / RN4870
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) applications 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 a BLE peripheral, which communicates with the BLE Android app. For developer topics or advanced API information for Android BLE development refer to the Android developer website and other available app development resources. For detailed information on BLE refer to the available Bluetooth specification document.
Microchip offers a broad range of Bluetooth ICs and modules that 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 is available in Android OS. All the available APIs on Android OS are part of the 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 that 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 compiled 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.
Reference Materials
To follow the process described on this page you will need the following hardware and software tools:
- BM-70-PICTAIL
- Android Studio: Android app development environment and tools
- Android Studio Overview and Installation
- Tera Term
- Android v5.2 and up
- BLE Hardware version and up
- BM70/71 User's Guide
- BM70 EVB User's Guide
- BM70 Compact Demo Board (CDB) User's Guide
- Documentation for app developers
- BM70 Microchip Developer Help
- Bluetooth SIG Specifications
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 that is advertising and initiate an outgoing connection with it. Such a BLE device that 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 a 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 that 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 that 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 accesses Services with Characteristics as a GATT Client. After a successful connection, the GATT Client performs a Services discovery to discover all the Services and Characteristics hosted by the GATT Server and then initiates 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 of the BM70/RN4870 modules and use them to communicate with 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 is available in Android OS. All the available APIs on Android OS are part of the 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 a lack of BLE hardware or a 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 the 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 a BLE application for Android OS that can 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.