preface

In the last article, I recorded the basic content of bluetooth broadcast package assembly for iOS. In this paper, I will continue to supplement the contents of ProductID and MAC partial broadcast package assembly and bluetooth broadcast package parsing mentioned in the previous article.

Assemble bluetooth broadcast package

Assemble the 7-n (subscript 6-n) byte

7-n bytes we can assemble the Product ID of the device (Product ID, 4 bytes, issued by the living iot platform). And the MAC address. (Bluetooth device MAC address, 6 bytes, unique device address, issued by the Living iot platform.) I will directly use the provided example values here, ProductID is 0x00EF1000 and Mac address is 0xB0b448D07882. In conclusion, the value of ProductID and MAC is 0x00EF1000b448D07882. This section deals with converting hexadecimal strings to hexadecimal. The conversion between iOS NSData and hexadecimal string is described as follows: a hexadecimal string to hexadecimal binary.

/ /! Hexadecimal string to hexadecimal NSData
- (NSData *)dataOfHexString:(NSString *)hexStr {
    
    NSInteger hexLength = [hexStr length];
    if(! hexStr || hexLength ==0) {
        return [NSData data];
    }
    
    hexLength = [hexStr length];
    
    NSMutableData *mHexData = [NSMutableData data];
    NSRange range;
    
    if (hexLength % 2= =0) {
        range = NSMakeRange(0.2);
    } else {
        // For odd numbers, reserve multiples of 2
        range = NSMakeRange(0.1);
    }
    
    for (NSInteger i = range.location; i < hexLength; i += 2) {
        unsigned int anInt;
        NSString *hexCharStr = [hexStr substringWithRange:range];
        if ([hexCharStr isEqualToString:@"0x"]) {
            / / remove the 0 x
            range.location += range.length;
            range.length = 2;
            continue;
        }
        NSScanner *scanner = [[NSScanner alloc] initWithString:hexCharStr];
        // Scans for an unsigned value from a hexadecimal representation, returning a found value by reference.
        [scanner scanHexInt:&anInt];
        NSData *data = [[NSData alloc] initWithBytes:&anInt length:1];
        [mHexData appendData:data];
        range.location += range.length;
        range.length = 2;
    }
    
    return [mHexData copy];
}
Copy the code

Using the above method, we can call the ProductID and MAC hexadecimal NSData values as follows.

    NSData *productIDData = [self dataOfHexString:@"0x00ef1000"];
    NSData *macData = [self dataOfHexString:@"0xb448d07882"];
Copy the code

Based on the basic record of iOS bluetooth broadcast package assembly, we can package bluetooth broadcast package as follows:

2021- 01- 17 21:15:33.602065+0800 WDemo[9420:919097] {length = 15, bytes = 0x0fff01a8b50300ef1000b448d07882}
Copy the code

Parse bluetooth broadcast packets

Resolves the length value
	// 0 bytes Length
    NSData *data0 = [data subdataWithRange:NSMakeRange(0.1)];
    Byte *byte0 = (Byte *)data0.bytes;
    UInt8 length = (UInt8)byte0[0];
    NSLog(Length: % hhu "@", length);
Copy the code

The author will take the first Byte of the Bluetooth broadcast packet, then take the corresponding Byte value, and finally get the value of Length is 15. The output of the above code is

2021-01-17 21:36:51.526314+0800 WDemo[9919:967052] Length: 15Copy the code
Parse the value of Type
	// 1 byte Type
    NSData *data1 = [data subdataWithRange:NSMakeRange(1.1)];
    Byte *byte1 = (Byte *)data1.bytes;
    UInt8 type = (UInt8)byte1[0];
    NSLog(Type: % hhu "@", type);
Copy the code

The author will take the second Byte of the Bluetooth broadcast packet, and then take the corresponding Byte value, and finally get the value of Type 255.

The output of the above code is

2021-01-17 21:36:51.526583+0800 WDemo[9919:967052] Type: 255Copy the code
Parsing the CID values
// 3 bytes CID is a hexadecimal to hexadecimal string, which I will share with the ProductID part in a future article.Copy the code
Parse the values of VID and subType

The author will take out the 5th Byte of the Bluetooth broadcast package, and then fetch the corresponding Byte value. Combined with the and operator and the right-shift operation of VID, the value of VID is 11, and the value of subType is 5.

	 // the 5th byte (the 4th byte indexed from 0) VID and Subtype
    NSData *data5 = [data subdataWithRange:NSMakeRange(4.1)];
    Byte *byte5 = (Byte *)data5.bytes;
    UInt8 vid = byte5[0] & 0xF0;
    vid = vid >> 4;
    UInt8 subType = byte5[0] & 0x0F;
    NSLog(Vid: % @ "hhu", vid);
    NSLog(@ "subType: % hhu", subType);
Copy the code

The output of the above code is

2021-01-17 21:36:54.057641+0800 WDemo[9919:967052] Vid: 11 2021-01-17 21:36:54.057827+0800 WDemo[9919:967052] subType :5Copy the code

The remaining sixth byte FMSK and CID, ProductID and MAC values will be shared in a future article.

Refer to study website

Aliyun’s living iot platform is a custom Bluetooth BLE broadcast specification