Android Bluetooth Low Energy (BLE) – Part 1

Tram Ho

Hi guys, I’m currently doing a project related to BLE (Bluetooth Low Energy). I looked online and found a lot of documents related to Bluetooth Classic, and the documentation about BLE is much more limited. But I noticed that the applications that serve to connect to the device, currently using BLE as a connection solution is quite common. So today I will share some basic knowledge related to this topic. Somewhere will help you to overview the approach, so that it can be easily investigated and implemented as required.

Your article includes the following main sections:

  1. About BLE
  2. Find BLE Device
  3. Connection and Disconnect BLE Device
  4. Transfer BLE Data

Let’s start

1. Introduction to BLE

The problem is, in most cases, designers of wearables, peripherals, as well as all other items need to extend their functionality with smartphones. Everyone needs to find a solution to connect, control and share data. And Bluetooth Classic and BLE are the solution. In the scope of today’s sharing, I only focus on the Android platform. As you know, Bluetooth Classic allows the device to exchange data wirelessly with other Bluetooth devices. The application framework provides access to Bluetooth functionality through the Bluetooth API. These APIs allow applications to connect to other Bluetooth devices, enabling point-to-point and multipoint wireless features.

Android version 4.3 (API 18) and above BLE was born. BLE is designed for significantly lower power consumption. This allows applications to communicate with BLE devices that have more stringent power requirements. Specifically: Common use cases include:

  • Transfer small amounts of data between neighboring devices.
  • Interact with proximity sensors to provide users with a customized experience based on their current location.

2. Find BLE Device

This is the first step before using Bluetooth Classic or BLE features. You need to make sure that all necessary permissions and features are applied and allowed.

Permissions :

  • android.permission.BLUETOOTH – basic Bluetooth classic and BLE features
  • android.permission.BLUETOOTH_ADMIN – advanced BC and BLE operations such as on/off module _ Bluetooth, discovery device, ….
  • android.permission.ACCESS_COARSE_LOCATION – required for BLE scanning on Android 5.0 (API 21) and later. Note: This is Runtime Permission from API 23.

I have read more attention about a permission that I see or be declared when dealing with BLE. You also need to make sure that your phone or Tablet has a built-in Bluetooth adapter. If we want to not be available to non-Bluetooth devices, we just need to add the declaration:

However, according to the Document I learned, it is not required if you declare the Bluetooth permission and set Android 5 or higher version.

image

To find BLE devices, you use startScan() and stopScan() . This method takes leScanCallback as a parameter. You must implement this callback because that’s how the scan results are returned. Pay attention to the optimization when executing, for example stopScan() when it has found the device you want or set a certain interval time scan…

image

You can also consider scanning mode in the case of Background down and ForceGround respectively to optimize the device’s power.

image

In the next I will continue with the Connection, Disconnect BLE Device and Transfer BLE Data of BLE.

See you in the next post.

Share the news now

Source : Viblo