Save the date! Android Dev Summit is coming to Mountain View, CA on November 7-8, 2018.

BluetoothLeScanner

public final class BluetoothLeScanner
extends Object

java.lang.Object
   ↳ android.bluetooth.le.BluetoothLeScanner


This class provides methods to perform scan related operations for Bluetooth LE devices. An application can scan for a particular type of Bluetooth LE devices using ScanFilter. It can also request different types of callbacks for delivering the result.

Use BluetoothAdapter.getBluetoothLeScanner() to get an instance of BluetoothLeScanner.

Note: Most of the scan methods here require Manifest.permission.BLUETOOTH_ADMIN permission.

See also:

Summary

Constants

String EXTRA_CALLBACK_TYPE

Optional extra indicating the callback type, which will be one of CALLBACK_TYPE_* constants in ScanSettings.

String EXTRA_ERROR_CODE

Optional extra indicating the error code, if any.

String EXTRA_LIST_SCAN_RESULT

Extra containing a list of ScanResults.

Public methods

void flushPendingScanResults(ScanCallback callback)

Flush pending batch scan results stored in Bluetooth controller.

int startScan(List<ScanFilter> filters, ScanSettings settings, PendingIntent callbackIntent)

Start Bluetooth LE scan using a PendingIntent.

void startScan(ScanCallback callback)

Start Bluetooth LE scan with default parameters and no filters.

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

Start Bluetooth LE scan.

void stopScan(ScanCallback callback)

Stops an ongoing Bluetooth LE scan.

void stopScan(PendingIntent callbackIntent)

Stops an ongoing Bluetooth LE scan started using a PendingIntent.

Inherited methods

Constants

EXTRA_CALLBACK_TYPE

added in API level 26
public static final String EXTRA_CALLBACK_TYPE

Optional extra indicating the callback type, which will be one of CALLBACK_TYPE_* constants in ScanSettings.

Constant Value: "android.bluetooth.le.extra.CALLBACK_TYPE"

EXTRA_ERROR_CODE

added in API level 26
public static final String EXTRA_ERROR_CODE

Optional extra indicating the error code, if any. The error code will be one of the SCAN_FAILED_* codes in ScanCallback.

Constant Value: "android.bluetooth.le.extra.ERROR_CODE"

EXTRA_LIST_SCAN_RESULT

added in API level 26
public static final String EXTRA_LIST_SCAN_RESULT

Extra containing a list of ScanResults. It can have one or more results if there was no error. In case of error, EXTRA_ERROR_CODE will contain the error code and this extra will not be available.

Constant Value: "android.bluetooth.le.extra.LIST_SCAN_RESULT"

Public methods

flushPendingScanResults

added in API level 21
public void flushPendingScanResults (ScanCallback callback)

Flush pending batch scan results stored in Bluetooth controller. This will return Bluetooth LE scan results batched on bluetooth controller. Returns immediately, batch scan results data will be delivered through the callback.

Parameters
callback ScanCallback: Callback of the Bluetooth LE Scan, it has to be the same instance as the one used to start scan.

startScan

added in API level 26
public int startScan (List<ScanFilter> filters, 
                ScanSettings settings, 
                PendingIntent callbackIntent)

Start Bluetooth LE scan using a PendingIntent. The scan results will be delivered via the PendingIntent. Use this method of scanning if your process is not always running and it should be started when scan results are available.

An app must hold ACCESS_COARSE_LOCATION or ACCESS_FINE_LOCATION permission in order to get results.

When the PendingIntent is delivered, the Intent passed to the receiver or activity will contain one or more of the extras EXTRA_CALLBACK_TYPE, EXTRA_ERROR_CODE and EXTRA_LIST_SCAN_RESULT to indicate the result of the scan.

Requires the BLUETOOTH_ADMIN permission.

Parameters
filters List: Optional list of ScanFilters for finding exact BLE devices.

This value may be null.

settings ScanSettings: Optional settings for the scan.

This value may be null.

callbackIntent PendingIntent: The PendingIntent to deliver the result to.

This value must never be null.

Returns
int Returns 0 for success or an error code from ScanCallback if the scan request could not be sent.

startScan

added in API level 21
public void startScan (ScanCallback callback)

Start Bluetooth LE scan with default parameters and no filters. The scan results will be delivered through callback. For unfiltered scans, scanning is stopped on screen off to save power. Scanning is resumed when screen is turned on again. To avoid this, use startScan(List, ScanSettings, ScanCallback) with desired ScanFilter.

An app must hold ACCESS_COARSE_LOCATION or ACCESS_FINE_LOCATION permission in order to get results.

Requires the BLUETOOTH_ADMIN permission.

Parameters
callback ScanCallback: Callback used to deliver scan results.

Throws
IllegalArgumentException If callback is null.

startScan

added in API level 21
public void startScan (List<ScanFilter> filters, 
                ScanSettings settings, 
                ScanCallback callback)

Start Bluetooth LE scan. The scan results will be delivered through callback. For unfiltered scans, scanning is stopped on screen off to save power. Scanning is resumed when screen is turned on again. To avoid this, do filetered scanning by using proper ScanFilter.

An app must hold ACCESS_COARSE_LOCATION or ACCESS_FINE_LOCATION permission in order to get results.

Requires the BLUETOOTH_ADMIN permission.

Parameters
filters List: ScanFilters for finding exact BLE devices.

settings ScanSettings: Settings for the scan.

callback ScanCallback: Callback used to deliver scan results.

Throws
IllegalArgumentException If settings or callback is null.

stopScan

added in API level 21
public void stopScan (ScanCallback callback)

Stops an ongoing Bluetooth LE scan.

Requires the BLUETOOTH_ADMIN permission.

stopScan

added in API level 26
public void stopScan (PendingIntent callbackIntent)

Stops an ongoing Bluetooth LE scan started using a PendingIntent.

Requires the BLUETOOTH_ADMIN permission.

Parameters
callbackIntent PendingIntent: The PendingIntent that was used to start the scan.