iOS 蓝牙开发iOS Developer

iOS蓝牙开发

2017-08-20  本文已影响97人  67cb26e2116a

iOS蓝牙模块支持BLE4.0设备通讯。

在使用蓝牙的过程中,如果是iOS10及以上机型需要在info.plist文件中添加NSBluetoothPeripheralUsageDescription描述字段,向用户声明使用蓝牙的意图

一、CBCentralManager

1. 初始化

CBCentralManager 对象用于扫描、发现、连接远程的外围设备。系统提供了两个初始化该类的方法

- (instancetype)initWithDelegate:(id<CBCentralManagerDelegate>)delegate  queue:(dispatch_queue_t)queue;

- (instancetype)initWithDelegate:(id<CBCentralManagerDelegate>)delegate queue:(dispatch_queue_t)queue options:(NSDictionary<NSString *,id> *)options;

在第二个初始化方法中,初始化的选项情况如下:

2.扫描设备

当系统第一次发现外设时,会根据mac地址等生成一个特定的标识符,此时我们可以保存这个特定的标识符用于后续的重连操作
系统提供唯一的方法进行对周边设备的扫描

//扫描设备
- (void)scanForPeripheralsWithServices:(NSArray<CBUUID *> *)serviceUUIDs ptions:(NSDictionary<NSString *,id> *)options;

//停止扫描设备
- (void)stopScan;

3. 连接设备

//连接外设
- (void)connectPeripheral:(CBPeripheral *)peripheral  options:(NSDictionary<NSString *,id> *)options;

//断开与外设的连接
- (void)cancelPeripheralConnection:(CBPeripheral *)peripheral;

连接设备时选项情况如下:

官方建议如果连接设备不成功且没有进行重连,要明确取消与外设的连接(即调用断开与外设连接的方法)
当调用断开方法,断开与设备连接时,官方明确表明取消本地连接不能保证物理链接立即断开。当设备连接时间超过8秒后,调用断开的API能立即断开;但是连接未超过8秒,就调用断开API需要几秒后系统才与设备断开连接

4. 通过搜索系统获取periphera对象

//获取与系统已经连接的外设对象,如果设备已经与系统连接,通过该方法获取到外设对象后可以直接对CBPeripheral对象发起连接
- (NSArray<CBPeripheral *> *)retrieveConnectedPeripheralsWithServices:(NSArray<CBUUID *> *)serviceUUIDs;

//获取一个已知CBPeripheral的列表(过去曾经发现过或者连接过的peripheral)
- (NSArray<CBPeripheral *> *)retrievePeripheralsWithIdentifiers:(NSArray<NSUUID *> *)identifiers;

5. CBCentralManagerDelegate

启动搜索发现设备后调用此代理

/*
* advertisementData 广播中的信息
*
* CBAdvertisementDataLocalNameKey 对应设置NSString类型的广播名
* CBAdvertisementDataManufacturerDataKey 外设制造商的NSData数据
* CBAdvertisementDataServiceDataKey  外设制造商的CBUUID数据
* CBAdvertisementDataServiceUUIDsKey 服务的UUID与其对应的服务数据字典数组
* CBAdvertisementDataOverflowServiceUUIDsKey 附加服务的UUID数组
* CBAdvertisementDataTxPowerLevelKey 外设的发送功率 NSNumber类型
* CBAdvertisementDataIsConnectable 外设是否可以连接
* CBAdvertisementDataSolicitedServiceUUIDsKey 服务的UUID数组
*
* RSSI 收到当前信号的强度 单位为分贝
*
* 如果后续要使用peripheral对象,必须在引用该对象,否则后续操作无法进行
*/
- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary<NSString *,id> *)advertisementData RSSI:(NSNumber *)RSSI;

当设备连接成功后调用此代理

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

当设备断开连接后调用此代理方法

//当断开连接后,外设中所有的服务、特征、特征描述都将失效
- (void)centralManager:(CBCentralManager *)central didDisconnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error;

当连接失败后调用此代理方法

//在ios系统的蓝牙连接中,不存在超时的现象,因此连接通常表示为一个暂时性的问题,可在改方法中再次尝试连接
- (void)centralManager:(CBCentralManager *)central didFailToConnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error;

6. 状态的监控

当蓝牙状态发现变化时回调

/*
* CBCentralManagerStateUnknown 状态未知
* CBCentralManagerStateResetting 连接断开 即将重置
* CBCentralManagerStateUnsupported 该平台不支持蓝牙
* CBCentralManagerStateUnauthorized 未授权蓝牙使用
* CBCentralManagerStatePoweredOff 蓝牙关闭
* CBCentralManagerStatePoweredOn 蓝牙正常开启
*
* 该方法必须实现,否则系统报错
*/
- (void)centralManagerDidUpdateState:(CBCentralManager *)central;

当系统恢复时先调用此方法

/*
*app状态的保存或者恢复,这是第一个被调用的方法当APP进入后台去完成一些蓝牙有关的工作设置,使用这个方法同步app状态通过蓝牙系统
*
* dic中的信息
* CBCentralManagerRestoredStatePeripheralsKey 在系统终止程序时,包含的已经连接或者正在连接的外设的集合
* CBCentralManagerRestoredStateScanServicesKey 系统终止应用程序时中心管理器连接的服务UUID数组
* CBCentralManagerRestoredStateScanOptionsKey  系统终止应用程序时中心管理器正在使用的外设选项
*/
- (void)centralManager:(CBCentralManager *)central willRestoreState:(NSDictionary<NSString *,id> *)dict;

二、CBPeripheral

1. 属性

2. 发现服务

发现外设指定的服务

/*
* 当设置serviceUUIDs参数时,只返回指定的服务(官方推荐做法)
* 当为指定serviceUUIDs参数时,将返回外设中所有可用的服务
*/
- (void)discoverServices:(NSArray<CBUUID *> *)serviceUUIDs;

从服务中发现服务

- (void)discoverIncludedServices:(NSArray<CBUUID *> *)includedServiceUUIDs forService:(CBService *)service;

3. 发现特征

/*
* 参数characteristicUUIDs不为空时,返回对应的特征值,当参数为nil时,返回该服务中的所有特征值
*/
- (void)discoverCharacteristics:(NSArray<CBUUID *> *)characteristicUUIDs forService:(CBService *)service;

//发现特征值描述
- (void)discoverDescriptorsForCharacteristic:(CBCharacteristic *)characteristic;

4. 读取数据

/* 读取特征值
 * 并非所有特征都有特征值,需通过CBCharacteristicProperties枚举判断该特征是否可读
 */
- (void)readValueForCharacteristic:(CBCharacteristic *)characteristic;

//读取特征值描述
- (void)readValueForDescriptor:(CBDescriptor *)descriptor;

5. 写入数据

/*
 * 写入数据,只允许对其特定的类型进行写入操作
 * 通过characteristic的properties的判断,判别支持那种类型的写入
 * CBCharacteristicWriteWithResponse 如果写入不成功,将以详细错误说明进行响应
 * CBCharacteristicWriteWithoutResponse 如果写入失败,则不会收到通知
 */
- (void)writeValue:(NSData *)data forCharacteristic:(CBCharacteristic *)characteristic type:(CBCharacteristicWriteType)type;

//写入特征值的描述,一般为字符串对characteristic的描述
- (void)writeValue:(NSData *)data forDescriptor:(CBDescriptor *)descriptor;

6. 设置特征值的通知

//如果设置为YES,当特征值发现变化时则会发起通知
- (void)setNotifyValue:(BOOL)enabled forCharacteristic:(CBCharacteristic *)characteristic;

7. 读取RSSI

//当调用此方法时,外设会调用peripheral:didReadRSSI:error:代理方法
- (void)readRSSI;

8. 获取最大的写入长度

/* 根据写入类型获取最大的写入长度
 * CBCharacteristicWriteWithResponse、CBCharacteristicWriteWithoutResponse
 */
- (NSUInteger)maximumWriteValueLengthForType:(CBCharacteristicWriteType)type;

9. CBPeripheralDelegate

发现服务的回调

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

发现服务中的服务的回调

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

发现特征的回调

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

发现特征描述的回调

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

当特征值发现变化时回调

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

当特征值描述发生变化时回调

- (void)peripheral:(CBPeripheral *)peripheral didUpdateValueForDescriptor:(CBDescriptor *)descriptor error:(NSError *)error;

写入特征值时回调

//只有指定写入类型有回应时才回到此方法
- (void)peripheral:(CBPeripheral *)peripheral didWriteValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error;

写入特征值描述时回调

//当调用写入特征值描述时回调该方法
- (void)peripheral:(CBPeripheral *)peripheral didWriteValueForDescriptor:(CBDescriptor *)descriptor error:(NSError *)error;

当外设启动或者停止指定特征值的通知时回调

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

当连接外设成功后获取信号强度的方法后回调

- (void)peripheral:(CBPeripheral *)peripheral didReadRSSI:(NSNumber *)RSSI error:(NSError *)error;

当外设名称发生变化时回调

//由于外设可以改变其设备名称,因此若要显示该设备的当前名称可在该方法中处理
- (void)peripheralDidUpdateName:(CBPeripheral *)peripheral;

当外设服务发生变化时回调

- (void)peripheral:(CBPeripheral *)peripheral didModifyServices:(NSArray<CBService *> *)invalidatedServices;

三. CBCharacteristic

拥有的属性

上一篇 下一篇

猜你喜欢

热点阅读