1 // return name String(successful) or null(failed)
2 public static String parseDeviceName(byte[] scanRecord) {
3 String ret = null;
4 if(null == scanRecord) {
5 return ret;
6 }
7
8 ByteBuffer buffer = ByteBuffer.wrap(scanRecord).order(ByteOrder.LITTLE_ENDIAN);
9 while (buffer.remaining() > 2) {
10 byte length = buffer.get();
11 if (length == 0)
12 break;
13
14 byte type = buffer.get();
15 length -= 1;
16 switch (type) {
17 case 0x01: // Flags
18 buffer.get(); // flags
19 length--;
20 break;
21 case 0x02: // Partial list of 16-bit UUIDs
22 case 0x03: // Complete list of 16-bit UUIDs
23 case 0x14: // List of 16-bit Service Solicitation UUIDs
24 while (length >= 2) {
25 buffer.getShort();
26 length -= 2;
27 }
28 break;
29 case 0x04: // Partial list of 32 bit service UUIDs
30 case 0x05: // Complete list of 32 bit service UUIDs
31 while (length >= 4) {
32 buffer.getInt();
33 length -= 4;
34 }
35 break;
36 case 0x06: // Partial list of 128-bit UUIDs
37 case 0x07: // Complete list of 128-bit UUIDs
38 case 0x15: // List of 128-bit Service Solicitation UUIDs
39 while (length >= 16) {
40 long lsb = buffer.getLong();
41 long msb = buffer.getLong();
42 length -= 16;
43 }
44 break;
45 case 0x08: // Short local device name
46 case 0x09: // Complete local device name
47 byte sb[] = new byte[length];
48 buffer.get(sb, 0, length);
49 length = 0;
50 ret = new String(sb).trim();
51 return ret;
52 case (byte) 0xFF: // Manufacturer Specific Data
53 buffer.getShort();
54 length -= 2;
55 break;
56 default: // skip
57 break;
58 }
59 if (length > 0) {
60 buffer.position(buffer.position() + length);
61 }
62 }
63 return ret;
64 }