-
Notifications
You must be signed in to change notification settings - Fork 128
/
IBeaconAdvertisingPacket.java
294 lines (241 loc) · 8.13 KB
/
IBeaconAdvertisingPacket.java
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
package com.nexenio.bleindoorpositioning.ble.advertising;
import com.nexenio.bleindoorpositioning.ble.beacon.Beacon;
import com.nexenio.bleindoorpositioning.ble.beacon.IBeacon;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.Arrays;
import java.util.UUID;
/**
* For advertising packets as specified in Apples <a href="http://www.blueupbeacons.com/docs/dev/Proximity%20Beacon%20Specification%20R1.pdf">Proximity
* Beacon Specification</a>.
*/
public class IBeaconAdvertisingPacket extends AdvertisingPacket {
protected static final byte[] EXPECTED_FLAGS = {0x02, 0x01, 0x06};
protected static final byte EXPECTED_LENGTH = 0x1A;
protected static final byte EXPECTED_TYPE = (byte) 0xFF;
protected static final byte[] EXPECTED_COMPANY_ID = {0x4C, 0x00};
protected static final byte[] EXPECTED_BEACON_TYPE = {0x02, 0x15};
private byte[] flagsBytes;
private byte lengthByte;
private byte typeByte;
private byte[] companyIdBytes;
private byte[] beaconTypeBytes;
private byte[] proximityUuidBytes;
private byte[] majorBytes;
private byte[] minorBytes;
private byte measuredPowerByte;
private UUID proximityUuid;
private int major;
private int minor;
public IBeaconAdvertisingPacket(byte[] data) {
super(data);
}
private void parseData() {
flagsBytes = getFlagsBytes(data);
lengthByte = getLengthBytes(data);
typeByte = getTypeBytes(data);
companyIdBytes = getCompanyIdBytes(data);
beaconTypeBytes = getBeaconTypeBytes(data);
proximityUuidBytes = getProximityUuidBytes(data);
majorBytes = getMajorBytes(data);
minorBytes = getMinorBytes(data);
measuredPowerByte = getMeasuredPowerBytes(data);
}
@Override
public Class<? extends Beacon> getBeaconClass() {
return IBeacon.class;
}
@Override
public String toString() {
return new StringBuilder(getBeaconClass().getSimpleName() + " Advertising Packet (")
.append("Proximity UUID: ").append(getProximityUuid(getProximityUuidBytes())).append(" ")
.append("Major: ").append(getMajor(getMajorBytes())).append(" ")
.append("Minor: ").append(getMinor(getMinorBytes())).append(" ")
.append("RSSI at 1m: ").append(getMeasuredPowerByte())
.append(")")
.toString();
}
public static boolean meetsSpecification(byte[] data) {
if (data == null || data.length < 29) {
return false;
}
// In order to support advertising packets from manufacturers that
// adjusted the data type, we'll ignore this for now.
// See: https://github.com/neXenio/BLE-Indoor-Positioning/issues/79
/*
if (getTypeBytes(data) != EXPECTED_TYPE) {
return false;
}
*/
if (!Arrays.equals(getFlagsBytes(data), EXPECTED_FLAGS)) {
return false;
}
if (!Arrays.equals(getBeaconTypeBytes(data), EXPECTED_BEACON_TYPE)) {
return false;
}
return true;
}
public static boolean dataMatchesUuid(byte[] data, UUID referenceUuid) {
if (data.length < 9) {
return false;
}
return getProximityUuid(getProximityUuidBytes(data)).equals(referenceUuid);
}
public static byte[] getFlagsBytes(byte[] data) {
return Arrays.copyOfRange(data, 0, 3);
}
public static byte getLengthBytes(byte[] data) {
return data[3];
}
public static byte getTypeBytes(byte[] data) {
return data[4];
}
public static byte[] getCompanyIdBytes(byte[] data) {
return Arrays.copyOfRange(data, 5, 5 + 2);
}
public static byte[] getBeaconTypeBytes(byte[] data) {
return Arrays.copyOfRange(data, 7, 7 + 2);
}
public static byte[] getProximityUuidBytes(byte[] data) {
return Arrays.copyOfRange(data, 9, 9 + 24);
}
public static byte[] getMajorBytes(byte[] data) {
return Arrays.copyOfRange(data, 25, 25 + 2);
}
public static byte[] getMinorBytes(byte[] data) {
return Arrays.copyOfRange(data, 27, 27 + 2);
}
public static byte getMeasuredPowerBytes(byte[] data) {
return data[29];
}
public static UUID getProximityUuid(byte[] proximityUuidBytes) {
return AdvertisingPacketUtil.toUuid(proximityUuidBytes);
}
public static int getMajor(byte[] majorBytes) {
return getInt(majorBytes);
}
public static int getMinor(byte[] minorBytes) {
return getInt(minorBytes);
}
/**
* According to the iBeacon specification, minor and major are unsigned integer values between 0
* and 65535 (2 bytes each).
*
* @param data the minor or major bytes (2 bytes)
* @return integer between 0 and 65535
*/
private static int getInt(byte[] data) {
return ByteBuffer.wrap(new byte[]{data[1], data[0], 0, 0}).order(ByteOrder.LITTLE_ENDIAN).getInt();
}
/*
Getter & Setter
*/
public byte[] getFlagsBytes() {
if (flagsBytes == null) {
flagsBytes = getFlagsBytes(data);
}
return flagsBytes;
}
public void setFlagsBytes(byte[] flagsBytes) {
this.flagsBytes = flagsBytes;
}
public byte getLengthByte() {
if (lengthByte == 0) {
lengthByte = getLengthBytes(data);
}
return lengthByte;
}
public void setLengthByte(byte lengthByte) {
this.lengthByte = lengthByte;
}
public byte getTypeByte() {
if (typeByte == 0) {
typeByte = getTypeBytes(data);
}
return typeByte;
}
public void setTypeByte(byte typeByte) {
this.typeByte = typeByte;
}
public byte[] getCompanyIdBytes() {
if (companyIdBytes == null) {
companyIdBytes = getCompanyIdBytes(data);
}
return companyIdBytes;
}
public void setCompanyIdBytes(byte[] companyIdBytes) {
this.companyIdBytes = companyIdBytes;
}
public byte[] getBeaconTypeBytes() {
if (beaconTypeBytes == null) {
beaconTypeBytes = getBeaconTypeBytes(data);
}
return beaconTypeBytes;
}
public void setBeaconTypeBytes(byte[] beaconTypeBytes) {
this.beaconTypeBytes = beaconTypeBytes;
}
public byte[] getProximityUuidBytes() {
if (proximityUuidBytes == null) {
proximityUuidBytes = getProximityUuidBytes(data);
}
return proximityUuidBytes;
}
public void setProximityUuidBytes(byte[] proximityUuidBytes) {
this.proximityUuidBytes = proximityUuidBytes;
}
public byte[] getMajorBytes() {
if (majorBytes == null) {
majorBytes = getMajorBytes(data);
}
return majorBytes;
}
public void setMajorBytes(byte[] majorBytes) {
this.majorBytes = majorBytes;
}
public byte[] getMinorBytes() {
if (minorBytes == null) {
minorBytes = getMinorBytes(data);
}
return minorBytes;
}
public void setMinorBytes(byte[] minorBytes) {
this.minorBytes = minorBytes;
}
public byte getMeasuredPowerByte() {
if (measuredPowerByte == 0) {
measuredPowerByte = getMeasuredPowerBytes(data);
}
return measuredPowerByte;
}
public void setMeasuredPowerByte(byte measuredPowerByte) {
this.measuredPowerByte = measuredPowerByte;
}
public UUID getProximityUuid() {
if (proximityUuid == null) {
proximityUuid = getProximityUuid(getProximityUuidBytes());
}
return proximityUuid;
}
public void setProximityUuid(UUID proximityUuid) {
this.proximityUuid = proximityUuid;
}
public int getMajor() {
if (major == 0) {
major = getMajor(getMajorBytes());
}
return major;
}
public void setMajor(int major) {
this.major = major;
}
public int getMinor() {
if (minor == 0) {
minor = getMinor(getMinorBytes());
}
return minor;
}
public void setMinor(int minor) {
this.minor = minor;
}
}