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

The following examples show how to use no.nordicsemi.android.support.v18.scanner.ScanRecord. 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: BlePeripheralsAdapter.java    From Bluefruit_LE_Connect_Android_V2 with MIT License 5 votes vote down vote up
private static String getUriBeaconAdvertisementDescription(Context context, BlePeripheral blePeripheral) {
    StringBuilder result = new StringBuilder();

    final String name = blePeripheral.getDevice().getName();
    if (name != null) {
        result.append(context.getString(R.string.scanresult_advertisement_localname)).append(": <b>").append(name).append("</b><br>");
    }

    final String address = blePeripheral.getDevice().getAddress();
    result.append(context.getString(R.string.scanresult_advertisement_address)).append(": <b>").append(address == null ? "" : address).append("</b><br>");

    final ScanRecord scanRecord = blePeripheral.getScanRecord();
    if (scanRecord != null) {
        byte[] scanRecordBytes = scanRecord.getBytes();
        if (scanRecordBytes != null) {
            String uri = UriBeaconUtils.getUriFromAdvertisingPacket(scanRecordBytes) + "</b><br>";
            result.append(context.getString(R.string.scanresult_advertisement_uribeacon_uri)).append(": <b>").append(uri);

            final int txPower = scanRecord.getTxPowerLevel();
            if (txPower > -255) {       // is valid?
                result.append(context.getString(R.string.scanresult_advertisement_txpower)).append(": <b>").append(txPower).append("</b>");
            }
        }
    }

    return result.toString();
}
 
Example #3
Source File: BlePeripheralsAdapter.java    From Bluefruit_LE_Connect_Android_V2 with MIT License 5 votes vote down vote up
private static String getCommonAdvertisementDescription(Context context, BlePeripheral blePeripheral) {
    StringBuilder result = new StringBuilder();

    final String name = blePeripheral.getDevice().getName();
    if (name != null) {
        result.append(context.getString(R.string.scanresult_advertisement_localname)).append(": <b>").append(name).append("</b><br>");
    }
    final String address = blePeripheral.getDevice().getAddress();
    result.append(context.getString(R.string.scanresult_advertisement_address)).append(": <b>").append(address == null ? "" : address).append("</b><br>");

    StringBuilder serviceText = new StringBuilder();
    ParcelUuid[] uuids = blePeripheral.getDevice().getUuids();
    if (uuids != null) {
        int i = 0;
        for (ParcelUuid uuid : uuids) {
            if (i > 0) serviceText.append(", ");
            serviceText.append(uuid.toString().toUpperCase());
            i++;
        }
    }
    if (!serviceText.toString().isEmpty()) {
        result.append(context.getString(R.string.scanresult_advertisement_servicesuuids)).append(": <b>").append(serviceText).append("</b><br>");
    }

    ScanRecord scanRecord = blePeripheral.getScanRecord();
    if (scanRecord != null) {
        final int txPower = scanRecord.getTxPowerLevel();
        if (txPower > -255) {       // is valid?
            result.append(context.getString(R.string.scanresult_advertisement_txpower)).append(": <b>").append(txPower).append("</b>");
        }
    }

    return result.toString();
}
 
Example #4
Source File: Utils.java    From Android-nRF-Mesh-Library with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Nullable
public static byte[] getServiceData(@NonNull final ScanResult result, @NonNull final UUID serviceUuid) {
    final ScanRecord scanRecord = result.getScanRecord();
    if (scanRecord != null) {
        return scanRecord.getServiceData(new ParcelUuid((serviceUuid)));
    }
    return null;
}
 
Example #5
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 #6
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 #7
Source File: ScannerRepository.java    From Android-nRF-Mesh-Library with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private void updateScannerLiveData(final ScanResult result) {
    final ScanRecord scanRecord = result.getScanRecord();
    if (scanRecord != null) {
        if (scanRecord.getBytes() != null) {
            final byte[] beaconData = mMeshManagerApi.getMeshBeaconData(scanRecord.getBytes());
            if (beaconData != null) {
                mScannerLiveData.deviceDiscovered(result, mMeshManagerApi.getMeshBeacon(beaconData));
            } else {
                mScannerLiveData.deviceDiscovered(result);
            }
            mScannerStateLiveData.deviceFound();
        }
    }
}
 
Example #8
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 #9
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 #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: 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 #12
Source File: BleScanner.java    From Bluefruit_LE_Connect_Android_V2 with MIT License 4 votes vote down vote up
public static int getDeviceType(@NonNull BlePeripheral blePeripheral) {
    int type = kDeviceType_Unknown;

    ScanRecord scanRecord = blePeripheral.getScanRecord();
    if (scanRecord != null) {
        byte[] advertisedData = scanRecord.getBytes();

        // Check if is an iBeacon ( 0x02, 0x01, a flag byte, 0x1A, 0xFF, manufacturer (2bytes), 0x02, 0x15)
        final boolean isBeacon = advertisedData != null && advertisedData.length > 8 && advertisedData[0] == 0x02 && advertisedData[1] == 0x01 && advertisedData[3] == 0x1A && advertisedData[4] == (byte) 0xFF && advertisedData[7] == 0x02 && advertisedData[8] == 0x15;
        if (isBeacon) {
            type = kDeviceType_Beacon;
        } else {
            // Check if is an URIBeacon
            final byte[] kUriBeaconPrefix = {0x03, 0x03, (byte) 0xD8, (byte) 0xFE};
            final boolean isUriBeacon = advertisedData != null && advertisedData.length > 7 && Arrays.equals(Arrays.copyOf(advertisedData, kUriBeaconPrefix.length), kUriBeaconPrefix) && advertisedData[5] == 0x16 && advertisedData[6] == kUriBeaconPrefix[2] && advertisedData[7] == kUriBeaconPrefix[3];

            if (isUriBeacon) {
                type = kDeviceType_UriBeacon;
            } else {
                // Check if Uart is contained in the uuids
                boolean isUart = false;
                List<ParcelUuid> serviceUuids = scanRecord.getServiceUuids();
                if (serviceUuids != null) {
                    ParcelUuid uartUuid = ParcelUuid.fromString("6e400001-b5a3-f393-e0a9-e50e24dcca9e");
                    for (ParcelUuid serviceUuid : serviceUuids) {
                        if (serviceUuid.equals(uartUuid)) {
                            isUart = true;
                            break;
                        }
                    }
                }

                if (isUart) {
                    type = kDeviceType_Uart;
                }
            }
        }
    }

    return type;
}
 
Example #13
Source File: BlePeripheral.java    From Bluefruit_LE_Connect_Android_V2 with MIT License 4 votes vote down vote up
public ScanRecord getScanRecord() {
    return mScanResult.getScanRecord();
}