no.nordicsemi.android.support.v18.scanner.ScanResult Java Examples

The following examples show how to use no.nordicsemi.android.support.v18.scanner.ScanResult. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: NrfMeshRepository.java    From Android-nRF-Mesh-Library with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
public void onScanResult(final int callbackType, final ScanResult result) {
    //In order to connectToProxy to the correct device, the hash advertised in the advertisement data should be matched.
    //This is to make sure we connectToProxy to the same device as device addresses could change after provisioning.
    final ScanRecord scanRecord = result.getScanRecord();
    if (scanRecord != null) {
        final byte[] serviceData = Utils.getServiceData(result, MESH_PROXY_UUID);
        if (serviceData != null) {
            if (mMeshManagerApi.isAdvertisedWithNodeIdentity(serviceData)) {
                final ProvisionedMeshNode node = mProvisionedMeshNode;
                if (mMeshManagerApi.nodeIdentityMatches(node, serviceData)) {
                    stopScan();
                    mConnectionState.postValue("Provisioned node found");
                    onProvisionedDeviceFound(node, new ExtendedBluetoothDevice(result));
                }
            }
        }
    }
}
 
Example #2
Source File: BleScanner.java    From Bluefruit_LE_Connect_Android_V2 with MIT License 6 votes vote down vote up
private void peripheralDiscovered(@NonNull ScanResult result) {
    // Check that the device was not previously found
    final String resultAddress = result.getDevice().getAddress();

    int index = 0;
    boolean found = false;
    while (index < mPeripheralScanResults.size() && !found) {
        if (mPeripheralScanResults.get(index).getIdentifier().equals(resultAddress)) {
            found = true;
        } else {
            index++;
        }
    }

    if (found) {
        // Replace existing record
        mPeripheralScanResults.get(index).replaceScanResult(result);
    } else {
        // Add mew record
        BlePeripheral blePeripheral = new BlePeripheral(result);
        mPeripheralScanResults.add(blePeripheral);
    }
}
 
Example #3
Source File: ScannerLiveData.java    From Android-nRF-Mesh-Library with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
void deviceDiscovered(final ScanResult result, final MeshBeacon beacon) {
    ExtendedBluetoothDevice device;

    final int index = indexOf(result);
    if (index == -1) {
        device = new ExtendedBluetoothDevice(result, beacon);
        mDevices.add(device);
        mUpdatedDeviceIndex = null;
    } else {
        device = mDevices.get(index);
        mUpdatedDeviceIndex = index;
    }
    // Update RSSI and name
    device.setRssi(result.getRssi());
    device.setName(result.getScanRecord().getDeviceName());

    postValue(this);
}
 
Example #4
Source File: ScannerLiveData.java    From Android-nRF-Mesh-Library with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
void deviceDiscovered(final ScanResult result) {
    ExtendedBluetoothDevice device;

    final int index = indexOf(result);
    if (index == -1) {
        device = new ExtendedBluetoothDevice(result);
        mDevices.add(device);
        mUpdatedDeviceIndex = null;
    } else {
        device = mDevices.get(index);
        mUpdatedDeviceIndex = index;
    }
    // Update RSSI and name
    device.setRssi(result.getRssi());
    device.setName(result.getScanRecord().getDeviceName());

    postValue(this);
}
 
Example #5
Source File: DevicesLiveData.java    From Android-nRF-Blinky with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
synchronized boolean deviceDiscovered(@NonNull final ScanResult result) {
DiscoveredBluetoothDevice device;

// Check if it's a new device.
final int index = indexOf(result);
if (index == -1) {
	device = new DiscoveredBluetoothDevice(result);
	devices.add(device);
} else {
	device = devices.get(index);
}

// Update RSSI and name.
device.update(result);

// Return true if the device was on the filtered list or is to be added.
return (filteredDevices != null && filteredDevices.contains(device))
		|| (matchesUuidFilter(result) && matchesNearbyFilter(device.getHighestRssi()));
  }
 
Example #6
Source File: ScannerViewModel.java    From Android-nRF-Blinky with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
public void onBatchScanResults(@NonNull final List<ScanResult> results) {
	// This callback will be called only if the report delay set above is greater then 0.

	// If the packet has been obtained while Location was disabled, mark Location as not required
	if (Utils.isLocationRequired(getApplication()) && !Utils.isLocationEnabled(getApplication()))
		Utils.markLocationNotRequired(getApplication());

	boolean atLeastOneMatchedFilter = false;
	for (final ScanResult result : results)
		atLeastOneMatchedFilter = devicesLiveData.deviceDiscovered(result) || atLeastOneMatchedFilter;
	if (atLeastOneMatchedFilter) {
		devicesLiveData.applyFilter();
		scannerStateLiveData.recordFound();
	}
}
 
Example #7
Source File: ScannerViewModel.java    From mcumgr-android with Apache License 2.0 6 votes vote down vote up
@Override
public void onBatchScanResults(@NotNull final List<ScanResult> results) {
    // This callback will be called only if the report delay set above is greater then 0.

    // If the packet has been obtained while Location was disabled, mark Location as not required
    if (Utils.isLocationRequired(getApplication()) && !Utils.isLocationEnabled(getApplication()))
        Utils.markLocationNotRequired(getApplication());

    boolean atLeastOneMatchedFilter = false;
    for (final ScanResult result : results)
        atLeastOneMatchedFilter =
                (!isNoise(result) && mDevicesLiveData.deviceDiscovered(result))
                        || atLeastOneMatchedFilter;
    if (atLeastOneMatchedFilter) {
        mDevicesLiveData.applyFilter();
        mScannerStateLiveData.recordFound();
    }
}
 
Example #8
Source File: ScannerViewModel.java    From mcumgr-android with Apache License 2.0 6 votes vote down vote up
/**
 * This method returns true if the scan result may be considered as noise.
 * This is to make the device list on the scanner screen shorter.
 * <p>
 * This implementation considers as noise devices that:
 * <ul>
 * <li>Are not connectable (Android Oreo or newer only),</li>
 * <li>Are far away (RSSI < -80),</li>
 * <li>Advertise as beacons (iBeacons, Nordic Beacons, Microsoft Advertising Beacons,
 * Eddystone,</li>
 * <li>Advertise with AirDrop footprint,</li>
 * <li>Advertise as Bluetooth Mesh devices.</li>
 * </ul>
 * Noise devices will no the shown on the scanner screen even with all filters disabled.
 *
 * @param result the scan result.
 * @return true, if the device may be dismissed, false otherwise.
 */
@SuppressWarnings({"BooleanMethodIsAlwaysInverted", "RedundantIfStatement"})
private boolean isNoise(@NonNull final ScanResult result) {
    // Do not show non-connectable devices.
    // Only Android Oreo or newer can say if a device is connectable. On older Android versions
    // the Support Scanner Library assumes all devices are connectable (compatibility mode).
    if (!result.isConnectable())
        return true;

    // Very distant devices are noise.
    if (result.getRssi() < -80)
        return true;

    if (FilterUtils.isBeacon(result))
        return true;

    if (FilterUtils.isAirDrop(result))
        return true;

    if (FilterUtils.isMeshDevice(result))
        return true;

    return false;
}
 
Example #9
Source File: DevicesLiveData.java    From mcumgr-android with Apache License 2.0 6 votes vote down vote up
synchronized boolean deviceDiscovered(final ScanResult result) {
    DiscoveredBluetoothDevice device;

    // Check if it's a new device.
    final int index = indexOf(result);
    if (index == -1) {
        device = new DiscoveredBluetoothDevice(result);
        mDevices.add(device);
    } else {
        device = mDevices.get(index);
    }

    // Update RSSI and name.
    device.update(result);

    // Return true if the device was on the filtered list or is to be added.
    return (mFilteredDevices != null && mFilteredDevices.contains(device))
            || (matchesUuidFilter(result) && matchesNearbyFilter(device.getHighestRssi()));
}
 
Example #10
Source File: FilterUtils.java    From mcumgr-android with Apache License 2.0 5 votes vote down vote up
public static boolean isAirDrop(final ScanResult result) {
    if (result != null && result.getScanRecord() != null) {
        final ScanRecord record = result.getScanRecord();

        // iPhones and iMacs advertise with AirDrop packets
        final byte[] appleData = record.getManufacturerSpecificData(COMPANY_ID_APPLE);
        return appleData != null && appleData.length > 1 && appleData[0] == 0x10;
    }
    return false;
}
 
Example #11
Source File: DiscoveredBluetoothDevice.java    From mcumgr-android with Apache License 2.0 5 votes vote down vote up
private DiscoveredBluetoothDevice(final Parcel in) {
    device = in.readParcelable(BluetoothDevice.class.getClassLoader());
    lastScanResult = in.readParcelable(ScanResult.class.getClassLoader());
    name = in.readString();
    rssi = in.readInt();
    previousRssi = in.readInt();
    highestRssi = in.readInt();
}
 
Example #12
Source File: DiscoveredBluetoothDevice.java    From mcumgr-android with Apache License 2.0 5 votes vote down vote up
/**
 * Updates the device values based on the scan result.
 *
 * @param scanResult the new received scan result.
 */
public void update(final ScanResult scanResult) {
    lastScanResult = scanResult;
    name = scanResult.getScanRecord() != null ?
            scanResult.getScanRecord().getDeviceName() : null;
    previousRssi = rssi;
    rssi = scanResult.getRssi();
    if (highestRssi < rssi)
        highestRssi = rssi;
}
 
Example #13
Source File: ExtendedBluetoothDevice.java    From Android-nRF-Mesh-Library with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public ExtendedBluetoothDevice(final ScanResult scanResult, final MeshBeacon beacon) {
    this.scanResult = scanResult;
    this.device = scanResult.getDevice();
    final ScanRecord scanRecord = scanResult.getScanRecord();
    if(scanRecord != null) {
        this.name = scanRecord.getDeviceName();
    }
    this.rssi = scanResult.getRssi();
    this.beacon = beacon;
}
 
Example #14
Source File: FilterUtils.java    From mcumgr-android with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("RedundantIfStatement")
public static boolean isBeacon(final ScanResult result) {
    if (result != null && result.getScanRecord() != null) {
        final ScanRecord record = result.getScanRecord();

        final byte[] appleData = record.getManufacturerSpecificData(COMPANY_ID_APPLE);
        if (appleData != null) {
            // iBeacons
            if (appleData.length == 23 && appleData[0] == 0x02 && appleData[1] == 0x15)
                return true;
        }

        final byte[] nordicData = record.getManufacturerSpecificData(COMPANY_ID_NORDIC_SEMI);
        if (nordicData != null) {
            // Nordic Beacons
            if (nordicData.length == 23 && nordicData[0] == 0x02 && nordicData[1] == 0x15)
                return true;
        }

        final byte[] microsoftData = record.getManufacturerSpecificData(COMPANY_ID_MICROSOFT);
        if (microsoftData != null) {
            // Microsoft Advertising Beacon
            if (microsoftData[0] == 0x01) // Scenario Type = Advertising Beacon
                return true;
        }

        // Eddystone
        final byte[] eddystoneData = record.getServiceData(EDDYSTONE_UUID);
        if (eddystoneData != null)
            return true;
    }

    return false;
}
 
Example #15
Source File: ScannerRepository.java    From Android-nRF-Mesh-Library with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public void onScanResult(final int callbackType, @NonNull final ScanResult result) {
    try {
        if (mFilterUuid.equals(BleMeshManager.MESH_PROVISIONING_UUID)) {
            // If the packet has been obtained while Location was disabled, mark Location as not required
            if (Utils.isLocationRequired(mContext) && !Utils.isLocationEnabled(mContext))
                Utils.markLocationNotRequired(mContext);

            updateScannerLiveData(result);
        } else if (mFilterUuid.equals(BleMeshManager.MESH_PROXY_UUID)) {
            final byte[] serviceData = Utils.getServiceData(result, BleMeshManager.MESH_PROXY_UUID);
            if (mMeshManagerApi != null) {
                if (mMeshManagerApi.isAdvertisingWithNetworkIdentity(serviceData)) {
                    if (mMeshManagerApi.networkIdMatches(mNetworkId, serviceData)) {
                        updateScannerLiveData(result);
                    }
                } else if (mMeshManagerApi.isAdvertisedWithNodeIdentity(serviceData)) {
                    if (checkIfNodeIdentityMatches(serviceData)) {
                        updateScannerLiveData(result);
                    }
                }
            }
        }
    } catch (Exception ex) {
        Log.e(TAG, "Error: " + ex.getMessage());
    }
}
 
Example #16
Source File: ExtendedBluetoothDevice.java    From Android-nRF-Mesh-Library with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public ExtendedBluetoothDevice(final ScanResult scanResult) {
    this.scanResult = scanResult;
    this.device = scanResult.getDevice();
    final ScanRecord scanRecord = scanResult.getScanRecord();
    if(scanRecord != null) {
        this.name = scanRecord.getDeviceName();
    }
    this.rssi = scanResult.getRssi();
}
 
Example #17
Source File: ExtendedBluetoothDevice.java    From Android-nRF-Mesh-Library with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
protected ExtendedBluetoothDevice(Parcel in) {
    device = in.readParcelable(BluetoothDevice.class.getClassLoader());
    scanResult = in.readParcelable(ScanResult.class.getClassLoader());
    name = in.readString();
    rssi = in.readInt();
    beacon = in.readParcelable(MeshBeacon.class.getClassLoader());
}
 
Example #18
Source File: DevicesLiveData.java    From mcumgr-android with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("SimplifiableIfStatement")
private boolean matchesUuidFilter(final ScanResult result) {
    if (!mFilterUuidRequired)
        return true;

    final ScanRecord record = result.getScanRecord();
    if (record == null)
        return false;

    final List<ParcelUuid> uuids = record.getServiceUuids();
    if (uuids == null)
        return false;

    return uuids.contains(FILTER_UUID);
}
 
Example #19
Source File: DeviceListAdapter.java    From Android-nRF-Beacon-for-Eddystone with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * If such device exists on the bonded device list, this method does nothing. If not then the device is updated (rssi value) or added.
 * 
 * @param results scan results
 */
public void update(final List<ScanResult> results) {
	for (final ScanResult result : results) {
		final ExtendedBluetoothDevice device = findDevice(result);
		if (device == null) {
			mDevices.add(new ExtendedBluetoothDevice(result));
		} else {
			device.name = result.getScanRecord() != null ? result.getScanRecord().getDeviceName() : null;
			device.rssi = result.getRssi();
		}
	}
	notifyDataSetChanged();
}
 
Example #20
Source File: DeviceListAdapter.java    From Android-nRF-Toolbox with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Updates the list of not bonded devices.
 * @param results list of results from the scanner
 */
public void update(@NonNull final List<ScanResult> results) {
	for (final ScanResult result : results) {
		final ExtendedBluetoothDevice device = findDevice(result);
		if (device == null) {
			listValues.add(new ExtendedBluetoothDevice(result));
		} else {
			device.name = result.getScanRecord() != null ? result.getScanRecord().getDeviceName() : null;
			device.rssi = result.getRssi();
		}
	}
	notifyDataSetChanged();
}
 
Example #21
Source File: DevicesAdapter.java    From Android-nRF-Toolbox with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public void onBatchScanResults(final List<ScanResult> results) {
	final int size = devices.size();
	for (final ScanResult result : results) {
		final BluetoothDevice device = result.getDevice();
		if (!devices.contains(device))
			devices.add(device);
	}
	if (size != devices.size()) {
		notifyItemRangeInserted(size, devices.size() - size);
		if (size == 0)
			listView.scrollToPosition(0);
	}
}
 
Example #22
Source File: MainActivity.java    From Android-nRF-BLE-Joiner with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public boolean hasDevice(ScanResult result) {
    for (ExtendedBluetoothDevice device : mDevices) {
        if (device.matches(result))
            return true;
    }
    return false;
}
 
Example #23
Source File: MainActivity.java    From Android-nRF-BLE-Joiner with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public void onBatchScanResults(List<ScanResult> results) {
    boolean newDeviceFound = false;
    for (final ScanResult result : results) {
        if (!mBleDeviceListAdapter.hasDevice(result)) {
            newDeviceFound = true;
            mBleDeviceListAdapter.addDevice(new ExtendedBluetoothDevice(result));
        }
    }

    if (newDeviceFound)
        mBleDeviceListAdapter.notifyDataSetChanged();
}
 
Example #24
Source File: DiscoveredBluetoothDevice.java    From Android-nRF-Blinky with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Updates the device values based on the scan result.
 *
 * @param scanResult the new received scan result.
 */
public void update(@NonNull final ScanResult scanResult) {
	lastScanResult = scanResult;
	name = scanResult.getScanRecord() != null ?
			scanResult.getScanRecord().getDeviceName() : null;
	previousRssi = rssi;
	rssi = scanResult.getRssi();
	if (highestRssi < rssi)
		highestRssi = rssi;
}
 
Example #25
Source File: DiscoveredBluetoothDevice.java    From Android-nRF-Blinky with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private DiscoveredBluetoothDevice(final Parcel in) {
	device = in.readParcelable(BluetoothDevice.class.getClassLoader());
	lastScanResult = in.readParcelable(ScanResult.class.getClassLoader());
	name = in.readString();
	rssi = in.readInt();
	previousRssi = in.readInt();
	highestRssi = in.readInt();
}
 
Example #26
Source File: ScannerViewModel.java    From Android-nRF-Blinky with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public void onScanResult(final int callbackType, @NonNull final ScanResult result) {
	// This callback will be called only if the scan report delay is not set or is set to 0.

	// If the packet has been obtained while Location was disabled, mark Location as not required
	if (Utils.isLocationRequired(getApplication()) && !Utils.isLocationEnabled(getApplication()))
		Utils.markLocationNotRequired(getApplication());

	if (devicesLiveData.deviceDiscovered(result)) {
		devicesLiveData.applyFilter();
		scannerStateLiveData.recordFound();
	}
}
 
Example #27
Source File: DevicesLiveData.java    From Android-nRF-Blinky with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Refreshes the filtered device list based on the filter flags.
 */
/* package */ synchronized boolean applyFilter() {
	final List<DiscoveredBluetoothDevice> tmp = new ArrayList<>();
	for (final DiscoveredBluetoothDevice device : devices) {
		final ScanResult result = device.getScanResult();
		if (matchesUuidFilter(result) && matchesNearbyFilter(device.getHighestRssi())) {
			tmp.add(device);
		}
	}
	filteredDevices = tmp;
       postValue(filteredDevices);
       return !filteredDevices.isEmpty();
}
 
Example #28
Source File: DevicesLiveData.java    From Android-nRF-Blinky with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Finds the index of existing devices on the device list.
 *
 * @param result scan result.
 * @return Index of -1 if not found.
 */
private int indexOf(@NonNull final ScanResult result) {
	int i = 0;
	for (final DiscoveredBluetoothDevice device : devices) {
		if (device.matches(result))
			return i;
		i++;
	}
	return -1;
}
 
Example #29
Source File: DevicesLiveData.java    From Android-nRF-Blinky with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@SuppressWarnings("SimplifiableIfStatement")
private boolean matchesUuidFilter(@NonNull final ScanResult result) {
	if (!filterUuidRequired)
		return true;

	final ScanRecord record = result.getScanRecord();
	if (record == null)
		return false;

	final List<ParcelUuid> uuids = record.getServiceUuids();
	if (uuids == null)
		return false;

	return uuids.contains(FILTER_UUID);
}
 
Example #30
Source File: ExtendedBluetoothDevice.java    From Android-nRF-BLE-Joiner with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public ExtendedBluetoothDevice(final ScanResult result) {
	mBluetoothDevice = result.getDevice();
	if (result.getScanRecord() != null)
		mName = result.getScanRecord().getDeviceName();
	else
		mName = mBluetoothDevice.getName();
}