Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Eddystone BLE Scan
// Eddystone service UUID
private static final ParcelUuid UID_SERVICE =
ParcelUuid.fromString("0000feaa-0000-1000-8000-00805f9b34fb");
BluetoothManager manager = (BluetoothManager) getSystemService(BLUETOOTH_SERVICE);
BluetoothAdapter adapter = manager.getAdapter();
BluetoothLeScanner scanner = adapter.getBluetoothLeScanner();
ScanFilter beaconFilter = new ScanFilter.Builder()
.setServiceUuid(UID_SERVICE)
.setServiceData(UID_SERVICE, FRAME_FILTER_ON_UID, FILTER_MASK_ON_FRAME_TYPE)
.build();
List<ScanFilter> filters = Collections.singletonList(beaconFilter);
ScanSettings settings = new ScanSettings.Builder()
.setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY)
.build();
scanner.startScan(filters, settings, scanCallback);
private ScanCallback scanCallback = new ScanCallback() {
@Override
public void onScanResult(int callbackType, ScanResult result) {
byte[] serviceDate = result.getScanRecord().getServiceData(UID_SERVICE);
}
@Override
public void onBatchScanResults(List<ScanResult> results) {}
@Override
public void onScanFailed(int errorCode) {}
};
// Filter to ensure we only get Eddystone-UID advertisements
public static final byte[] FRAME_FILTER_ON_UID = {
0x00, //Frame type
0x00, //TX power
0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
public static final byte[] FILTER_MASK_ON_FRAME_TYPE = {
0xFF,
0x00,
0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment