Tuesday, 23 June 2020

Bluetooth Low Energy Scanning

Hi Guys !!! Hope all is well
In my previous post  I have discussed BLE Advertisement. Now I am going to discus Bluetooth Low Energy (BLE) scanning in Android.

Recap from previous post
  • Bluetooth Low Energy (BLE), available in Android 4.3 and later
  • Creates short connections between devices to transfer bursts of data
  • Bluetooth Low Energy (BLE) conserves power by remaining in sleep mode most of the time
  • It wakes up only to make advertisements and short connections
This lets BLE provide lower bandwidth and reduced power consumption compared to Classic Bluetooth.
In this tutorial, We will learn about the BluetoothAdapter.LeScanCallback class,which enables developers to turn a supported phone into a Bluetooth LE scannerwithout the need for additional hardware. You can detect all near by BLE Beacons and communicate with them.
So  below sample code detect ble advertising devices(ble beacons) and list it down there name MAC Address and RSSI value. You can download complete project from my GitHub page 
/**
 * Start the bluetooth low energy scan.
*/
private boolean startBleScan() {
boolean isSuccess = true;
// Scan filter for iBeacon.
ScanFilter.Builder builder = new ScanFilter.Builder();
builder.setManufacturerData(0x004c, new byte[] {});
SCAN_FILTER_LIST.add(builder.build());
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
mScanCallback = new ScanCallback() {
@Override
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public void onScanResult(final int callbackType,
final ScanResult result) {
// Print out the RSSI value from result
Log.d("saurabh_Tx", "onScanResult RSSI: "
+ result.getRssi());
try {
// Print out the rssi value
runOnUiThread(new Runnable() {
@Override
public void run() {
mDeviceAdapter.
update(result.getDevice(), result.getRssi(), 
result.getScanRecord());
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void onScanFailed(final int errorCode) {
Log.d("saurabh_ScanFailed", "errorCode: "
+ errorCode);
}
};
// There is no immediate value telling if scan is failed or not
if (null != mBTAdapter.getBluetoothLeScanner()) {
ScanSettings scanSettings = new ScanSettings.Builder().
setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY)
.build();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
scanSettings = new ScanSettings.Builder()
.setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY)
.setCallbackType(ScanSettings.CALLBACK_TYPE_ALL_MATCHES)
.setNumOfMatches(ScanSettings.MATCH_NUM_MAX_ADVERTISEMENT)
.build();
}
mBTAdapter.getBluetoothLeScanner().
startScan(SCAN_FILTER_LIST, scanSettings, mScanCallback);
} else {
isSuccess = false;
}
} else if (Build.VERSION.SDK_INT
>= Build.VERSION_CODES.JELLY_BEAN_MR2) {
// On some Android device, Bluetooth LE (Android Version < 4.1)
// scan is unstable. Turning off scan every 3 seconds as
// a workaround for this issue
mLeScanCallback = new BluetoothAdapter.LeScanCallback() {
@Override
public void onLeScan(final BluetoothDevice device,
final int rssi, final byte[] scanRecord) {
try {
// Print out the rssi value
Log.d("saurabh_Tx", "onScanResult RSSI: " + rssi);
runOnUiThread(new Runnable() {
@Override
public void run() {
mDeviceAdapter.
update(device, rssi, scanRecord);
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
};
isSuccess = mBTAdapter.startLeScan(mLeScanCallback);
}
return isSuccess;
}
Thanks
Saurabh 
Happy Coding !!!

No comments:

Post a Comment

Build a Custom Kernel Module for Android

Hi Guys!!!Hope you are doing well !!!. Today I will describe how you can write a custom kernel module(Hello world) for Android and load it a...