Matter蓝牙解析
解析 Matter 蓝牙广播
定义需要解析的字段。
#import <Foundation/Foundation.h>NS_ASSUME_NONNULL_BEGIN/// 蓝牙广播服务 ID
extern NSString * const MatterBLEAdvServiceID;@interface MatterBLEAdv : NSObject
@property (nonatomic, assign) NSInteger opCode;
@property (nonatomic, assign) NSInteger advVersion;
@property (nonatomic, assign) NSInteger discriminator;
@property (nonatomic, assign) NSInteger vendorID;
@property (nonatomic, assign) NSInteger productID;/// 从广播的 NSData 中解析信息。
+ (MatterBLEAdv * _Nullable)advInfoFromData:(NSData *)advertising;@end
.m 文件
具体的解析方法, 按照协议规则解析。
#import "MatterBLEAdv.h"NSString * const MatterBLEAdvServiceID = @"FFF6";@implementation MatterBLEAdv/// 从广播中解析相关数据。
+ (MatterBLEAdv *)advInfoFromData:(NSData *)advertising {if (advertising.length != 8) {return nil;}int opCode = 0;[advertising getBytes:&opCode range:NSMakeRange(0, 1)];int data1_2 = 0;[advertising getBytes:&data1_2 range:NSMakeRange(1, 2)];int advVersion = data1_2 & 0xf000; // bit 15-12int discriminator = data1_2 & 0x0fff; // bit 0-12int vendorID = 0; // 16进制:1345 十进制:4933[advertising getBytes:&vendorID range:NSMakeRange(3, 2)];int productID = 0; // 16进制:a001 十进制:40961[advertising getBytes:&productID range:NSMakeRange(5, 2)];MatterBLEAdv *adv = [[MatterBLEAdv alloc] init];adv.opCode = opCode;adv.advVersion = advVersion;adv.discriminator = discriminator;adv.vendo