FileDocCategorySizeDatePackage
BluetoothLeScanner.javaAPI DocAndroid 5.1 API14779Thu Mar 12 22:22:10 GMT 2015android.bluetooth.le

BluetoothLeScanner

public final class BluetoothLeScanner extends Object
This class provides methods to perform scan related operations for Bluetooth LE devices. An application can scan for a particular type of Bluetotoh LE devices using {@link ScanFilter}. It can also request different types of callbacks for delivering the result.

Use {@link BluetoothAdapter#getBluetoothLeScanner()} to get an instance of {@link BluetoothLeScanner}.

Note: Most of the scan methods here require {@link android.Manifest.permission#BLUETOOTH_ADMIN} permission.

see
ScanFilter

Fields Summary
private static final String
TAG
private static final boolean
DBG
private static final boolean
VDBG
private final android.bluetooth.IBluetoothManager
mBluetoothManager
private final android.os.Handler
mHandler
private android.bluetooth.BluetoothAdapter
mBluetoothAdapter
private final Map
mLeScanClients
Constructors Summary
public BluetoothLeScanner(android.bluetooth.IBluetoothManager bluetoothManager)
Use {@link BluetoothAdapter#getBluetoothLeScanner()} instead.

param
bluetoothManager BluetoothManager that conducts overall Bluetooth Management.
hide


                      
       
        mBluetoothManager = bluetoothManager;
        mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
        mHandler = new Handler(Looper.getMainLooper());
        mLeScanClients = new HashMap<ScanCallback, BleScanCallbackWrapper>();
    
Methods Summary
public voidcleanup()
Cleans up scan clients. Should be called when bluetooth is down.

hide

        mLeScanClients.clear();
    
public voidflushPendingScanResults(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 {@code callback}.

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

        BluetoothLeUtils.checkAdapterStateOn(mBluetoothAdapter);
        if (callback == null) {
            throw new IllegalArgumentException("callback cannot be null!");
        }
        synchronized (mLeScanClients) {
            BleScanCallbackWrapper wrapper = mLeScanClients.get(callback);
            if (wrapper == null) {
                return;
            }
            wrapper.flushPendingBatchResults();
        }
    
private booleanisSettingsConfigAllowedForScan(ScanSettings settings)

        if (mBluetoothAdapter.isOffloadedFilteringSupported()) {
            return true;
        }
        final int callbackType = settings.getCallbackType();
        // Only support regular scan if no offloaded filter support.
        if (callbackType == ScanSettings.CALLBACK_TYPE_ALL_MATCHES
                && settings.getReportDelayMillis() == 0) {
            return true;
        }
        return false;
    
private voidpostCallbackError(ScanCallback callback, int errorCode)

        mHandler.post(new Runnable() {
            @Override
            public void run() {
                callback.onScanFailed(errorCode);
            }
        });
    
public voidstartScan(ScanCallback callback)
Start Bluetooth LE scan with default parameters and no filters. The scan results will be delivered through {@code callback}.

Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN} permission.

param
callback Callback used to deliver scan results.
throws
IllegalArgumentException If {@code callback} is null.

        if (callback == null) {
            throw new IllegalArgumentException("callback is null");
        }
        startScan(null, new ScanSettings.Builder().build(), callback);
    
public voidstartScan(java.util.List filters, ScanSettings settings, ScanCallback callback)
Start Bluetooth LE scan. The scan results will be delivered through {@code callback}.

Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN} permission.

param
filters {@link ScanFilter}s for finding exact BLE devices.
param
settings Settings for the scan.
param
callback Callback used to deliver scan results.
throws
IllegalArgumentException If {@code settings} or {@code callback} is null.

        startScan(filters, settings, callback, null);
    
private voidstartScan(java.util.List filters, ScanSettings settings, ScanCallback callback, java.util.List resultStorages)

        BluetoothLeUtils.checkAdapterStateOn(mBluetoothAdapter);
        if (settings == null || callback == null) {
            throw new IllegalArgumentException("settings or callback is null");
        }
        synchronized (mLeScanClients) {
            if (mLeScanClients.containsKey(callback)) {
                postCallbackError(callback, ScanCallback.SCAN_FAILED_ALREADY_STARTED);
                return;
            }
            IBluetoothGatt gatt;
            try {
                gatt = mBluetoothManager.getBluetoothGatt();
            } catch (RemoteException e) {
                gatt = null;
            }
            if (gatt == null) {
                postCallbackError(callback, ScanCallback.SCAN_FAILED_INTERNAL_ERROR);
                return;
            }
            if (!isSettingsConfigAllowedForScan(settings)) {
                postCallbackError(callback,
                        ScanCallback.SCAN_FAILED_FEATURE_UNSUPPORTED);
                return;
            }
            BleScanCallbackWrapper wrapper = new BleScanCallbackWrapper(gatt, filters,
                    settings, callback, resultStorages);
            wrapper.startRegisteration();
        }
    
public voidstartTruncatedScan(java.util.List truncatedFilters, ScanSettings settings, ScanCallback callback)
Start truncated scan.

hide

        int filterSize = truncatedFilters.size();
        List<ScanFilter> scanFilters = new ArrayList<ScanFilter>(filterSize);
        List<List<ResultStorageDescriptor>> scanStorages =
                new ArrayList<List<ResultStorageDescriptor>>(filterSize);
        for (TruncatedFilter filter : truncatedFilters) {
            scanFilters.add(filter.getFilter());
            scanStorages.add(filter.getStorageDescriptors());
        }
        startScan(scanFilters, settings, callback, scanStorages);
    
public voidstopScan(ScanCallback callback)
Stops an ongoing Bluetooth LE scan.

Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN} permission.

param
callback

        BluetoothLeUtils.checkAdapterStateOn(mBluetoothAdapter);
        synchronized (mLeScanClients) {
            BleScanCallbackWrapper wrapper = mLeScanClients.remove(callback);
            if (wrapper == null) {
                if (DBG) Log.d(TAG, "could not find callback wrapper");
                return;
            }
            wrapper.stopLeScan();
        }