Scanning for BLE Devices

Last modified by Microchip on 2023/11/09 08:53

The BluetoothAdapter, which is a local Android device adapter, is the key to performing Bluetooth® tasks for Bluetooth LE and Classic devices. The BluetoothAdapter is used to access BLE scanning and to listen for connection requests from other devices.

BluetoothAdapter used to access BLE scanning

The method is available in the BluetoothLeScanner class of the android.bluetooth.le for BLE scanning operations. To scan BLE peripheral devices, the startScan() method is used by the app.

void startScan(List<ScanFilter> filters, ScanSettings settings, ScanCallback callback)

The startScan() method accepts ScanFilter to filter out the scan results with the exact peripheral devices that match the Advertisement Data Type values set for the scan filter. The scan settings parameter can be used to set the intensity of scanning, match, and result callback requirements. The scan callback parameter specifies the scan callback class used to deliver the scan results. The callback must be implemented for processing the results of scanning.

Since scanning is battery-intensive, the following actions are recommended:

  • After finding the desired peripheral device, stop scanning.
  • Set a time limit on the scan. Note that the peripheral device that was previously available may have moved out of range.

The stopScan() method is used to stop the peripheral scanning.

void stopScan(ScanCallback callback)

The ScanCallback class reports the scan results using callback methods onScanResults(), onBatchScanResults() and onScanFailed(). The onScanResults() method reports the scan results when a BLE peripheral advertisement has been found. The callback type indicates the condition type that caused the callback and the scan result provides the Bluetooth device information, the advertisement payload, or the scan record, as well as the Received Signal Strength Indicator (RSSI).

void onScanResult(int callbackType, ScanResult result)

The methods of the BluetoothDevice class can be used to access the Bluetooth address and the friendly name of the remote device.

BluetoothDevice getDevice()

The RSSI can be used to estimate the distance/region of the device.

int getRssi()

With the ScanResult object, the methods of the ScanRecord class can then be used to access the advertisement data types and payload available in the advertisement.

ScanRecord getScanRecord()

The MCHP Transparent UART Android app handles scanning for BLE peripherals in the PeripheralScanActivity activity (PeripheralScanActivity.java). The startScan() method is invoked in the PerpheralScanActivity.scanLeDevice() method right when the activity starts in the PerpheralScanActivity.onStart() method and when the app user clicks on the Scan button. The startScan() method is highlighted in the screenshot below.

startScan() method is highlighted

In MCHP Transparent UART Android app, the startScan() method is invoked as part of the PerpheralScanActivity.scanLeDevice() method when the app switches to a different activity, or when the user clicks on Stop scan button, or when the 10 second scan timeout expires. The stopScan() method is highlighted in the screenshot above.

In MCHP Transparent UART Android app, the mScanCallback callback object is used for processing scan results when a BLE peripheral advertisement has been found as shown in the screenshot below. The ScanResult result object provides the device information and advertisement payload.

the mScanCallback callback object

The screenshot below shows the PeripheralScanActivity view of the MCHP Transparent UART Android app. When the app starts, the PeripheralScanActivity View automatically starts scanning for the nearby BLE peripherals for 10 seconds. All the BLE peripherals found are listed in the Peripherals List View. The Scan button can be used to scan for the BLE peripherals again. The user can click on the desired peripheral from the list to start a connection to it.

PeripheralScanActivity view of the MCHP Transparent UART Android app