Example usage for android.bluetooth.le ScanResult getRssi

List of usage examples for android.bluetooth.le ScanResult getRssi

Introduction

In this page you can find the example usage for android.bluetooth.le ScanResult getRssi.

Prototype

public int getRssi() 

Source Link

Document

Returns the received signal strength in dBm.

Usage

From source file:com.wolkabout.hexiwear.service.DeviceDiscoveryService.java

@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private void startLolipopScan() {
    lolipopScanCallback = new ScanCallback() {
        @Override/* ww w .  j  av a2 s.  c om*/
        public void onScanResult(int callbackType, ScanResult result) {
            super.onScanResult(callbackType, result);
            final BluetoothDeviceWrapper wrapper = new BluetoothDeviceWrapper();
            wrapper.setDevice(result.getDevice());
            wrapper.setSignalStrength(result.getRssi());
            onDeviceDiscovered(wrapper);
        }
    };
    bluetoothAdapter.getBluetoothLeScanner().startScan(lolipopScanCallback);
}

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

private synchronized void startBluetoothLeScanner() {
    if (mLeScanner == null) {
        return;//w ww. ja  v a2 s  .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);
}

From source file:com.google.sample.beaconservice.MainActivityFragment.java

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    sharedPreferences = getActivity().getSharedPreferences(Constants.PREFS_NAME, 0);
    arrayList = new ArrayList<>();
    arrayAdapter = new BeaconArrayAdapter(getActivity(), R.layout.beacon_list_item, arrayList);

    scanCallback = new ScanCallback() {
        @Override//w ww  .  j  av  a  2 s . com
        public void onScanResult(int callbackType, ScanResult result) {
            ScanRecord scanRecord = result.getScanRecord();
            if (scanRecord == null) {
                Log.w(TAG, "Null ScanRecord for device " + result.getDevice().getAddress());
                return;
            }

            byte[] serviceData = scanRecord.getServiceData(EDDYSTONE_SERVICE_UUID);
            if (serviceData == null) {
                return;
            }

            // We're only interested in the UID frame time since we need the beacon ID to register.
            if (serviceData[0] != EDDYSTONE_UID_FRAME_TYPE) {
                return;
            }

            // Extract the beacon ID from the service data. Offset 0 is the frame type, 1 is the
            // Tx power, and the next 16 are the ID.
            // See https://github.com/google/eddystone/eddystone-uid for more information.
            byte[] id = Arrays.copyOfRange(serviceData, 2, 18);
            if (arrayListContainsId(arrayList, id)) {
                return;
            }

            // Draw it immediately and kick off a async request to fetch the registration status,
            // redrawing when the server returns.
            Log.i(TAG, "id " + Utils.toHexString(id) + ", rssi " + result.getRssi());

            Beacon beacon = new Beacon("EDDYSTONE", id, Beacon.STATUS_UNSPECIFIED, result.getRssi());
            insertIntoListAndFetchStatus(beacon);
        }

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

    createScanner();
}