0

I aim to use code via https://github.com/davidgyoung/ble-advert-counter/blob/master/app/src/main/java/com/radiusnetworks/blepacketcounter/MainActivity.java to scan and read BLE device's adverting data.

The code works well. I could get the formatted adverting data via LogCat as pic shown.

enter image description here

But in the code I can't find the related log statement.

I didnt see BluetoothLeScanner class or onScanResult() method invoked.

And I want to obtain the String "ScanResult{mDevice=F3:E5:7F:73:4F:81, mScanRecord=ScanRecord..." to get the formatted data value.

How can I achieve this?

Thanks

DanielBarbarian
  • 5,093
  • 12
  • 35
  • 44

1 Answers1

0

I'm not sure about the logs but here's how you can get the data.

onLeScan() callback has all the information that is being printed in the logs. To get the device information you can use the device object from the call back(ex. device.getAddress()). Scan record will be in the callback's scanRecord byte array. You need to parse the array to get the information. I've used below code to parse the scan information.

public WeakHashMap<Integer, String> ParseRecord(byte[] scanRecord) {
    WeakHashMap<Integer, String> ret = new WeakHashMap<>();
    int index = 0;
    while (index < scanRecord.length) {
        int length = scanRecord[index++];
        //Zero value indicates that we are done with the record now
        if (length == 0) break;

        int type = scanRecord[index];
        //if the type is zero, then we are pass the significant section of the data,
        // and we are thud done
        if (type == 0) break;

        byte[] data = Arrays.copyOfRange(scanRecord, index + 1, index + length);
        if (data != null && data.length > 0) {
            StringBuilder hex = new StringBuilder(data.length * 2);
            // the data appears to be there backwards
            for (int bb = data.length - 1; bb >= 0; bb--) {
                hex.append(String.format("%02X", data[bb]));
            }
            ret.put(type, hex.toString());
        }
        index += length;
    }

    return ret;
}

Refer the below link to understand about the ble date advertisement.

BLE obtain uuid encoded in advertising packet

Hope this helps.

Community
  • 1
  • 1
Avinash4551
  • 220
  • 1
  • 9
  • Thanks for the help. It makes sense about to deal with scanRecord byte array. – stev90098289dev Oct 19 '16 at 09:24
  • Thanks for the help. It makes sense about to deal with scanRecord byte array. However, from LogCat, it seems the BluetoothLeScanner class or onScanResult() method has already handled with scanRecord byte array and output well-labeled Strings including readable MANUFACTURING DATA and other data info as well. If I could use this String from LogCat, it will be handy. – stev90098289dev Oct 19 '16 at 09:28