美文网首页
蓝牙开发系列二:整体流程代码

蓝牙开发系列二:整体流程代码

作者: 猿二胖 | 来源:发表于2017-11-27 21:58 被阅读0次

一、初始化中心设备对象

self.centralManager = [[CBCentralManager alloc] initWithDelegate:self queue:nil];

二、扫描外设

1、扫描

[self.centralManager scanForPeripheralsWithServices:@[[CBUUID UUIDWithString:服务UUID字符串]] options:nil];

2、扫描到设备

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

参数:

central:中心设备

 peripheral:扫描到外设

advertisementData:广播数据(设备名称、设备uuid、设备Mac地址)

RSSI:表示外围设备信号强度

三、连接设备

1、连接设备

[self.centralManager connectPeripheral:peripheral options:@{

CBConnectPeripheralOptionNotifyOnConnectionKey: @YES,

CBConnectPeripheralOptionNotifyOnDisconnectionKey: @YES,

CBConnectPeripheralOptionNotifyOnNotificationKey: @YES,

}];

CBConnectPeripheralOptionNotifyOnConnectionKey:如果应用被挂起时,连接某设备成功,系统给予连接成功提示。

CBConnectPeripheralOptionNotifyOnDisconnectionKey:如果应用被挂起时,设备连接断开,系统给予设备断开提示。

CBConnectPeripheralOptionNotifyOnNotificationKey:如果应用被挂起时,从设备接收到的所有信息通知,系统给予提示。

2、连接"成功"

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

此时只是蓝牙层连接成功,尚未发现设备服务,不可以进行读写指令操作。此处不能算是真正意义上的连接成功状态。

在该方法内寻找设备对应的服务

//寻找指定UUID的Service

[peripheral discoverServices:@[[CBUUID UUIDWithString:指定服务UUID]]];

3、发现服务

//发现服务时调用的方法

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

发现服务,在该回调方法内扫描服务对应的特征值。即:

//寻找指定UUID的Characteristic

for (CBService *service in peripheral.services) {

    [peripheral discoverCharacteristics:nil forService:service];

}

4、发现服务特征值

//发现服务的特征值后回调的方法

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

扫描到对应的读写特征值。存储起来。并将设备的状态置为连接成功状态,即:

//遍历服务对应所有特征值

for (CBCharacteristic *characteristic in service.characteristics) {

//判断是不是需要的读服务UUID

if ([characteristic.UUID isEqual:[CBUUID UUIDWithString:读服务UUID]]) {

    //读服务UUID,则订阅该服务

    [peripheral setNotifyValue:YES forCharacteristic:characteristic];

    //存储起来

    self.readCharacteristic = characteristic;

} else if ([characteristic.UUID isEqual:[CBUUID UUIDWithString:写服务UUID]]) {

    //存储起来

    self.writeCharacteristic = characteristic;

}

}

5、连接失败

//连接外设失败

- (void)centralManager:(CBCentralManager *)central didFailToConnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error;

四、指令交互

1、发送指令

[self.peripheral writeValue:chunk forCharacteristic:self.writeCharacteristic type:CBCharacteristicWriteWithResponse];

2、读取数据

//特征值更新时回调的方法

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

该方法内进行数据的拼接。

五、断开连接

1、断开连接

[self.centralManager cancelPeripheralConnection:peripheral];

2、断开连接成功回调

//断开外设连接

- (void)centralManager:(CBCentralManager *)central didDisconnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error;

本文主要对扫描设备,扫描服务、连接设备、断开连接的整体流程,以及需要注意的地方简单梳理,如果对你有帮助,点击下方喜欢呦。下一章节将着重介绍CBCentralManager。

相关文章

网友评论

      本文标题:蓝牙开发系列二:整体流程代码

      本文链接:https://www.haomeiwen.com/subject/gmrjbxtx.html