如果你進(jìn)來了,點(diǎn)下關(guān)注行不行_
CBCentralManager 中心設(shè)備管理類#
//設(shè)置中心設(shè)備代理
@property(assign, nonatomic, nullable) id<CBCentralManagerDelegate> delegate;
//中心設(shè)備當(dāng)前狀態(tài)
@property(readonly) CBCentralManagerState state;
//中心設(shè)備是否正在掃描
@property(readonly) BOOL isScanning NS_AVAILABLE(NA, 9_0);
CBCentralManagerState 設(shè)備狀態(tài)#
typedef NS_ENUM(NSInteger, CBCentralManagerState) {
//狀態(tài)未知
CBCentralManagerStateUnknown = 0,
//連接斷開 即將重置
CBCentralManagerStateResetting,
//該平臺(tái)不支持藍(lán)牙
CBCentralManagerStateUnsupported,
//未授權(quán)藍(lán)牙使用
CBCentralManagerStateUnauthorized,
//藍(lán)牙關(guān)閉
CBCentralManagerStatePoweredOff,
//藍(lán)牙正常開啟
CBCentralManagerStatePoweredOn,
};
初始化CBCentralManager方法#
//初始化方法
//設(shè)置的代理需要遵守CBCentralManagerDelegate協(xié)議
//queue可以設(shè)置藍(lán)牙掃描的線程 傳入nil則為在主線程中進(jìn)行
- (instancetype)initWithDelegate:(nullable id<CBCentralManagerDelegate>)delegate
queue:(nullable dispatch_queue_t)queue;
//此方法同上 在options字典中用于進(jìn)行一些管理中心的初始化屬性設(shè)置
//字典中支持的鍵值如下
/*
NSString * const CBCentralManagerOptionShowPowerAlertKey 對(duì)應(yīng)一個(gè)NSNumber類型的bool值扒腕,用于設(shè)置是否在關(guān)閉藍(lán)牙時(shí)彈出用戶提示
NSString * const CBCentralManagerOptionRestoreIdentifierKey 對(duì)應(yīng)一個(gè)NSString對(duì)象绞绒,設(shè)置管理中心的標(biāo)識(shí)符ID
*/
- (instancetype)initWithDelegate:(nullable id<CBCentralManagerDelegate>)delegate
queue:(nullable dispatch_queue_t)queue
options:(nullable NSDictionary<NSString *, id> *)options;
根據(jù)獲取連接設(shè)備列表#
//?根據(jù)服務(wù)NSUUID,獲取所有設(shè)備,并返回一個(gè)列表
- (NSArray<CBPeripheral *> *)retrievePeripheralsWithIdentifiers:(NSArray<NSUUID *> *)identifiers;
//根據(jù)服務(wù)id獲取所有連接的設(shè)備
- (NSArray<CBPeripheral *> *)retrieveConnectedPeripheralsWithServices:(NSArray<CBUUID *> *)serviceUUIDs;
初始化CBCentralManager后,會(huì)自動(dòng)回調(diào)代理方法#
//這個(gè)方法中可以獲取到管理中心的狀態(tài)
- (void)centralManagerDidUpdateState:(CBCentralManager *)central;
掃描藍(lán)牙設(shè)備方法#
// serviceUUIDs : 用于掃描一個(gè)特點(diǎn)ID的外設(shè)
// options : 用于設(shè)置一些掃描屬性 鍵值如下
/*
//是否允許重復(fù)掃描 對(duì)應(yīng)NSNumber的bool值,默認(rèn)為NO透敌,會(huì)自動(dòng)去重
NSString *const CBCentralManagerScanOptionAllowDuplicatesKey;
//要掃描的設(shè)備UUID 數(shù)組 對(duì)應(yīng)NSArray
NSString *const CBCentralManagerScanOptionSolicitedServiceUUIDsKey;
*/
- (void)scanForPeripheralsWithServices:(nullable NSArray<CBUUID *> *)serviceUUIDs options:(nullable NSDictionary<NSString *, id> *)options;
停止藍(lán)牙掃描的方法#
- (void)stopScan;
掃描成功的方法#
// peripheral 掃描到的外設(shè)
// advertisementData是外設(shè)發(fā)送的廣播數(shù)據(jù)
// RSSI 是信號(hào)強(qiáng)度
- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary<NSString *, id> *)advertisementData RSSI:(NSNumber *)RSSI;
連接設(shè)備的方法#
/*
options中可以設(shè)置一些連接設(shè)備的初始屬性鍵值如下
//對(duì)應(yīng)NSNumber的bool值,設(shè)置當(dāng)外設(shè)連接后是否彈出一個(gè)警告
NSString *const CBConnectPeripheralOptionNotifyOnConnectionKey;
//對(duì)應(yīng)NSNumber的bool值,設(shè)置當(dāng)外設(shè)斷開連接后是否彈出一個(gè)警告
NSString *const CBConnectPeripheralOptionNotifyOnDisconnectionKey;
//對(duì)應(yīng)NSNumber的bool值,設(shè)置當(dāng)外設(shè)暫停連接后是否彈出一個(gè)警告
NSString *const CBConnectPeripheralOptionNotifyOnNotificationKey;
*/
- (void)connectPeripheral:(CBPeripheral *)peripheral options:(nullable NSDictionary<NSString *, id> *)options;
取消連接設(shè)備的方法#
//取消一個(gè)外設(shè)的連接
- (void)cancelPeripheralConnection:(CBPeripheral *)peripheral;
連接后處理的方法#
//連接外設(shè)成功
- (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral;
//連接外設(shè)失敗
- (void)centralManager:(CBCentralManager *)central didFailToConnectPeripheral:(CBPeripheral *)peripheral error:(nullable NSError *)error;
//斷開外設(shè)連接
- (void)centralManager:(CBCentralManager *)central didDisconnectPeripheral:(CBPeripheral *)peripheral error:(nullable NSError *)error;
當(dāng)管理中心恢復(fù)時(shí)會(huì)調(diào)用如下方法#
//dict中會(huì)傳入如下鍵值對(duì)
/*
//恢復(fù)連接的外設(shè)數(shù)組
NSString *const CBCentralManagerRestoredStatePeripheralsKey;
//恢復(fù)連接的服務(wù)UUID數(shù)組
NSString *const CBCentralManagerRestoredStateScanServicesKey;
//恢復(fù)連接的外設(shè)掃描屬性字典數(shù)組
NSString *const CBCentralManagerRestoredStateScanOptionsKey;
*/
- (void)centralManager:(CBCentralManager *)central willRestoreState:(NSDictionary<NSString *, id> *)dict;
外設(shè)的設(shè)備的方法,(找服務(wù),找特征)#
//設(shè)置代理
@property(assign, nonatomic, nullable) id<CBPeripheralDelegate> delegate;
//外設(shè)name
@property(retain, readonly, nullable) NSString *name;
//信號(hào)強(qiáng)度
@property(retain, readonly, nullable) NSNumber *RSSI NS_DEPRECATED(NA, NA, 5_0, 8_0);
//外設(shè)狀態(tài)
/*
typedef NS_ENUM(NSInteger, CBPeripheralState) {
CBPeripheralStateDisconnected = 0,//未連接
CBPeripheralStateConnecting,//正在鏈接
CBPeripheralStateConnected,//已經(jīng)連接
CBPeripheralStateDisconnecting NS_AVAILABLE(NA, 9_0),//正在斷開連接
} NS_AVAILABLE(NA, 7_0);
*/
@property(readonly) CBPeripheralState state;
//所有的服務(wù)數(shù)組
@property(retain, readonly, nullable) NSArray<CBService *> *services;
//獲取當(dāng)前信號(hào)強(qiáng)度
- (void)readRSSI;
//根據(jù)服務(wù)UUID尋找服務(wù)對(duì)象
- (void)discoverServices:(nullable NSArray<CBUUID *> *)serviceUUIDs;
//在服務(wù)對(duì)象UUID數(shù)組中尋找特定服務(wù)
- (void)discoverIncludedServices:(nullable NSArray<CBUUID *> *)includedServiceUUIDs forService:(CBService *)service;
//在一個(gè)服務(wù)中尋找特征值
- (void)discoverCharacteristics:(nullable NSArray<CBUUID *> *)characteristicUUIDs forService:(CBService *)service;
//從一個(gè)特征中讀取數(shù)據(jù)
- (void)readValueForCharacteristic:(CBCharacteristic *)characteristic;
//寫數(shù)據(jù)的最大長(zhǎng)度
//type枚舉如下
/*
typedef NS_ENUM(NSInteger, CBCharacteristicWriteType) {
CBCharacteristicWriteWithResponse = 0,//寫數(shù)據(jù)并且接收成功與否回執(zhí)
CBCharacteristicWriteWithoutResponse,//寫數(shù)據(jù)不接收回執(zhí)
};
*/
- (NSUInteger)maximumWriteValueLengthForType:(CBCharacteristicWriteType)type NS_AVAILABLE(NA, 9_0);
//向某個(gè)特征中寫數(shù)據(jù)
- (void)writeValue:(NSData *)data forCharacteristic:(CBCharacteristic *)characteristic type:(CBCharacteristicWriteType)type;
//為制定的特征值設(shè)置監(jiān)聽通知
- (void)setNotifyValue:(BOOL)enabled forCharacteristic:(CBCharacteristic *)characteristic;
//尋找特征值的描述
- (void)discoverDescriptorsForCharacteristic:(CBCharacteristic *)characteristic;
//讀取特征的描述值
- (void)readValueForDescriptor:(CBDescriptor *)descriptor;
//寫特征的描述值
- (void)writeValue:(NSData *)data forDescriptor:(CBDescriptor *)descriptor;
外設(shè)方法的回調(diào),代理方法#
//外設(shè)名稱更改時(shí)回調(diào)的方法
- (void)peripheralDidUpdateName:(CBPeripheral *)peripheral NS_AVAILABLE(NA, 6_0);
//外設(shè)服務(wù)變化時(shí)回調(diào)的方法
- (void)peripheral:(CBPeripheral *)peripheral didModifyServices:(NSArray<CBService *> *)invalidatedServices NS_AVAILABLE(NA, 7_0);
//信號(hào)強(qiáng)度改變時(shí)調(diào)用的方法
- (void)peripheralDidUpdateRSSI:(CBPeripheral *)peripheral error:(nullable NSError *)error NS_DEPRECATED(NA, NA, 5_0, 8_0);
//讀取信號(hào)強(qiáng)度回調(diào)的方法
- (void)peripheral:(CBPeripheral *)peripheral didReadRSSI:(NSNumber *)RSSI error:(nullable NSError *)error NS_AVAILABLE(NA, 8_0);
//發(fā)現(xiàn)服務(wù)時(shí)調(diào)用的方法
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(nullable NSError *)error;
//在服務(wù)中發(fā)現(xiàn)子服務(wù)回調(diào)的方法
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverIncludedServicesForService:(CBService *)service error:(nullable NSError *)error;
//發(fā)現(xiàn)服務(wù)的特征值后回調(diào)的方法
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(nullable NSError *)error;
//特征值更新時(shí)回調(diào)的方法
- (void)peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic error:(nullable NSError *)error;
//向特征值寫數(shù)據(jù)時(shí)回調(diào)的方法
- (void)peripheral:(CBPeripheral *)peripheral didWriteValueForCharacteristic:(CBCharacteristic *)characteristic error:(nullable NSError *)error;
//特征值的通知設(shè)置改變時(shí)觸發(fā)的方法
- (void)peripheral:(CBPeripheral *)peripheral didUpdateNotificationStateForCharacteristic:(CBCharacteristic *)characteristic error:(nullable NSError *)error;
//發(fā)現(xiàn)特征值的描述信息觸發(fā)的方法
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverDescriptorsForCharacteristic:(CBCharacteristic *)characteristic error:(nullable NSError *)error;
//特征的描述值更新時(shí)觸發(fā)的方法
- (void)peripheral:(CBPeripheral *)peripheral didUpdateValueForDescriptor:(CBDescriptor *)descriptor error:(nullable NSError *)error;
//寫描述信息時(shí)觸發(fā)的方法
- (void)peripheral:(CBPeripheral *)peripheral didWriteValueForDescriptor:(CBDescriptor *)descriptor error:(nullable NSError *)error;
CBService服務(wù)的方法#
//對(duì)應(yīng)的外設(shè)
@property(assign, readonly, nonatomic) CBPeripheral *peripheral;
//是否是初等服務(wù)
@property(readonly, nonatomic) BOOL isPrimary;
//包含的自服務(wù)
@property(retain, readonly, nullable) NSArray<CBService *> *includedServices;
//服務(wù)中的特征值
@property(retain, readonly, nullable) NSArray<CBCharacteristic *> *characteristics;
Characteristic特征的方法#
//對(duì)應(yīng)的服務(wù)對(duì)象
@property(assign, readonly, nonatomic) CBService *service;
//特征值的屬性 枚舉如下
/*
typedef NS_OPTIONS(NSUInteger, CBCharacteristicProperties) {
CBCharacteristicPropertyBroadcast,//允許廣播特征
CBCharacteristicPropertyRead,//可讀屬性
CBCharacteristicPropertyWriteWithoutResponse,//可寫并且接收回執(zhí)
CBCharacteristicPropertyWrite,//可寫屬性
CBCharacteristicPropertyNotify,//可通知屬性
CBCharacteristicPropertyIndicate,//可展現(xiàn)的特征值
CBCharacteristicPropertyAuthenticatedSignedWrites,//允許簽名的特征值寫入
CBCharacteristicPropertyExtendedProperties,
CBCharacteristicPropertyNotifyEncryptionRequired,
CBCharacteristicPropertyIndicateEncryptionRequired
};
*/
@property(readonly, nonatomic) CBCharacteristicProperties properties;
//特征值的數(shù)據(jù)
@property(retain, readonly, nullable) NSData *value;
//特征值的描述
@property(retain, readonly, nullable) NSArray<CBDescriptor *> *descriptors;
//是否是當(dāng)前廣播的特征
@property(readonly) BOOL isBroadcasted;
//是否是正在通知的特征
@property(readonly) BOOL isNotifying;