杏仁丶的iOS学习专题

iOS 蓝牙开发“中心模式下的编程从属关系”

2016-09-18  本文已影响438人  黑羽肃霜

手机端作为中心角色

中心角色.png
手机端作为中心角色
  1. 建立中心角色
  2. 扫描外设(discover)
  3. 连接外设(connect)
  4. 扫描外设中的服务和特征(discover)
  1. 与外设做数据交互(explore and interact)
  2. 订阅Characteristic的通知
  3. 断开连接(disconnect)

CBCentralManagerDelegate 中心代理。

CBPeripheralDelegate 外设代理


FAQ

//SERVICE UUID = 180A
-----扫描到特征, service UUIDData = <180a>, data = 180A;;;;; 的 Characteristic = Manufacturer Name String-----
//其他UUID
-----扫描到特征, service UUIDData = <49535343 fe7d4ae5 8fa99faf d205e455>, data = 49535343-FE7D-4AE5-8FA9-9FAFD205E455;;;;; 的 Characteristic = 49535343-6DAA-4D02-ABF6-19569ACA69FE-----

外设广播的内容,大部分可以不需要关注。单个设备广播出多个UUID是正常的。而真正我们需要关注的service UUID是在中心和外设连接上后读到的。如下两张图


广播
注意点

UUID有可能是重复的,而MAC是唯一的。

  1. 是同一个东西,MAC是一个6个字节(48bit)的数.中间用冒号隔开。如xx:xx:xx:xx:xx:xx.前三个字节是IEEE授权的,后三个字节是厂家指定的。但是我们的mpos产品,其实mac是自己指定的。
  2. BLE配对过程,设备是有将MAC广播出去的,Android的设备可以获取到,但iOS似乎不行。
  3. 有一些厂家会将mac写在UUID的某个值中,如指定的 service UUID 0x180A内容的characteristic UUID 0x232A 用来存放mac. 但并不是所有的厂家都会将mac写在这里面。iOS的api是无法获取到Mac地址的。

个人理解,我们传统意义上的UUID都是CBAttribute的那些子类:服务,特征,描述这些里面呈现的UUID。我们连接蓝牙设备的时候,也是通过他的service暴露出来的service.UUID来识别设备的UUID.至于外设和中心体现出来的identifier,文档中有下面一段话

Peers are identified by NSUUID UUIDs instead of by the CBUUID objects that identify a peripheral’s services, characteristics, and characteristic descriptors.

使用read还是subscribe,一个看具体应用,一个看特征本身的属性 character.properties

enum {  
     CBCharacteristicPropertyBroadcast = 0x01,  
     CBCharacteristicPropertyRead = 0x02,  
     CBCharacteristicPropertyWriteWithoutResponse = 0x04,  
     CBCharacteristicPropertyWrite = 0x08,  
     CBCharacteristicPropertyNotify = 0x10,  
     CBCharacteristicPropertyIndicate = 0x20,  
     CBCharacteristicPropertyAuthenticatedSignedWrites = 0x40,  
     CBCharacteristicPropertyExtendedProperties = 0x80,  
     };

读取方式是根据 [self.character.properties & CBCharacteristicPropertyWriteWithoutResponse]来判断他的类型。


上一篇 下一篇

猜你喜欢

热点阅读