Example usage for android.bluetooth.le ScanSettings CALLBACK_TYPE_MATCH_LOST

List of usage examples for android.bluetooth.le ScanSettings CALLBACK_TYPE_MATCH_LOST

Introduction

In this page you can find the example usage for android.bluetooth.le ScanSettings CALLBACK_TYPE_MATCH_LOST.

Prototype

int CALLBACK_TYPE_MATCH_LOST

To view the source code for android.bluetooth.le ScanSettings CALLBACK_TYPE_MATCH_LOST.

Click Source Link

Document

Receive a callback when advertisements are no longer received from a device that has been previously reported by a first match callback.

Usage

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

private synchronized void startBluetoothLeScanner() {
    if (mLeScanner == null) {
        return;// w w  w  .  j  a v a 2  s.c  o  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);
}