Introduction to the use of the core methods of the CoreBlueTooth library for iOS Bluetooth development

I. Introduction

Bluetooth is a convenient means for devices to communicate at close range. After the introduction of Bluetooth 4.0 on the iPhone, communication between devices has become simpler. Related Bluetooth operations are managed uniformly by the dedicated CoreBluetooth.framework. The communication interaction through Bluetooth is divided into two parties, one is the central device, and the other is the peripheral device. data interaction. Here I wrote a lightweight bluetooth library GJLightBlueTooth , which includes basic bluetooth scanning connection operations.

2. Central equipment CBCentralManager

CBCentralManager is the management class of the management center device, and the important methods are as follows:

//设置中心设备代理
@property(assign, nonatomic, nullable) id<CBCentralManagerDelegate> delegate;
//中心设备当前状态
@property(readonly) CBCentralManagerState state;
//中心设备是否正在扫描
@property(readonly) BOOL isScanning NS_AVAILABLE(NA, 9_0);

Where state is an enumeration, the status about whether bluetooth is available is as follows:

typedef NS_ENUM(NSInteger, CBCentralManagerState) {
        //状态未知
    CBCentralManagerStateUnknown = 0,
    //连接断开 即将重置
    CBCentralManagerStateResetting,
    //该平台不支持蓝牙
    CBCentralManagerStateUnsupported,
    //未授权蓝牙使用
    CBCentralManagerStateUnauthorized,
    //蓝牙关闭
    CBCentralManagerStatePoweredOff,
    //蓝牙正常开启
    CBCentralManagerStatePoweredOn,
};

The following methods are used to initialize the Manager:

//初始化方法
//设置的代理需要遵守CBCentralManagerDelegate协议
//queue可以设置蓝牙扫描的线程 传入nil则为在主线程中进行
- (instancetype)initWithDelegate:(nullable id<CBCentralManagerDelegate>)delegate
                           queue:(nullable dispatch_queue_t)queue;
//此方法同上 在options字典中用于进行一些管理中心的初始化属性设置
//字典中支持的键值如下
/*
NSString * const CBCentralManagerOptionShowPowerAlertKey 对应一个NSNumber类型的bool值,用于设置是否在关闭蓝牙时弹出用户提示
NSString * const CBCentralManagerOptionRestoreIdentifierKey 对应一个NSString对象,设置管理中心的标识符ID
*/
- (instancetype)initWithDelegate:(nullable id<CBCentralManagerDelegate>)delegate
                           queue:(nullable dispatch_queue_t)queue
                         options:(nullable NSDictionary<NSString *, id> *)options;
//根据获取所有已知设备
- (NSArray<CBPeripheral *> *)retrievePeripheralsWithIdentifiers:(NSArray<NSUUID *> *)identifiers;
//根据服务id获取所有连接的设备
- (NSArray<CBPeripheral *> *)retrieveConnectedPeripheralsWithServices:(NSArray<CBUUID *> *)serviceUUIDs;

After the initialization of the management center is completed, the following methods in the proxy will be called back. We must implement the following methods:

//这个方法中可以获取到管理中心的状态
- (void)centralManagerDidUpdateState:(CBCentralManager *)central;

If the status of the management center in the above method is Bluetooth available, you can use the following methods to enable scanning for peripherals:

//serviceUUIDs用于扫描一个特点ID的外设 options用于设置一些扫描属性 键值如下
/*
//是否允许重复扫描 对应NSNumber的bool值,默认为NO,会自动去重
NSString *const CBCentralManagerScanOptionAllowDuplicatesKey;
//要扫描的设备UUID 数组 对应NSArray
NSString *const CBCentralManagerScanOptionSolicitedServiceUUIDsKey;
*/
- (void)scanForPeripheralsWithServices:(nullable NSArray<CBUUID *> *)serviceUUIDs options:(nullable NSDictionary<NSString *, id> *)options;
//停止扫描外设
- (void)stopScan;

The result of the scan will be returned in the following proxy method:

//peripheral 扫描到的外设
//advertisementData是外设发送的广播数据
//RSSI 是信号强度
- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary<NSString *, id> *)advertisementData RSSI:(NSNumber *)RSSI;

After scanning to a peripheral, you can connect a peripheral by:

/*
options中可以设置一些连接设备的初始属性键值如下
//对应NSNumber的bool值,设置当外设连接后是否弹出一个警告
NSString *const CBConnectPeripheralOptionNotifyOnConnectionKey;
//对应NSNumber的bool值,设置当外设断开连接后是否弹出一个警告
NSString *const CBConnectPeripheralOptionNotifyOnDisconnectionKey;
//对应NSNumber的bool值,设置当外设暂停连接后是否弹出一个警告
NSString *const CBConnectPeripheralOptionNotifyOnNotificationKey;
*/
- (void)connectPeripheral:(CBPeripheral *)peripheral options:(nullable NSDictionary<NSString *, id> *)options;
//取消一个外设的连接
- (void)cancelPeripheralConnection:(CBPeripheral *)peripheral;

After calling the method of connecting to the peripheral, the following proxy method will be returned:

//连接外设成功
- (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral;
//连接外设失败
- (void)centralManager:(CBCentralManager *)central didFailToConnectPeripheral:(CBPeripheral *)peripheral error:(nullable NSError *)error;
//断开外设连接
- (void)centralManager:(CBCentralManager *)central didDisconnectPeripheral:(CBPeripheral *)peripheral error:(nullable NSError *)error;

The following agents are called when the Manager is restored:

//dict中会传入如下键值对
/*
//恢复连接的外设数组
NSString *const CBCentralManagerRestoredStatePeripheralsKey;
//恢复连接的服务UUID数组
NSString *const CBCentralManagerRestoredStateScanServicesKey;
//恢复连接的外设扫描属性字典数组
NSString *const CBCentralManagerRestoredStateScanOptionsKey;
*/
- (void)centralManager:(CBCentralManager *)central willRestoreState:(NSDictionary<NSString *, id> *)dict;

3. Peripheral CBPeripheralManager

From the above, we know that the central device is used to scan the surrounding peripherals. In the communication between the two devices, one must act as the central device and the other as the peripheral. The peripherals are managed by the CBPeripheralManager. The main methods are as follows:

//设置外设管理中心代理
@property(assign, nonatomic, nullable) id<CBPeripheralManagerDelegate> delegate;
//外设状态 枚举如中心设备
@property(readonly) CBPeripheralManagerState state;
//是否正在发送广播
@property(readonly) BOOL isAdvertising;
//用户的授权状态
+ (CBPeripheralManagerAuthorizationStatus)authorizationStatus;
//初始化并设置代理 参数的具体含义与中心设备管理中心
- (instancetype)initWithDelegate:(nullable id<CBPeripheralManagerDelegate>)delegate
                           queue:(nullable dispatch_queue_t);
- (instancetype)initWithDelegate:(nullable id<CBPeripheralManagerDelegate>)delegate
                           queue:(nullable dispatch_queue_t)queue
                         options:(nullable NSDictionary<NSString *, id> *)options;
//开始发送广播
//advertisementData中可以发送的数据有约定 如下
/*
对应设置NSString类型的广播名
NSString *const CBAdvertisementDataLocalNameKey;
外设制造商的NSData数据
NSString *const CBAdvertisementDataManufacturerDataKey;
外设制造商的CBUUID数据
NSString *const CBAdvertisementDataServiceDataKey;
服务的UUID与其对应的服务数据字典数组
NSString *const CBAdvertisementDataServiceUUIDsKey;
附加服务的UUID数组
NSString *const CBAdvertisementDataOverflowServiceUUIDsKey;
外设的发送功率 NSNumber类型
NSString *const CBAdvertisementDataTxPowerLevelKey;
外设是否可以连接
NSString *const CBAdvertisementDataIsConnectable;
服务的UUID数组
NSString *const CBAdvertisementDataSolicitedServiceUUIDsKey;
*/
- (void)startAdvertising:(nullable NSDictionary<NSString *, id> *)advertisementData;
//停止发送广播
- (void)stopAdvertising;
//设置一个连接的具体central设备的延时 枚举如下
/*
typedef NS_ENUM(NSInteger, CBPeripheralManagerConnectionLatency) {
    CBPeripheralManagerConnectionLatencyLow = 0,
    CBPeripheralManagerConnectionLatencyMedium,
    CBPeripheralManagerConnectionLatencyHigh
} NS_ENUM_AVAILABLE(NA, 6_0);
*/
- (void)setDesiredConnectionLatency:(CBPeripheralManagerConnectionLatency)latency forCentral:(CBCentral *)central;
//添加一个服务
- (void)addService:(CBMutableService *)service;
//移除一个服务
- (void)removeService:(CBMutableService *)service;
//移除所有服务
- (void)removeAllServices;
//响应中心设备的读写请求
- (void)respondToRequest:(CBATTRequest *)request withResult:(CBATTError)result;
//更新一个连接中心设备的订阅特征值
- (BOOL)updateValue:(NSData *)value forCharacteristic:(CBMutableCharacteristic *)characteristic onSubscribedCentrals:(nullable NSArray<CBCentral *> *)centrals;

The related methods of the peripheral agent are as follows:

//这个方法是必须实现的 状态可用后可以发送广播
- (void)peripheralManagerDidUpdateState:(CBPeripheralManager *)peripheral;
//连接回复时调用的方法 和centralManager类似
- (void)peripheralManager:(CBPeripheralManager *)peripheral willRestoreState:(NSDictionary<NSString *, id> *)dict;
//开始发送广播时调用的方法
- (void)peripheralManagerDidStartAdvertising:(CBPeripheralManager *)peripheral error:(nullable NSError *)error;
//添加服务调用的回调
- (void)peripheralManager:(CBPeripheralManager *)peripheral didAddService:(CBService *)service error:(nullable NSError *)error;
//当一个central设备订阅一个特征值时调用的方法
- (void)peripheralManager:(CBPeripheralManager *)peripheral central:(CBCentral *)central didSubscribeToCharacteristic:(CBCharacteristic *)characteristic;
//取消订阅一个特征值时调用的方法
- (void)peripheralManager:(CBPeripheralManager *)peripheral central:(CBCentral *)central didUnsubscribeFromCharacteristic:(CBCharacteristic *)characteristic;
//收到读请求时触发的方法
- (void)peripheralManager:(CBPeripheralManager *)peripheral didReceiveReadRequest:(CBATTRequest *)request;
//收到写请求时触发的方法
- (void)peripheralManager:(CBPeripheralManager *)peripheral didReceiveWriteRequests:(NSArray<CBATTRequest *> *)requests;
//外设准备更新特征值时调用的方法
- (void)peripheralManagerIsReadyToUpdateSubscribers:(CBPeripheralManager *)peripheral;

4. Central equipment and peripheral objects CBCentral and CBPeripheral

The central device management class and the peripheral device management class are introduced above. These classes are used to establish the device connection, and the data exchange services and some information of the appliance are in the corresponding device objects.

1. Properties and methods of central device CBCentral

//设备UUID
@property(readonly, nonatomic) NSUUID *identifier;
//中心设备最大接收的数据长度
@property(readonly, nonatomic) NSUInteger maximumUpdateValueLength;

2. Peripheral CAPeripheral properties and methods

The peripheral object is much more complicated than the central object. When the centralManager is connected to the peripheral, it needs to exchange data through the proxy method of the peripheral object. The main method attributes are as follows:

//设置代理
@property(assign, nonatomic, nullable) id<CBPeripheralDelegate> delegate;
//外设name
@property(retain, readonly, nullable) NSString *name;
//信号强度
@property(retain, readonly, nullable) NSNumber *RSSI NS_DEPRECATED(NA, NA, 5_0, 8_0);
//外设状态
/*
typedef NS_ENUM(NSInteger, CBPeripheralState) {
    CBPeripheralStateDisconnected = 0,//未连接
    CBPeripheralStateConnecting,//正在链接
    CBPeripheralStateConnected,//已经连接
    CBPeripheralStateDisconnecting NS_AVAILABLE(NA, 9_0),//正在断开连接
} NS_AVAILABLE(NA, 7_0);
*/
@property(readonly) CBPeripheralState state;
//所有的服务数组
@property(retain, readonly, nullable) NSArray<CBService *> *services;
//获取当前信号强度
- (void)readRSSI;
//根据服务UUID寻找服务对象
- (void)discoverServices:(nullable NSArray<CBUUID *> *)serviceUUIDs;
//在服务对象UUID数组中寻找特定服务
- (void)discoverIncludedServices:(nullable NSArray<CBUUID *> *)includedServiceUUIDs forService:(CBService *)service;
//在一个服务中寻找特征值
- (void)discoverCharacteristics:(nullable NSArray<CBUUID *> *)characteristicUUIDs forService:(CBService *)service;
//从一个特征中读取数据
- (void)readValueForCharacteristic:(CBCharacteristic *)characteristic;
//写数据的最大长度
//type枚举如下
/*
typedef NS_ENUM(NSInteger, CBCharacteristicWriteType) {
    CBCharacteristicWriteWithResponse = 0,//写数据并且接收成功与否回执
    CBCharacteristicWriteWithoutResponse,//写数据不接收回执
};
*/
- (NSUInteger)maximumWriteValueLengthForType:(CBCharacteristicWriteType)type NS_AVAILABLE(NA, 9_0);
//向某个特征中写数据
- (void)writeValue:(NSData *)data forCharacteristic:(CBCharacteristic *)characteristic type:(CBCharacteristicWriteType)type;
//为制定的特征值设置监听通知
- (void)setNotifyValue:(BOOL)enabled forCharacteristic:(CBCharacteristic *)characteristic;
//寻找特征值的描述
- (void)discoverDescriptorsForCharacteristic:(CBCharacteristic *)characteristic;
//读取特征的描述值
- (void)readValueForDescriptor:(CBDescriptor *)descriptor;
//写特征的描述值
- (void)writeValue:(NSData *)data forDescriptor:(CBDescriptor *)descriptor;

The proxy method of the peripheral is as follows:

//外设名称更改时回调的方法
- (void)peripheralDidUpdateName:(CBPeripheral *)peripheral NS_AVAILABLE(NA, 6_0);
//外设服务变化时回调的方法
- (void)peripheral:(CBPeripheral *)peripheral didModifyServices:(NSArray<CBService *> *)invalidatedServices NS_AVAILABLE(NA, 7_0);
//信号强度改变时调用的方法
- (void)peripheralDidUpdateRSSI:(CBPeripheral *)peripheral error:(nullable NSError *)error NS_DEPRECATED(NA, NA, 5_0, 8_0);
//读取信号强度回调的方法
- (void)peripheral:(CBPeripheral *)peripheral didReadRSSI:(NSNumber *)RSSI error:(nullable NSError *)error NS_AVAILABLE(NA, 8_0);
//发现服务时调用的方法
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(nullable NSError *)error;
//在服务中发现子服务回调的方法
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverIncludedServicesForService:(CBService *)service error:(nullable NSError *)error;
//发现服务的特征值后回调的方法
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(nullable NSError *)error;
//特征值更新时回调的方法
- (void)peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic error:(nullable NSError *)error;
//向特征值写数据时回调的方法
 - (void)peripheral:(CBPeripheral *)peripheral didWriteValueForCharacteristic:(CBCharacteristic *)characteristic error:(nullable NSError *)error;
 //特征值的通知设置改变时触发的方法
 - (void)peripheral:(CBPeripheral *)peripheral didUpdateNotificationStateForCharacteristic:(CBCharacteristic *)characteristic error:(nullable NSError *)error;
 //发现特征值的描述信息触发的方法
 - (void)peripheral:(CBPeripheral *)peripheral didDiscoverDescriptorsForCharacteristic:(CBCharacteristic *)characteristic error:(nullable NSError *)error;
 //特征的描述值更新时触发的方法
 - (void)peripheral:(CBPeripheral *)peripheral didUpdateValueForDescriptor:(CBDescriptor *)descriptor error:(nullable NSError *)error;
 //写描述信息时触发的方法
 - (void)peripheral:(CBPeripheral *)peripheral didWriteValueForDescriptor:(CBDescriptor *)descriptor error:(nullable NSError *)error;

Five, the service object CBService

Service objects are used to manage some data services provided by peripherals, and the attributes are as follows:

//对应的外设
@property(assign, readonly, nonatomic) CBPeripheral *peripheral;
//是否是初等服务
@property(readonly, nonatomic) BOOL isPrimary;
//包含的自服务
@property(retain, readonly, nullable) NSArray<CBService *> *includedServices;
//服务中的特征值
@property(retain, readonly, nullable) NSArray<CBCharacteristic *> *characteristics;

6. The characteristic value of the service CBCharacteristic

Data read and write operations are performed by binding the characteristic values ​​in the service, where the properties are as follows:

//对应的服务对象
@property(assign, readonly, nonatomic) CBService *service;
//特征值的属性 枚举如下
/*
typedef NS_OPTIONS(NSUInteger, CBCharacteristicProperties) {
    CBCharacteristicPropertyBroadcast,//允许广播特征
    CBCharacteristicPropertyRead,//可读属性
    CBCharacteristicPropertyWriteWithoutResponse,//可写并且接收回执
    CBCharacteristicPropertyWrite,//可写属性
    CBCharacteristicPropertyNotify,//可通知属性
    CBCharacteristicPropertyIndicate,//可展现的特征值
    CBCharacteristicPropertyAuthenticatedSignedWrites,//允许签名的特征值写入
    CBCharacteristicPropertyExtendedProperties,
    CBCharacteristicPropertyNotifyEncryptionRequired,
    CBCharacteristicPropertyIndicateEncryptionRequired
};
*/
@property(readonly, nonatomic) CBCharacteristicProperties properties;
//特征值的数据
@property(retain, readonly, nullable) NSData *value;
//特征值的描述
@property(retain, readonly, nullable) NSArray<CBDescriptor *> *descriptors;
//是否是当前广播的特征
@property(readonly) BOOL isBroadcasted;
//是否是正在通知的特征
@property(readonly) BOOL isNotifying;

Seven, read and write request object CBATTRequest

The service object is the relevant data service provided by the peripheral device to the central device. After obtaining the corresponding service, the central device can make read and write requests. The properties of the read and write objects are as follows:

//对应的中心设备
@property(readonly, nonatomic) CBCentral *central;
//对应的特征值
@property(readonly, nonatomic) CBCharacteristic *characteristic;
//读写数据值
@property(readwrite, copy, nullable) NSData *value;    

Here I wrote a lightweight bluetooth library GJLightBlueTooth , which includes basic bluetooth scanning connection operations.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324416408&siteId=291194637
Recommended