Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

BLE Scan Record Explanation

I am trying to get the UUID, Major, Minor IDs from the BLE advertisement received in the form of a byte[]. I have used the suggested code here but the i am unable to understand the output of the parser. Here is the output i get for one of the BLE devices

Length: 2 Type : 1 Data : 6,   
Length: 26 Type : -1 Data : 76 0 2 21 -9 -126 109 -90 79 -94 78 -104 -128 36 -68 91 113 -32 -119 62 12 -121 -79 52 -77,  
Length: 8 Type : 9 Data : 75 111 110 116 97 107 116,  
Length: 2 Type : 10 Data : -12,  
Length: 10 Type : 22 Data : 13 -48 117 76 106 98 50 55 100

How to understand which field contains the UUID , major and minor IDs? . I read from same post on stackoverflow that 0x07 indicates UUID, how do i understand how the type is 0x07 from the above data.

This is my first question here so apologies for any mistakes in the way the question was asked.

Here is the code just in case:

public void printScanRecord (byte[] scanRecord) {
    // Simply print all raw bytes   
    try {
        String decodedRecord = new String(scanRecord,"UTF-8");
        Log.d("DEBUG","decoded String : " + ByteArrayToString(scanRecord));
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }

    // Parse data bytes into individual records
    List<AdRecord> records = AdRecord.parseScanRecord(scanRecord);


    // Print individual records 
    if (records.size() == 0) {
        Log.i("DEBUG", "Scan Record Empty");
    } else {
        Log.i("DEBUG", "Scan Record: " + TextUtils.join(",", records));
    }

}


public static String ByteArrayToString(byte[] ba)
{
  StringBuilder hex = new StringBuilder(ba.length * 2);
  for (byte b : ba)
    hex.append(b + " ");
  return hex.toString();
}


public static class AdRecord {

    public AdRecord(int length, int type, byte[] data) {
        String decodedRecord = "";
        try {
            decodedRecord = new String(data,"UTF-8");

        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

        Log.d("DEBUG", "Length: " + length + " Type : " + type + " Data : " + ByteArrayToString(data));         
    }

    // ...

    public static List<AdRecord> parseScanRecord(byte[] scanRecord) {
        List<AdRecord> records = new ArrayList<AdRecord>();

        int index = 0;
        while (index < scanRecord.length) {
            int length = scanRecord[index++];
            //Done once we run out of records
            if (length == 0) break;

            int type = scanRecord[index];
            //Done if our record isn't a valid type
            if (type == 0) break;

            byte[] data = Arrays.copyOfRange(scanRecord, index+1, index+length);

            records.add(new AdRecord(length, type, data));
            //Advance
            index += length;
        }

        return records;
    }

    // ...
}
like image 357
user3679686 Avatar asked Jul 14 '15 21:07

user3679686


People also ask

How does a BLE scan work?

During BLE advertisement, a BLE Peripheral device transmits the same packet on the 3 advertising channels, one after the other. A Central device scanning for devices or beacons will listen to those channels for the advertising packets, which helps it discover devices nearby.

What does raw data mean on ble scanner?

Raw data is an array of hexadecimal values read from your device characteristic, or advertisement data, which starts with the vendor ID on 2 bytes, followed with the manufacturer data. – matdev. May 20, 2021 at 13:25. So you mean, manufacturer data from scan record is called as RAW data?

How do I scan a BLE device?

To find BLE devices, you use the startScan() method. This method takes a ScanCallback as a parameter. You must implement this callback, because that is how scan results are returned.

What is BLE code?

BLE stands for Bluetooth Low Energy (Bluetooth LE, and marketed as Bluetooth Smart). Bluetooth Low Energy (BLE) is a form of wireless communication designed especially for short-range communication. BLE is very similar to Wi-Fi in the sense that it allows devices to communicate with each other.


1 Answers

I suppose the Type values are explained in: Generic Access Profile web page

I also made some parser code that you could find from the bottom of mybletest/BLEBase.java file.

like image 59
Dr.Jukka Avatar answered Oct 17 '22 01:10

Dr.Jukka