bluetooth low energy on Android device (central side)
I have started using bluetooth low energy since January this year and I know it is not difficult but there are not enough resource on the web i stated 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 different callback to handle different situation (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 a 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 bluetoothAdapter 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.
next step is to initialize bluetooth adapter inside onCreate function
to scan for nearby bluetooth peripheral there are many solutions but for us we are going to use the easiest because we know our charactristic 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 mode and we have to choose one of theme :
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 using highest duty cycle. It’s recommended to only use this mode when the application is running in the foreground.
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 foreground.
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 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 th one that was defined in our code, if so we can save the device propreties 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 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.