【iOS】蓝牙学习笔记

1.外围设备

(1)创建外围设备,并设置代理

(2)设置代理会触发下面方法.在方法中,如果蓝牙打开,则创建特征、创建服务,然后将服务添加到外围设备

- (void)peripheralManagerDidUpdateState:(CBPeripheralManager *)peripheral

(3)将服务添加到外围设备会触发下面方法.在方法中,设置广播信息(如名称,UUID),然后开始广播

- (void)peripheralManager:(CBPeripheralManager *)peripheral didAddService:(CBService *)service error:(NSError *)error

(4)开始广播会触发下面方法.在方法中,判断启动广播是否成功,如果成功,则自动循环广播

- (void)peripheralManagerDidStartAdvertising:(CBPeripheralManager *)peripheral error:(NSError *)error

(5)中心设备读取数据会触发下面方法.在方法中,外围设备响应请求

- (void)peripheralManager:(CBPeripheralManager *)peripheral didReceiveReadRequest:(CBATTRequest *)request

(6)中心设备写入数据会触发下面方法.在方法中,处理写入的数据

- (void)peripheralManager:(CBPeripheralManager *)peripheral didReceiveWriteRequests:(NSArray<CBATTRequest *> *)requests

(7)中心设备订阅了外围设备的某个特征会触发下面方法.在方法中可以获得中心设备以及被订阅的特征,另外需要将中心设备保存

- (void)peripheralManager:(CBPeripheralManager *)peripheral central:(CBCentral *)central didSubscribeToCharacteristic:(CBCharacteristic *)characteristic

中心设备取消订阅会触发下面方法

-(void)peripheralManager:(CBPeripheralManager *)peripheral central:(CBCentral *)central didUnsubscribeFromCharacteristic:(CBCharacteristic *)characteristic)

2.中心设备

(1).创建中心设备,并设置代理

(2).设置代理会触发下面方法.在方法中,如果蓝牙打开,则开始扫描外围设备

- (void)centralManagerDidUpdateState:(CBCentralManager *)central

(3).发现外围设备会触发下面方法.在方法中,保存外围设备,然后连接外围设备

-(void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI

(4).连接到外围设备会触发下面方法.在方法中,给外围设备设置代理,然后让外围设备根据服务UUID寻找服务

-(void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral

(5).外围设备找到服务后会触发下面方法.在方法中,遍历找到的服务,找到指定服务;外围设备在指定的服务中,根据特征UUID去寻找特征

-(void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error

(6).外围设备找到特征后会触发下面方法.在方法中遍历找到的特征,找到指定特征,此时可以做三种操作:a.读取外围设备特征数据 b.写入数据到外围设备特征 c.订阅特征

-(void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error

a.读取外围设备特征数据 或者订阅特征且特征值有变化 会触发下面方法.在方法中,处理得到的数据

-(void)peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error

b.写入数据到外围设备特征会触发下面方法.在方法中判断是否写入成功

- (void)peripheral:(CBPeripheral *)peripheral didWriteValueForCharacteristic:(nonnull CBCharacteristic *)characteristic error:(nullable NSError *)error

c.订阅特征会触发下面方法.在方法中判断是否订阅成功

-(void)peripheral:(CBPeripheral *)peripheral didUpdateNotificationStateForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,107评论 19 139
  • 一、蓝牙 随着蓝牙低功耗技术BLE(Bluetooth Low Energy)的发展,蓝牙技术正在一步步成熟,如今...
    执着丶执念阅读 2,533评论 14 35
  • 月满轩窗,对酒西廊。正炉边、茶袅清香。小笺落字,斑管成行。又吟新诗,赋新曲,斗新章。 枕上残妆,锦被寒凉。望灯花、...
    田梦_阅读 439评论 24 36
  • 最近情绪一直很糟糕,希望这个色调给自己一些沉静。 以下为过程图。
    陆离_mio阅读 161评论 2 4