Example usage for android.bluetooth.le ScanFilter matches

List of usage examples for android.bluetooth.le ScanFilter matches

Introduction

In this page you can find the example usage for android.bluetooth.le ScanFilter matches.

Prototype

public boolean matches(ScanResult scanResult) 

Source Link

Document

Check if the scan filter matches a scanResult .

Usage

From source file:io.v.android.impl.google.discovery.plugins.ble.Driver.java

private synchronized void startBluetoothLeScanner() {
    if (mLeScanner == null) {
        return;//from  www  . j av  a 2s  .co  m
    }

    ImmutableList.Builder<ScanFilter> builder = new ImmutableList.Builder();
    for (UUID uuid : mScanUuids) {
        builder.add(new ScanFilter.Builder().setServiceUuid(new ParcelUuid(uuid)).build());
    }
    List<ScanFilter> filters = builder.build();

    ScanSettings settings = new ScanSettings.Builder().setScanMode(ScanSettings.SCAN_MODE_BALANCED).build();

    // ScanFilter doesn't work with startScan() if there are too many - more than 63bits - ignore
    // bits. So we call startScan() without a scan filter for base/mask uuids and match scan results
    // against it.
    final ScanFilter matcher = new ScanFilter.Builder().setServiceUuid(mScanBaseUuid, mScanMaskUuid).build();

    mLeScanCallback = new ScanCallback() {
        @Override
        public void onScanResult(int callbackType, ScanResult result) {
            if (callbackType == ScanSettings.CALLBACK_TYPE_MATCH_LOST) {
                // This callback will never be called with this callback type, since the
                // scan setting is for CALLBACK_TYPE_ALL_MATCHES. But just for safety.
                return;
            }
            if (!matcher.matches(result)) {
                return;
            }
            BluetoothDevice device = result.getDevice();
            synchronized (Driver.this) {
                if (mScanSeens != null && mScanSeens.put(device, result.getRssi()) == null) {
                    mGattReader.readDevice(device);
                }
            }
        }

        @Override
        public void onScanFailed(int errorCode) {
            Log.e(TAG, "startScan failed: " + errorCode);
        }
    };

    mLeScanner.startScan(filters, settings, mLeScanCallback);
}