Scanning for BLE Devices

The BluetoothAdapter, which is a local Android device adapter, is the key to perform 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.

adapter.png

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 which 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 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.

scan.png

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.

scancallback.png

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.

scanview.png
© 2019 Microchip Technology, Inc.
Notice: ARM and Cortex are the registered trademarks of ARM Limited in the EU and other countries.
Information contained on this site regarding device applications and the like is provided only for your convenience and may be superseded by updates. It is your responsibility to ensure that your application meets with your specifications. MICROCHIP MAKES NO REPRESENTATIONS OR WARRANTIES OF ANY KIND WHETHER EXPRESS OR IMPLIED, WRITTEN OR ORAL, STATUTORY OR OTHERWISE, RELATED TO THE INFORMATION, INCLUDING BUT NOT LIMITED TO ITS CONDITION, QUALITY, PERFORMANCE, MERCHANTABILITY OR FITNESS FOR PURPOSE. Microchip disclaims all liability arising from this information and its use. Use of Microchip devices in life support and/or safety applications is entirely at the buyer's risk, and the buyer agrees to defend, indemnify and hold harmless Microchip from any and all damages, claims, suits, or expenses resulting from such use. No licenses are conveyed, implicitly or otherwise, under any Microchip intellectual property rights.