BTLE Example

It is mandatory­­ that you are working with a device supports API 18 or higher.

Declare the following permission in manifest file.

uses-feature android:name=”android.hardware.bluetooth_le” android:required=”true”

uses-permission android:name=”android.permission.BLUETOOTH”
uses-permission android:name=”android.permission.BLUETOOTH_ADMIN”
uses-permission android:name=”android.permission.ACCESS_FINE_LOCATION”
uses-permission android:name=”android.permission.ACCESS_COARSE_LOCATION”

How to ­­Get BluetoothAdapter and Enable?

BluetoothManager bluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
BluetoothAdapter bluetoothAdapter = bluetoothManager.getAdapter();
if (bluetoothAdapter != null && !bluetoothAdapter.isEnabled()){
Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);

How to discover Devices?

It is an Asynchronous Process.
Device discover method are different for Pre Lollipop and Post Lollipop,So In your code you have to handle that.

How to do in Pre Lollipop?

BluetoothManager bluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
BluetoothAdapter  bluetoothAdapter = bluetoothManager.getAdapter();
This Method will get called whenever a new device is discovered.
BluetoothAdapter.LeScanCallback leScanCallback = new BluetoothAdapter.LeScanCallback() {


    public void onLeScan(final BluetoothDevice device, int rssi, byte[] scanRecord) {
 runOnUiThread(new Runnable() {
public void run() {

For Starting and cancelling Discovery use


How to do in Post Lollipop?

BluetoothLeScanner bluetoothLeScanner = bluetoothAdapter.getBluetoothLeScanner();
ScanCallback scanCallback = new ScanCallback() {


    public void onScanResult(int callbackType, ScanResult result) {



    public void onBatchScanResults(List results) {



    public void onScanFailed(int errorCode) {

        Log.i(TAG ,"onScanFailed "+ errorCode);


For Starting and cancelling Discovery use
ScanSettings settings = new ScanSettings.Builder().setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY).build();
List filters = new ArrayList();

How to Create BluetoothGattCallback?

Once you found expected bluetooth device,It is time to initiate connection using

BluetoothGatt mBluetoothGatt=bluetoothDevice.connectGatt(this,false,mGattCallback);

What is BluetoothGattCallback?

  • Public API for the Bluetooth GATT Profile.
  • This class provides Bluetooth GATT functionality to enable communication between Bluetooth Smart and Bluetooth Smart ready devices.
  • To connect to a remote peripheral device,Create BluetoothGattCallback and call
    bluetoothDevice.connectGatt(this,false,BluetoothGattCallback) to get instance of this class;
BluetoothGattCallback mGattCallback = new BluetoothGattCallback() {


    public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
//Called when a device connects or disconnects.



    public void onServicesDiscovered(BluetoothGatt gatt, int status) {
//Called after client initiates BluetoothGatt.discoverServices()



 public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
//Called when client calls //BluetoothGatt.readCharacteristic(BluetoothGattCharacteristic characteristic);




    public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
//Called when you perform a read or write characteristic operation.



Discover Services and Characteristics

        • Once the connection success, BluetoothGattCallback.onConnectionStateChange() will be called with the newState argument set to BluetoothProfile.STATE_CONNECTED
    • After this , BluetoothGatt.discoverServices() can be called.When the device responds, you will receive the callback BluetoothGattCallback.onServicesDiscovered()
List services = bluetoothGatt.getServices();
for (BluetoothGattService service : services) {
         List characteristics = service.getCharacteristics();


It is always recommended to stop the scan once you found expected device because discovery process will drain android device’s battery.

How to Configure Descriptor?

From characteristic,try to get Descriptor using CLIENT_CHARACTERISTIC_CONFIG()
where CLIENT_CHARACTERISTIC_CONFIG = “00002902-0000-1000-8000-00805f9b34fb”;


BluetoothGattDescriptor bluetoothGattDescriptor = characteristic.getDescriptor(

Receiver Notification:

Whenever GATT server sends notification to the android device, BluetoothGattCallback.onCharacteristicChanged() will be called,Inside this callback you will of data sent by GATT Server.

How to Disconnect & Close GATT Client?


Example ::

Basic functionalities of BLE is available in this Github code