BLE扫描记录解释

8

我正在尝试从以byte[]形式接收的BLE广告中获取UUID, Major, Minor IDs。我已经使用了这里建议的代码,但是我无法理解解析器的输出。以下是我在一个BLE设备上得到的输出:

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

如何确定哪个字段包含UUID、major和minor IDs?我从stackoverflow的同一篇帖子中读到,0x07表示UUID。我如何从上述数据中了解类型为0x07的含义?
这是我的第一个问题,对于提问方式的任何错误请见谅。
代码如下:
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;
    }

    // ...
}
1个回答

5

你好,我想解码byte[] scanRecord以获取从我的BLE设备发送的广告数据。我已经尝试在BLEBase.java类中使用ParseRecord(),但Map中的数据与我从BLE设备发送的数据不匹配。请问有什么指南/链接可以帮助我从scanRecord[]中获取数据吗? - Sachin Rana
上面的“通用访问配置文件网页”链接已经失效,这个链接可以使用 https://www.bluetooth.com/specifications/assigned-numbers/generic-access-profile/ - Paul

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接