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) 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
BM70-pictail-50px.png
BM-70-PICTAIL
BM70 Bluetooth Low Energy PICtail™ Plus
Device OS Version BLE Hardware Version
Android v5.2 and up v4.0 and up

Software Tools

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:

  1. BLE Permissions - Enable BLE permissions in the Android app manifest to use BLE.
  2. Android Service - Add an Android Service for BLE to keep BLE operations active in background.
  3. Scan - Scan for nearby BLE Peripheral devices by using the BLE Scan API.
  4. Connect - Connect to the BLE Peripheral selected by the user using the Connect API.
  5. Discover - Discover the Services and Characteristics hosted by the connected BLE Peripheral by using the Discover Services API.
  6. 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:

  1. BM70/RM4870 Microchip Transparent UART Service
  2. Accessing the Transparent UART Service

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

  1. BM70 User’s Guides
    1. BM70/71 User's Guide
    2. BM70 EVB User's Guide
    3. BM70 Compact Demo Board (CDB) User's Guide
  2. RN4870 User’s Guides
    1. RN4870/71 Bluetooth Low Energy Module User's Guide
    2. RN4870/71 PICtail/PICtail Plus Daughter Board User's Guide
  3. Android BLE Overview and Developer Resources
    1. Bluetooth low energy overview
    2. Documentation for app developers
  4. BM70 Microchip Developer Help
  5. RN4870 Microchip Developer Help
  6. Bluetooth SIG Specifications
© 2024 Microchip Technology, Inc.
Notice: ARM and Cortex are the registered trademarks of ARM Limited in the EU and other countries.
Information contained on this site regarding device applications and the like is provided only for your convenience and may be superseded by updates. It is your responsibility to ensure that your application meets with your specifications. MICROCHIP MAKES NO REPRESENTATIONS OR WARRANTIES OF ANY KIND WHETHER EXPRESS OR IMPLIED, WRITTEN OR ORAL, STATUTORY OR OTHERWISE, RELATED TO THE INFORMATION, INCLUDING BUT NOT LIMITED TO ITS CONDITION, QUALITY, PERFORMANCE, MERCHANTABILITY OR FITNESS FOR PURPOSE. Microchip disclaims all liability arising from this information and its use. Use of Microchip devices in life support and/or safety applications is entirely at the buyer's risk, and the buyer agrees to defend, indemnify and hold harmless Microchip from any and all damages, claims, suits, or expenses resulting from such use. No licenses are conveyed, implicitly or otherwise, under any Microchip intellectual property rights.