TheBluetoothAdapter, 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.

The method is available in theBluetoothLeScanner class of the android.bluetooth.le for BLE scanning operations. To scan BLE peripheral devices, thestartScan() method is used by the app.
voidstartScan(List<ScanFilter> filters,ScanSettings settings,ScanCallback callback)
ThestartScan() method acceptsScanFilter 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.
ThestopScan() method is used to stop the peripheral scanning.
voidstopScan(ScanCallback callback)
TheScanCallback class reports the scan results using callback methodsonScanResults(),onBatchScanResults(), andonScanFailed(). TheonScanResults() 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).
voidonScanResult(int callbackType, ScanResult result)
The methods of theBluetoothDevice class can be used to access the Bluetooth address and the friendly name of the remote device.
BluetoothDevicegetDevice()
The RSSI can be used to estimate the distance/region of the device.
intgetRssi()
With theScanResult object, the methods of theScanRecord class can then be used to access the advertisement data types and payload available in the advertisement.
ScanRecordgetScanRecord()
The MCHP Transparent UART Android app handles scanning for BLE peripherals in the PeripheralScanActivity activity (PeripheralScanActivity.java). ThestartScan() method is invoked in thePerpheralScanActivity.scanLeDevice() method right when the activity starts in thePerpheralScanActivity.onStart() method and when the app user clicks on theScan button. ThestartScan() method is highlighted in the screenshot below.

In MCHP Transparent UART Android app, thestartScan() method is invoked as part of thePerpheralScanActivity.scanLeDevice() method when the app switches to a different activity, or when the user clicks onStop scan button, or when the 10 second scan timeout expires. ThestopScan() method is highlighted in the screenshot above.
In MCHP Transparent UART Android app, themScanCallback callback object is used for processing scan results when a BLE peripheral advertisement has been found as shown in the screenshot below. TheScanResult result object provides the device information and advertisement payload.

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


