【CoreBluetooth】iOS 蓝牙之一:搜索周边设备

CoreBluetooth:主要分为2大部分
1.作为中心,搜索周边设备
2.作为设备,发送广播被中心搜索

简述内容框架流程之一

iOS - 蓝牙.png

1 centerManager 实例化,并检查设备蓝牙状态,通过代理回调
2 蓝牙可用时,开始搜索周边设备,每次搜索到都会触发回调(注,将已搜索到的设备,实例化存本地,不然好像会一直搜索到)
3 选择某个扫描到的设备进行连接,代理回调

4 连接成功后,搜索设备包含的服务,代理回调
5 选择某个服务,搜索服务包含的特征,代理回调(注:服务也可能包含服务)(注2:一般对特征进行订阅与读写,注意属性)
6 选择某个特征,搜索特征包含的描述,代理回调

注:不管是设备还是服务等,都有特定的 UUID 区别

CBCentralManager 中心管理

职责:检查蓝牙状态,扫描设备,连接设备

  • 1 创建蓝牙管理
// 一般使用第二种,简单粗暴自带代理 
self.centerManager = [[CBCentralManager alloc] init];
self.centerManager = [[CBCentralManager alloc] initWithDelegate:self queue:nil];
 
// 这个....初始化特定的中心,可能与后台使用有关,确实不了解,请告知,谢谢;
self.centerManager = [[CBCentralManager alloc] initWithDelegate:self
                                                          queue:nil
                                                        options:@{CBCentralManagerOptionShowPowerAlertKey:@(YES),
                                                                  CBCentralManagerOptionRestoreIdentifierKey:@"UUID"}];


- (void)centralManagerDidUpdateState:(CBCentralManager *)central {
    if (central.state == CBCentralManagerStatePoweredOn) {
        // 蓝牙打开,则开始搜索
    } else {
        NSLog(@"蓝牙 异常");
    }
}

// 注意,这个代理有毒,可能与后台蓝牙有关
- (void)centralManager:(CBCentralManager *)central willRestoreState:(NSDictionary<NSString *, id> *)dict {

}

  • 2 扫描周边设备
 // 条件扫描 设备 
[self.centerManager scanForPeripheralsWithServices:nil options:nil];

// 判断 并 停止扫描
if ([self.centerManager isScanning]) {
    [self.centerManager stopScan];
}
  

- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary<NSString *, id> *)advertisementData RSSI:(NSNumber *)RSSI {
     NSLog(@"扫描到一个设备设备:%@",peripheral.name);  
    // RSSI 是设备信号强度
    // advertisementData 设备广告标识
    // 一般把新扫描到的设备添加到一个数组中,并更新列表
}
  • 3 连接/取消连接 设备
    [self.centerManager connectPeripheral:currentPer options:nil];
    [self.centerManager cancelPeripheralConnection:currentPer];


- (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral {
    NSLog(@"链接成功");
}

- (void)centralManager:(CBCentralManager *)central didFailToConnectPeripheral:(CBPeripheral *)peripheral error:(nullable NSError *)error {
    NSLog(@"链接失败");
}

- (void)centralManager:(CBCentralManager *)central didDisconnectPeripheral:(CBPeripheral *)peripheral error:(nullable NSError *)error {
    NSLog(@"设备断开链接");
}

  • 4 其他
 // 检索 设备
NSArray *arr1 =  [self.centerManager retrievePeripheralsWithIdentifiers:@[@"50088781-A05B-32CA-8BA0-F87365960D27"]];

NSArray *arr2 =  [self.centerManager retrieveConnectedPeripheralsWithServices:@[@"ECB0"]];

CBPeripheral 周边设备

职责:提供设备信息,服务,特征,描述
注:中间涉及 CBService,CBCharacteristic,CBDescriptor的 内容,所以穿插在里面了。

  • 1 CBPeripheral之一:获取设备信息
    peripheral.delegate = self;// 代理
    NSString *identify = peripheral.identifier.UUIDString;// id
    NSString *deviceName = peripheral.name;// 设备名称
    NSNumber *powerState = peripheral.RSSI;// 设备信号(ios8 开始使用,方法+代理的方式获取)
    if (peripheral.state == CBPeripheralStateConnected) {
        NSLog(@"已连接");
    } else {
        NSLog(@"其他状态");
    }
    
    NSArray *services = peripheral.services;// 需要扫描服务成功后才会有
    
    [peripheral readRSSI];// 获取信号,代理回调



- (void)peripheralDidUpdateName:(CBPeripheral *)peripheral NS_AVAILABLE(NA, 6_0) {
    NSLog(@"设备名称变化");
}

- (void)peripheral:(CBPeripheral *)peripheral didModifyServices:(NSArray<CBService *> *)invalidatedServices NS_AVAILABLE(NA, 7_0) {
    NSLog(@"设备服务变化");
}

- (void)peripheralDidUpdateRSSI:(CBPeripheral *)peripheral error:(nullable NSError *)error NS_DEPRECATED(NA, NA, 5_0, 8_0) {
    NSLog(@"获取到设备信号强度变化:%zi",peripheral.RSSI);
}

// 对应 readRSSI 方法
- (void)peripheral:(CBPeripheral *)peripheral didReadRSSI:(NSNumber *)RSSI error:(nullable NSError *)error NS_AVAILABLE(NA, 8_0) {
    NSLog(@"获取到设备信号强度:%zi",RSSI);

  • 2 CBPeripheral之二:设备服务
 // 服务相关,需要连接设备之后 需要主动扫描服务(一一对应代理)
    [peripheral discoverServices:nil];
    [peripheral discoverIncludedServices:nil forService:obj];


// 对应 discoverServices 方法
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(nullable NSError *)error {
    NSLog(@"成功扫描到设备的 服务");
    [peripheral.services enumerateObjectsUsingBlock:^(CBService * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        NSLog(@"第%zi个服务id是:%@",idx,obj.UUID.UUIDString);
    }];
}

// 对应 discoverIncludedServices 方法
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverIncludedServicesForService:(CBService *)service error:(nullable NSError *)error {
    NSLog(@"成功扫描到设备的 服务 内部的 服务");
    [service.includedServices enumerateObjectsUsingBlock:^(CBService * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        NSLog(@"第%zi个内部的服务id是:%@",idx,obj.UUID.UUIDString);
    }];
}

*** CBService 设备服务 ***
主要2部分:1被搜索的服务;2主动创建服务
注:搜索周边服务,仅使用1;

    CBPeripheral *currentPeripheral = obj.peripheral;// 提供服务的设备
    BOOL primary = obj.isPrimary;// 是否主要服务
    NSArray *includeServices = obj.includedServices;// 服务包含的服务(需要二次主动扫描,才能发现)
    NSArray *characteristics = obj.characteristics;// 服务包含的特征(同上)

*** CBAttribute 服务,特征,描述 的 父类 ***
提供唯一标识 UUID,由CBUUID 创建

*** CBUUID UUID 的类型 ***
这个不具体展开了:举个最简单的栗子:

[CBUUID UUIDWithString:@"ECB0"]
  • 3 CBPeripheral之三:设备服务特征
    // 服务特征相关,需要扫描到服务之后 主动扫描特征(一一对应代理)
    [peripheral discoverCharacteristics:nil forService:obj];

    // 读取 特征
    [peripheral readValueForCharacteristic:obj];

    // 写入 特征
    [peripheral writeValue:[NSData data] forCharacteristic:characteristic type:CBCharacteristicWriteWithResponse];

    // 订阅特征
    [peripheral setNotifyValue:YES forCharacteristic:characteristic];



// 对应 discoverCharacteristics 方法
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(nullable NSError *)error {
    NSLog(@"扫描到当前设备的服务特征:%@",service.characteristics);
}

// 对应 readValueForCharacteristic 同时,更新也会回调
- (void)peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic error:(nullable NSError *)error {
    NSLog(@"特征数据更新:%@",characteristic.value);
}

// 对应 writeValue forCharacteristic 方法
- (void)peripheral:(CBPeripheral *)peripheral didWriteValueForCharacteristic:(CBCharacteristic *)characteristic error:(nullable NSError *)error {
    NSLog(@"写入特征:%@",characteristic.value);
}

// 对应 setNotifyValue forCharacteristic 方法
- (void)peripheral:(CBPeripheral *)peripheral didUpdateNotificationStateForCharacteristic:(CBCharacteristic *)characteristic error:(nullable NSError *)error {
    NSLog(@"订阅特征 返回:%@",characteristic.value);
}

*** CBCharacteristic 设备服务特征 ***
同样包含2部分:1被搜索的服务特征;2主动创建服务特征
注:搜索周边服务,仅使用1;

    CBService *service = characteristic.service;// 特征上级服务
    NSData *data = characteristic.value;// 特征数据
    CBCharacteristicProperties state = characteristic.properties;// 特征属性
    BOOL isNotifying = characteristic.isNotifying;// 订阅状态
    
    NSArray *descriptors = characteristic.descriptors;// 需要主动扫描特征描述之后才能获取
    

// 特征状态 - 常见的 write 和 nofify,注意在特征操作时,关注其属性
typedef NS_OPTIONS(NSUInteger, CBCharacteristicProperties) {
 CBCharacteristicPropertyBroadcast            = 0x01,
 CBCharacteristicPropertyRead             = 0x02,
 CBCharacteristicPropertyWriteWithoutResponse         = 0x04,
 CBCharacteristicPropertyWrite             = 0x08,
 CBCharacteristicPropertyNotify             = 0x10,
 CBCharacteristicPropertyIndicate            = 0x20,
 CBCharacteristicPropertyAuthenticatedSignedWrites        = 0x40,
 CBCharacteristicPropertyExtendedProperties          = 0x80,
 CBCharacteristicPropertyNotifyEncryptionRequired NS_ENUM_AVAILABLE(NA, 6_0)  = 0x100,
 CBCharacteristicPropertyIndicateEncryptionRequired NS_ENUM_AVAILABLE(NA, 6_0) = 0x200
};


  • 4 CBPeripheral之四:设备服务特征的描述
    与特征类似,而且用的比较少
    // 服务特征描述相关,需要扫描到服务特征之后 主动扫描描述(一一对应代理)
    [peripheral discoverDescriptorsForCharacteristic:characteristic];
    // 读取
    [peripheral readValueForDescriptor:descriptor];
    // 写入
    [peripheral writeValue:[NSData data] forDescriptor:descriptor];



// 对应 discoverDescriptorsForCharacteristic 方法
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverDescriptorsForCharacteristic:(CBCharacteristic *)characteristic error:(nullable NSError *)error {
    NSLog(@"成功扫描到设备的 服务-特征 的 描述:%@",characteristic.descriptors);
}

// 对应 readValueForDescriptor 方法
- (void)peripheral:(CBPeripheral *)peripheral didUpdateValueForDescriptor:(CBDescriptor *)descriptor error:(nullable NSError *)error {
    NSLog(@"获取的特征的描述:%@",descriptor.value);
}

// 对应 writeValue forDescriptor 方法
- (void)peripheral:(CBPeripheral *)peripheral didWriteValueForDescriptor:(CBDescriptor *)descriptor error:(nullable NSError *)error {
    NSLog(@"成功写入特征的描述");
}

*** CBDescriptor 设备服务特征描述 ***
同样包含2部分:1被搜索的服务特征描述;2主动创建服务特征描述
注:搜索周边服务,仅使用1;

    CBCharacteristic *characteristic = descriptor.characteristic;// 描述的上级特征
    NSData *data = descriptor.value; // 描述数据

其他

注:基本上的API与相互关系,大致如此;实际使用还需要做逻辑处理 与 交互数据处理;

注2:github 有个第三方库,将代理变成了 block形式,支持链式编程(实际开发我并没有使用,感觉差不多)
https://github.com/coolnameismy/BabyBluetooth
有兴趣的可以用用

1

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 160,504评论 4 365
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,898评论 1 300
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 110,218评论 0 248
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,322评论 0 214
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,693评论 3 290
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,812评论 1 223
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 32,010评论 2 315
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,747评论 0 204
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,476评论 1 246
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,700评论 2 251
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,190评论 1 262
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,541评论 3 258
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,206评论 3 240
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,129评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,903评论 0 199
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,894评论 2 283
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,748评论 2 274

推荐阅读更多精彩内容