简体   繁体   中英

How to parse bluetooth raw data

I am getting a Bluetooth device raw data from my gateway and want to parse it to extract some information but it is not showing properly. Actually, the device manufacturer shared a document where it mentioned that battery level info can be found at offset 13 with a length of 1 but understand how to decode this byte to get battery level. Here is my code with the parse method which hardcoded raw data as a string(0201060303AAFE0C16AAFE10E8016D696E657700):

   public static byte[] hexStringToByteArray(String s) {
        int len = s.length();
        byte[] b = new byte[len / 2];
        for (int i = 0; i < len; i += 2) {
            b[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4) + Character
                    .digit(s.charAt(i + 1), 16));
        }
        return b;
    }
    
    
   private void parse() {
        String s = "0201060303AAFE0C16AAFE10E8016D696E657700";
        byte[] adv = hexStringToByteArray(s);

        if ((0x03 & adv[4]) == 0x03) {
            System.out.println("info frame");
           System.out.println("battery:" + new String(new byte[] {advData[13]}));

        }
    }

Here is the device manufacturer document: 在此处输入图像描述

Most likely, you are trying to decode the wrong BLE advertisement. Decoding your example creates the following data.

The raw data: 02 01 06 03 03 AA FE 0C 16 AA FE 10 E8 01 6D 69 6E 65 77 00

leads to the following BLE Advertising Data Types:

Raw data Bluetooth LE Advertising Data Type
02 01 06 Flags
03 03 AA FE Complete List of 16bit Service Class UUIDs 0xFEAA (Google LLC, Eddystone Protocol)
0C 16 AA FE 10 E8 01 6D 69 6E 65 77 00 Service Data 16bit UUID 0xFEAA, Eddystone Protocol

Decoding the Eddystone data, see [1]:

Raw data Eddystone frame encoding
10 URL
E8 TX Power: -24 dBm
01 URL Scheme Prefix: https://www.
6D 69 6E 65 77 00 Eddystone-URL HTTP URL encoding: minew.com/

resulting URL: https://www.minew.com/

[1] https://github.com/google/eddystone/blob/master/protocol-specification.md

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM