Example usage for android.bluetooth.le ScanSettings SCAN_MODE_BALANCED

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

Introduction

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

Prototype

int SCAN_MODE_BALANCED

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

Click Source Link

Document

Perform Bluetooth LE scan in balanced power mode.

Usage

From source file:au.com.smarttrace.beacons.BluetoothService.java

/** Init bluetooth adapter */
private void initBluetooth() {
    if (btManager == null)
        btManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
    btAdapter = btManager.getAdapter();/*from  w  w w. j  a  v a 2s. c o m*/

    ScanSettings.Builder builder = new ScanSettings.Builder().setScanMode(ScanSettings.SCAN_MODE_BALANCED)
            .setReportDelay(0);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
        builder.setCallbackType(ScanSettings.CALLBACK_TYPE_ALL_MATCHES);
    btSettings = builder.build();

    btFilters = new ArrayList<>();
}

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  2s.  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);
}

From source file:io.v.android.libs.discovery.ble.BlePlugin.java

private void updateScanning() {
    if (isScanning && scanCancellationThreads.size() == 0) {
        isScanning = false;//from w w w. j  ava2  s . c o  m
        bluetoothLeScanner.stopScan(scanCallback);
        return;
    }

    if (!isScanning && scanCancellationThreads.size() > 0) {
        isScanning = true;
        ScanFilter.Builder builder = new ScanFilter.Builder();
        byte[] manufacturerData = {};
        byte[] manufacturerMask = {};

        builder.setManufacturerData(1001, manufacturerData, manufacturerMask);
        final List<ScanFilter> scanFilter = new ArrayList<>();
        scanFilter.add(builder.build());

        scanCallback = new ScanCallback() {
            @Override
            public void onScanResult(int callbackType, ScanResult result) {
                // in L the only value for callbackType is CALLBACK_TYPE_ALL_MATCHES, so
                // we don't look at its value.
                ScanRecord record = result.getScanRecord();
                // Use 1001 to denote that this is a Vanadium device.  We picked an id that is
                // currently not in use.
                byte[] data = record.getManufacturerSpecificData(1001);
                ByteBuffer buffer = ByteBuffer.wrap(data);
                final long hash = buffer.getLong();
                final String deviceId = result.getDevice().getAddress();
                if (cachedDevices.haveSeenHash(hash, deviceId)) {
                    return;
                }
                synchronized (scannerLock) {
                    if (pendingCalls.contains(deviceId)) {
                        Log.d("vanadium", "not connecting to " + deviceId + " because of pending connection");
                        return;
                    }
                    pendingCalls.add(deviceId);
                }
                BluetoothGattClientCallback.Callback ccb = new BluetoothGattClientCallback.Callback() {
                    @Override
                    public void handle(Map<UUID, Map<UUID, byte[]>> services) {
                        Set<Advertisement> advs = new HashSet<>();
                        for (Map.Entry<UUID, Map<UUID, byte[]>> entry : services.entrySet()) {
                            try {
                                Advertisement adv = BleAdvertisementConverter
                                        .bleAttrToVAdvertisement(entry.getValue());
                                advs.add(adv);
                            } catch (IOException e) {
                                Log.e("vanadium", "Failed to convert advertisement" + e);
                            }
                        }
                        cachedDevices.saveDevice(hash, advs, deviceId);
                        synchronized (scannerLock) {
                            pendingCalls.remove(deviceId);
                        }
                        bluetoothLeScanner.startScan(scanFilter,
                                new ScanSettings.Builder().setScanMode(ScanSettings.SCAN_MODE_BALANCED).build(),
                                scanCallback);
                    }
                };
                BluetoothGattClientCallback cb = new BluetoothGattClientCallback(ccb);
                bluetoothLeScanner.stopScan(scanCallback);
                Log.d("vanadium", "connecting to " + result.getDevice());
                result.getDevice().connectGatt(androidContext, false, cb);
            }

            @Override
            public void onBatchScanResults(List<ScanResult> results) {
            }

            @Override
            public void onScanFailed(int errorCode) {
            }
        };
        bluetoothLeScanner.startScan(scanFilter,
                new ScanSettings.Builder().setScanMode(ScanSettings.SCAN_MODE_BALANCED).build(), scanCallback);
    }
}