Example usage for android.bluetooth.le ScanRecord getManufacturerSpecificData

List of usage examples for android.bluetooth.le ScanRecord getManufacturerSpecificData

Introduction

In this page you can find the example usage for android.bluetooth.le ScanRecord getManufacturerSpecificData.

Prototype

@Nullable
public byte[] getManufacturerSpecificData(int manufacturerId) 

Source Link

Document

Returns the manufacturer specific data associated with the manufacturer id.

Usage

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

private void updateScanning() {
    if (isScanning && scanCancellationThreads.size() == 0) {
        isScanning = false;//from ww w. j  a v a  2 s  .co  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);
    }
}