iOS開發之藍牙通信

iOS開發之藍牙通信

1、引言

        藍牙是設備近距離通訊的一種方便手段,在iPhone引入藍牙4.0後,設備之間的通信變得更加簡單。相關的藍牙操做由專門的CoreBluetooth.framework進行統一管理。經過藍牙進行通信交互分爲兩方,一方爲中心設備central,一方爲外設peripheral,外設經過廣播的方式向外發送信息,中心設備檢索到外設發的廣播信息,能夠進行配對鏈接,進而進行數據交互。數組

2、中心設備CBCentralManager

        CBCentralManager是管理中心設備的管理類,其中重要方法以下:ide

//設置中心設備代理
@property(assign, nonatomic, nullable) id<CBCentralManagerDelegate> delegate;
//中心設備當前狀態
@property(readonly) CBCentralManagerState state;
//中心設備是否正在掃描
@property(readonly) BOOL isScanning NS_AVAILABLE(NA, 9_0);

   其中state是一個枚舉,有關藍牙是否可用的狀態以下:ui

typedef NS_ENUM(NSInteger, CBCentralManagerState) {
        //狀態未知
	CBCentralManagerStateUnknown = 0,
	//鏈接斷開 即將重置
	CBCentralManagerStateResetting,
	//該平臺不支持藍牙
	CBCentralManagerStateUnsupported,
	//未受權藍牙使用
	CBCentralManagerStateUnauthorized,
	//藍牙關閉
	CBCentralManagerStatePoweredOff,
	//藍牙正常開啓
	CBCentralManagerStatePoweredOn,
};

下面這些方法用於初始化管理中心:atom

//初始化方法
//設置的代理須要遵照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;

在初始化管理中心完成後,會回調代理中的以下方法,咱們必須實現以下方法:spa

//這個方法中能夠獲取到管理中心的狀態
- (void)centralManagerDidUpdateState:(CBCentralManager *)central;

若是上面方法中管理中心狀態爲藍牙可用,能夠經過下面方法開啓掃描外設:線程

//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;

掃描的結果會在以下代理方法中回掉:代理

//peripheral 掃描到的外設
//advertisementData是外設發送的廣播數據
//RSSI 是信號強度
- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary<NSString *, id> *)advertisementData RSSI:(NSNumber *)RSSI;

 

掃描到外設後,經過下面方法能夠鏈接一個外設:code

/*
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;

調用過鏈接外設的方法後,會回掉以下代理方法:對象

//鏈接外設成功
- (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;

當管理中心恢復時會調用以下代理:ip

//dict中會傳入以下鍵值對
/*
//恢復鏈接的外設數組
NSString *const CBCentralManagerRestoredStatePeripheralsKey;
//恢復鏈接的服務UUID數組
NSString *const CBCentralManagerRestoredStateScanServicesKey;
//恢復鏈接的外設掃描屬性字典數組
NSString *const CBCentralManagerRestoredStateScanOptionsKey;
*/
- (void)centralManager:(CBCentralManager *)central willRestoreState:(NSDictionary<NSString *, id> *)dict;

3、外設CBPeripheralManager

        從上面咱們知道,中心設備是用來掃描周圍的外設,兩臺設備的通信中,必須有一個充當中心設備,一個充當外設,外設是由CBPeripheralManager進行管理,主要方法以下:

//設置外設管理中心代理
@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;

外設代理的相關方法以下:

//這個方法是必須實現的 狀態可用後能夠發送廣播
- (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、中心設備與外設對象CBCentral與CBPeripheral

        上面介紹了中心設備管理類與外設管理類,這些類用於將設備鏈接創建起來,器具的數據交換的服務和一些信息則是在對應的設備對象中。

一、中心設備 CBCentral屬性與方法

//設備UUID
@property(readonly, nonatomic) NSUUID *identifier;
//中心設備最大接收的數據長度
@property(readonly, nonatomic) NSUInteger maximumUpdateValueLength;

二、外設CAPeripheral屬性與方法

        外設對象要比中心對象複雜的多,當centralManager鏈接到外設後,須要經過外設對象的代理方法進行數據交互,其中主要方法屬性以下:

//設置代理
@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;

外設的代理方法以下:

//外設名稱更改時回調的方法
- (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;

5、服務對象CBService

    服務對象是用來管理外設提供的一些數據服務的,其中屬性以下:

//對應的外設
@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、服務的特徵值CBCharacteristic

        經過綁定服務中的特徵值來進行數據的讀寫操做,其中屬性以下:

//對應的服務對象
@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;

7、讀寫請求對象CBATTRequest

        服務對象是外設向中心設備提供的相關數據服務,獲取到相應服務後,中心設備能夠進行讀寫請求,讀寫對象屬性以下:

//對應的中心設備
@property(readonly, nonatomic) CBCentral *central;
//對應的特徵值
@property(readonly, nonatomic) CBCharacteristic *characteristic;
//讀寫數據值
@property(readwrite, copy, nullable) NSData *value;

 

專一技術,熱愛生活,交流技術,也作朋友。

——琿少 QQ羣:203317592

相關文章
相關標籤/搜索