zhuguifei
2025-12-30 61eee1173c00a7ba9d9c748d28fe3acdb33b9441
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package com.blakequ.bluetooth_manager_lib.device.adrecord;
 
import android.annotation.SuppressLint;
import android.util.SparseArray;
 
import com.blakequ.bluetooth_manager_lib.util.ByteUtils;
 
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
 
public final class AdRecordUtils {
 
    private AdRecordUtils(){
        // TO AVOID INSTANTIATION
    }
 
    public static String getRecordDataAsString(final AdRecord nameRecord) {
        if (nameRecord == null) {
            return "";
        }
        return new String(nameRecord.getData());
    }
 
    public static byte[] getServiceData(final AdRecord serviceData) {
        if (serviceData == null) {
            return null;
        }
        if (serviceData.getType() != AdRecord.TYPE_SERVICE_DATA) return null;
 
        final byte[] raw = serviceData.getData();
        //Chop out the uuid
        return Arrays.copyOfRange(raw, 2, raw.length);
    }
 
    public static int getServiceDataUuid(final AdRecord serviceData) {
        if (serviceData == null) {
            return -1;
        }
        if (serviceData.getType() != AdRecord.TYPE_SERVICE_DATA) return -1;
 
        final byte[] raw = serviceData.getData();
        //Find UUID data in byte array
        int uuid = (raw[1] & 0xFF) << 8;
        uuid += (raw[0] & 0xFF);
 
        return uuid;
    }
 
    /*
     * Read out all the AD structures from the raw scan record
     */
    public static List<AdRecord> parseScanRecordAsList(final byte[] scanRecord) {
        final List<AdRecord> records = new ArrayList<>();
 
        int index = 0;
        while (index < scanRecord.length) {
            final int length = scanRecord[index++];
            //Done once we run out of records
            if (length == 0) break;
 
            final int type = ByteUtils.getIntFromByte(scanRecord[index]);
 
            //Done if our record isn't a valid type
            if (type == 0) break;
 
            final byte[] data = Arrays.copyOfRange(scanRecord, index + 1, index + length);
 
            records.add(new AdRecord(length, type, data));
 
            //Advance
            index += length;
        }
 
        return Collections.unmodifiableList(records);
    }
 
    @SuppressLint("UseSparseArrays")
    public static Map<Integer, AdRecord> parseScanRecordAsMap(final byte[] scanRecord) {
        final Map<Integer, AdRecord> records = new HashMap<>();
 
        int index = 0;
        while (index < scanRecord.length) {
            final int length = scanRecord[index++];
            //Done once we run out of records
            if (length == 0) break;
 
            final int type = ByteUtils.getIntFromByte(scanRecord[index]);
 
            //Done if our record isn't a valid type
            if (type == 0) break;
 
            final byte[] data = Arrays.copyOfRange(scanRecord, index + 1, index + length);
 
            records.put(type, new AdRecord(length, type, data));
 
            //Advance
            index += length;
        }
 
        return Collections.unmodifiableMap(records);
    }
 
    public static SparseArray<AdRecord> parseScanRecordAsSparseArray(final byte[] scanRecord) {
        final SparseArray<AdRecord> records = new SparseArray<>();
 
        int index = 0;
        while (index < scanRecord.length) {
            final int length = scanRecord[index++];
            //Done once we run out of records
            if (length == 0) break;
 
            final int type = ByteUtils.getIntFromByte(scanRecord[index]);
 
            //Done if our record isn't a valid type
            if (type == 0) break;
 
            final byte[] data = Arrays.copyOfRange(scanRecord, index + 1, index + length);
 
            records.put(type, new AdRecord(length, type, data));
 
            //Advance
            index += length;
        }
 
        return records;
    }
}