Bluetooth Le Gatt Example, Step-By-Step

Posted on

Last post demonstrate the Android Bluetooth Le Gatt instance code amongst Arduino/Genuino 101. This post start to (TRY to) implement my own Bluetooth LE Gatt Example, step-by-step. The aim of the examples (in coming series) are scan BLE device, discovery together with link amongst specified device Genuino 101 (programmed amongst “LEDCB”, refer previous post), together with read together with write characteristic of the device to plough ON/OFF the Genuino 101 on-board LED.

The brain reference fabric are:
Android Developers Guide > Bluetooth Low Energy
– The Android instance code – BluetoothLeGatt. (The instance inwards lastly post)

In this outset step, create a novel projection inwards Android Studio, named AndroidBleGatt.

In my instance (in coming post) I volition scan LE device using BluetoothLeScanner (Added inwards API degree 21) instead of calling deprecated startLeScan() and stopLeScan() methods every bit inwards the BluetoothLeGatt example, then specify the Min Sdk Version of API 21.

Edit AndroidManifest.xml to add uses-feature of “android.hardware.bluetooth_le”, uses-permission of “android.permission.BLUETOOTH”, “android.permission.BLUETOOTH_ADMIN” together with “android.permission.ACCESS_COARSE_LOCATION”.


<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android=""     package="com.blogspot.android_er.androidblegatt">      <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_COARSE_LOCATION"/>      <application         android:allowBackup="true"         android:icon="@mipmap/ic_launcher"         android:label="@string/app_name"         android:supportsRtl="true"         android:theme="@style/AppTheme">         <activity android:name=".MainActivity">             <intent-filter>                 <action android:name="android.intent.action.MAIN" />                  <category android:name="android.intent.category.LAUNCHER" />             </intent-filter>         </activity>     </application>  </manifest> 


package com.blogspot.android_er.androidblegatt;  import; import android.bluetooth.BluetoothAdapter; import android.bluetooth.BluetoothManager; import android.bluetooth.le.BluetoothLeScanner; import android.content.Context; import android.content.Intent; import; import; import android.os.Bundle; import android.widget.Toast;  world shape MainActivity extends AppCompatActivity {      somebody BluetoothAdapter mBluetoothAdapter;     somebody BluetoothLeScanner mBluetoothLeScanner;      somebody boolean mScanning;      somebody static lastly int RQS_ENABLE_BLUETOOTH = 1;      @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.activity_main);          // Check if BLE is supported on the device.         if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)) {             Toast.makeText(this,                     "BLUETOOTH_LE non supported inwards this device!",                     Toast.LENGTH_SHORT).show();             finish();         }          getBluetoothAdapterAndLeScanner();          // Checks if Bluetooth is supported on the device.         if (mBluetoothAdapter == null) {             Toast.makeText(this,                     "bluetoothManager.getAdapter()==null",                     Toast.LENGTH_SHORT).show();             finish();             return;         }      }      @Override     protected void onResume() {         super.onResume();          if (!mBluetoothAdapter.isEnabled()) {             if (!mBluetoothAdapter.isEnabled()) {                 Intent enableBtIntent = novel Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);                 startActivityForResult(enableBtIntent, RQS_ENABLE_BLUETOOTH);             }         }     }      @Override     protected void onActivityResult(int requestCode, int resultCode, Intent data) {          if (requestCode == RQS_ENABLE_BLUETOOTH && resultCode == Activity.RESULT_CANCELED) {             finish();             return;         }          getBluetoothAdapterAndLeScanner();          // Checks if Bluetooth is supported on the device.         if (mBluetoothAdapter == null) {             Toast.makeText(this,                     "bluetoothManager.getAdapter()==null",                     Toast.LENGTH_SHORT).show();             finish();             return;         }          super.onActivityResult(requestCode, resultCode, data);     }      somebody void getBluetoothAdapterAndLeScanner(){         // Get BluetoothAdapter together with BluetoothLeScanner.         lastly BluetoothManager bluetoothManager =                 (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);         mBluetoothAdapter = bluetoothManager.getAdapter();         mBluetoothLeScanner = mBluetoothAdapter.getBluetoothLeScanner();          mScanning = false;     } }  

Up to here, the app uncomplicated cheque Bluetooth LE available together with enable it. In adjacent post, I volition present how to scan available Bluetooth LE devices inwards range.

– Bluetooth LE Gatt Example, scan BLE devices
– Scan specified BLE devices amongst ScanFilter
– Connect to Bluetooth LE device together with display GATT Services