Bluetooth low energy on Android device (central side)

Fares Othmane Mokhtari
Dev TEch
Published in
3 min readSep 20, 2018

I have started using Bluetooth low energy since January this year and I know it is not difficult but there are not enough resources on the web I started writing posts about it :

the first post was a simple introduction to this technology

the second one was a very deep dive in the BLE stack

but the most lovely and exciting part is how to use ble in android so I decided to make a kind of tutorial to share my knowledge.

before starting you have to know that :

our service’s UUID is: 19fc95c0-c111–11e3–9904–0002a5d5c51b

our characteristics UUID is :21fac9e0-c111–11e3–9246–0002a5d5c51b

in android (java or kotlin) Bluetooth low energy is about using a different callback to handle different situations (advertising , scanning, connecting …) so let’s start coding

declare BLE permission :

to be able to use Bluetooth low energy in the android you must declare permission in the manifest file

since SDK 6.0 location permission is necessary so we have to request a location permission

after that, we have to get some instances of BluetoothAdapter , BluetoothLeScanner and BluetoothGatt

and now we have to add our service and characteristic UUIDs in our code

The Bluetooth adapter represents the local device’s Bluetooth adapter and lets you perform fundamental Bluetooth tasks, such as discovering other devices and getting the properties of the discovered devices. BluetoothLeScanner provides methods to perform scan-related operations specific to Bluetooth LE devices and BluetoothGATT provides functionality to enable communication with Bluetooth Smart devices.

The next step is to initialize the Bluetooth adapter inside the onCreate function

to scan for nearby Bluetooth peripherals there are many solutions but for us we are going to use the easiest because we know our characteristics and services UUID:

we check the Bluetooth if it is enabled on the device by using BluetoothAdapter if it isn’t we request the user to enable it, after that we get an instance from BluetoothLeScanner which is used to start the scan.

but before start the scan we have to choose the scan mode, there are several modes and we have to choose one of themes:

* SCAN_MODE_BALANCED:

Perform Bluetooth LE scan in balanced power mode. Scan results are returned at a rate that provides a good trade-off between scan frequency and power consumption.

* SCAN_MODE_LOW_LATENCY:

Scan using the highest duty cycle. It’s recommended to only use this mode when the application is running in the foreground.

* SCAN_MODE_LOW_POWER:

Perform Bluetooth LE scan in low power mode. This is the default scan mode as it consumes the least power. This mode is enforced if the scanning application is not in the foreground.

* SCAN_MODE_OPPORTUNISTIC:

A special Bluetooth LE scan mode. Applications using this scan mode will passively listen for other scan results without starting BLE scans themselves.

now we need to define the callback variable (we can give whatever we want as an implementation name such as scanCallback).

the most important method overridden is onScanResult, which is called whenever there is any device to report when then we check if the device name is the same as the one that was defined in our code, if so we can save the device properties and connection information to bleGatt variable, we can also connect to the device by using bleGattCalback, which will be called whenever an Android system establishes a connection to the device, we stop the scan if we have found the device we are looking for.

in this callback we have overridden three important methods :

  • The onConnectionStateChange: is called whenever a connection is established to the remote device through Bluetooth, in this case, we can start a service discovery by the discoverServices.
  • the onServicesDiscovered: this method is called whenever services are discovered on the device.
  • the onCharacteristicRead: is used to read the value from the characteristics within the service.

I hope that this tutorial was clear enough to introduce you to Bluetooth low energy on Android please feel free to contact me if you need any help.

--

--