In this Blog, you will learn how you can work BLE in android and how you can perform communication with BLE devices using your app.
How is Bluetooth Low Energy different from Bluetooth Classic?
The "Low Energy" part of the BLE acronym pretty much gave it away: BLE is optimized for power efficiency, while Bluetooth Classic is intended for transmitting continuous streams of data like music playback. A BLE device is ideal for sensor-based or Internet of Things (IoT) use cases because it can usually operate on a small battery for weeks, if not months, or even years.
Some Basic Definitions:-
BLE: Android embedded platform for discovering devices and requesting and sending information from Bluetooth devices.
GATT: A generic attribute profile that defines how data is exchanged using predefined attributes.
Central: A computer/tablet/mobile device, also known as a GATT client. Scans, requests, and uses data provided by peripherals.
Peripheral: A device that sends data, also known as a GATT server. Data is structured as a definition for interacting with that "database".
Service: A set of functions and associated operations that are provided to interact with a peripheral. Each service contains a collection of properties.
Property: Data definition separated by declaration and value. Use the permission property (read, write, notify, view) to get the value.
descriptor: An optional attribute nested within a property that describes a particular value and how to access it.
UUID: A universally unique ID transmitted over the air so that the peripheral can tell the control center what services it offers.
Permissions:-
Here are some permissions required to run a BLE process in an Android app.
android.permission.BLUETOOTH android.permission.BLUETOOTH_ADMIN android.permission.ACCESS_COARSE_LOCATION
If the android version is more than 10 then there are some other permissions.
android.permission.BLUETOOTH_CONNECT android.permission.ACCESS_FINE_LOCATION android.permission.ACCESS_BACKGROUND_LOCATION
BluetoothAdapter
The BluetoothAdapter enables you to carry out standard Bluetooth tasks like starting device discovery, looking up a list of bonded (paired) devices, launching a BluetoothDevice with a known MAC address, starting a BluetoothServerSocket to monitor connection requests from other devices and scanning for Bluetooth LE devices.
val bluetoothManager : BluetoothManager = getSystemService (Context.BLUETOOTH_SERVICE) as BluetoothManager. val bluetoothAdapter : BluetoothAdapter = bluetoothManager.adapter if(!bluetoothAdapter.isEnabled){ val intentBtEnabled = Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE) startActivity(intentBtEnabled) }
Here is what happens when we scan the Bluetooth adapter to refer to the Bluetooth of your device. Also a check for letting you know if the bluetooth is online or offline.
private val leScanCallback: ScanCallback = object:ScanCallback() { override fun onScanResult (callbackType: Int , result: ScanResult) { super.onScanResult(callbackType, result) leDeviceListAdapter.addDevice(result.device) leDeviceListAdapter.notifyDataSetChanged() } }
Bluetooth LE scan callback and the scan results are reported using these callbacks.
private val bluetoothLeScanner = bluetoothAdapter.bluetoothLeScanner
This is to start the scanner that will scan the Bluetooth devices near your mobile in which you are using the application. Here is one important thing you can’t utilize bluetooth functionality in your emulator you need a dedicated mobile device to use bluetooth.
bluetoothLeScanner.startScan(leScanCallback)
To stop the scanning for the Bluetooth device, you have to use
bluetoothLeScanner.stopScan(leScanCallback)
If you didn’t use this syntax the scanning for the Bluetooth device won’t stop until you close the app.
When the app detects any Bluetooth device near the mobile, the leScanCallback function will execute and then the function will provide an object of the Bluetooth device which contains data of the device.
For establishing a Gatt connection with the device you need to execute simple syntax.
bluetoothGatt = device.connectGatt(this, false, bluetoothGattCallback)
It will take three arguments (Context, Auto Connect true/false, GattCallBack).