來自:https://my.oschina.net/u/2340880/blog/548127
iOS開發(fā)之藍牙通訊
一、引言
????????藍牙是設備近距離通信的一種方便手段,在iPhone引入藍牙4.0后,設備之間的通訊變得更加簡單。相關的藍牙操作由專門的CoreBluetooth.framework進行統(tǒng)一管理。通過藍牙進行通訊交互分為兩方,一方為中心設備central,一方為外設peripheral,外設通過廣播的方式向外發(fā)送信息,中心設備檢索到外設發(fā)的廣播信息,可以進行配對連接,進而進行數(shù)據(jù)交互。
二、中心設備CBCentralManager
CBCentralManager是管理中心設備的管理類,其中重要方法如下:
//設置中心設備代理
@property(assign,nonatomic,nullable)id?delegate;
//中心設備當前狀態(tài)
@property(readonly)?CBCentralManagerState?state;
//中心設備是否正在掃描
@property(readonly)BOOLisScanningNS_AVAILABLE(NA,9_0);
? ?其中state是一個枚舉,有關藍牙是否可用的狀態(tài)如下:
typedefNS_ENUM(NSInteger,?CBCentralManagerState)?{
CBCentralManagerStateUnknown?=0,?//狀態(tài)未知
CBCentralManagerStateResetting, //連接斷開?即將重置
CBCentralManagerStateUnsupported, //該平臺不支持藍牙
CBCentralManagerStateUnauthorized, //未授權藍牙使用
CBCentralManagerStatePoweredOff, //藍牙關閉
CBCentralManagerStatePoweredOn, //藍牙正常開啟};
下面這些方法用于初始化管理中心:
//初始化方法//設置的代理需要遵守CBCentralManagerDelegate協(xié)議//queue可以設置藍牙掃描的線程?傳入nil則為在主線程中進行
-?(instancetype)initWithDelegate:(nullableid)delegate???queue:(nullabledispatch_queue_t)queue;
//此方法同上?在options字典中用于進行一些管理中心的初始化屬性設置//字典中支持的鍵值如下/*
NSString?*?const?CBCentralManagerOptionShowPowerAlertKey?對應一個NSNumber類型的bool值,用于設置是否在關閉藍牙時彈出用戶提示
NSString?*?const?CBCentralManagerOptionRestoreIdentifierKey?對應一個NSString對象,設置管理中心的標識符ID
*/-?(instancetype)initWithDelegate:(nullableid)delegate???queue:(nullabledispatch_queue_t)queue?options:(nullableNSDictionary?*)options;
-?(NSArray?*)retrievePeripheralsWithIdentifiers:(NSArray?*)identifiers;?//根據(jù)獲取所有已知設備
-?(NSArray?*)retrieveConnectedPeripheralsWithServices:(NSArray?*)serviceUUIDs;?//根據(jù)服務id獲取所有連接的設備
在初始化管理中心完成后,會回調(diào)代理中的如下方法,我們必須實現(xiàn)如下方法:
-?(void)centralManagerDidUpdateState:(CBCentralManager?*)central;?//這個方法中可以獲取到管理中心的狀態(tài)
如果上面方法中管理中心狀態(tài)為藍牙可用,可以通過下面方法開啟掃描外設:
//serviceUUIDs用于掃描一個特點ID的外設?options用于設置一些掃描屬性?鍵值如下
/*
//是否允許重復掃描?對應NSNumber的bool值,默認為NO,會自動去重
NSString?*const?CBCentralManagerScanOptionAllowDuplicatesKey;
//要掃描的設備UUID?數(shù)組?對應NSArray
NSString?*const?CBCentralManagerScanOptionSolicitedServiceUUIDsKey;
*/
-?(void)scanForPeripheralsWithServices:(nullableNSArray?*)serviceUUIDs?options:(nullableNSDictionary?*)options;
-?(void)stopScan;?//停止掃描外設
掃描的結果會在如下代理方法中回掉:
-?(void)centralManager:(CBCentralManager?*)central?didDiscoverPeripheral:(CBPeripheral?*)peripheral?advertisementData:(NSDictionary?*)advertisementData?RSSI:(NSNumber*)RSSI;? //peripheral?掃描到的外設//advertisementData是外設發(fā)送的廣播數(shù)據(jù)//RSSI?是信號強度
掃描到外設后,通過下面方法可以連接一個外設:
/*
options中可以設置一些連接設備的初始屬性鍵值如下
//對應NSNumber的bool值,設置當外設連接后是否彈出一個警告
NSString?*const?CBConnectPeripheralOptionNotifyOnConnectionKey;
//對應NSNumber的bool值,設置當外設斷開連接后是否彈出一個警告
NSString?*const?CBConnectPeripheralOptionNotifyOnDisconnectionKey;
//對應NSNumber的bool值,設置當外設暫停連接后是否彈出一個警告
NSString?*const?CBConnectPeripheralOptionNotifyOnNotificationKey;
*/
-?(void)connectPeripheral:(CBPeripheral?*)peripheral?options:(nullableNSDictionary?*)options;
-?(void)cancelPeripheralConnection:(CBPeripheral?*)peripheral;?//取消一個外設的連接
調(diào)用過連接外設的方法后,會回掉如下代理方法:
-?(void)centralManager:(CBCentralManager?*)centraldidConnectPeripheral: (CBPeripheral?*)peripheral; //連接外設成功
-?(void)centralManager:(CBCentralManager?*)centraldidFailToConnectPeripheral:(CBPeripheral?*)peripheralerror:(nullable?NSError?*)error; //連接外設敗
-?(void)centralManager:(CBCentralManager?*)centraldidDisconnectPeripheral:(CBPeripheral?*)peripheralerror:(nullable?NSError?*)error; //斷開外設連接
當管理中心恢復時會調(diào)用如下代理:
//dict中會傳入如下鍵值對/*
//恢復連接的外設數(shù)組
NSString?*const?CBCentralManagerRestoredStatePeripheralsKey;
//恢復連接的服務UUID數(shù)組
NSString?*const?CBCentralManagerRestoredStateScanServicesKey;
//恢復連接的外設掃描屬性字典數(shù)組
NSString?*const?CBCentralManagerRestoredStateScanOptionsKey;
*/
-?(void)centralManager:(CBCentralManager?*)central?willRestoreState:(NSDictionary?*)dict;
三、外設CBPeripheralManager
????????從上面我們知道,中心設備是用來掃描周圍的外設,兩臺設備的通訊中,必須有一個充當中心設備,一個充當外設,外設是由CBPeripheralManager進行管理,主要方法如下:
@property(assign,nonatomic,nullable)id?delegate; //設置外設管理中心代理@property(readonly)?CBPeripheralManagerState?state; //外設狀態(tài)?枚舉如中心設備@property(readonly)BOOLisAdvertising; //是否正在發(fā)送廣播+?(CBPeripheralManagerAuthorizationStatus)authorizationStatus; //用戶的授權狀態(tài)
-?(instancetype)initWithDelegate:(nullableid)delegate???queue:(nullabledispatch_queue_t);-?(instancetype)initWithDelegate:(nullableid)delegate???queue:(nullabledispatch_queue_t)queue?options:(nullableNSDictionary?*)options; //初始化并設置代理?參數(shù)的具體含義與中心設備管理中心
//開始發(fā)送廣播//advertisementData中可以發(fā)送的數(shù)據(jù)有約定?如下
/*
對應設置NSString類型的廣播名
NSString?*const?CBAdvertisementDataLocalNameKey;
外設制造商的NSData數(shù)據(jù)
NSString?*const?CBAdvertisementDataManufacturerDataKey;
外設制造商的CBUUID數(shù)據(jù)
NSString?*const?CBAdvertisementDataServiceDataKey;
服務的UUID與其對應的服務數(shù)據(jù)字典數(shù)組
NSString?*const?CBAdvertisementDataServiceUUIDsKey;
附加服務的UUID數(shù)組
NSString?*const?CBAdvertisementDataOverflowServiceUUIDsKey;
外設的發(fā)送功率?NSNumber類型
NSString?*const?CBAdvertisementDataTxPowerLevelKey;
外設是否可以連接
NSString?*const?CBAdvertisementDataIsConnectable;
服務的UUID數(shù)組
NSString?*const?CBAdvertisementDataSolicitedServiceUUIDsKey;
*/
-?(void)startAdvertising:(nullableNSDictionary?*)advertisementData; //停止發(fā)送廣播-?(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:(nullableNSArray?*)centrals;?//更新一個連接中心設備的訂閱特征值
外設代理的相關方法如下:
-?(void)peripheralManagerDidUpdateState:(CBPeripheralManager?*)peripheral; //這個方法是必須實現(xiàn)的?狀態(tài)可用后可以發(fā)送廣播
-?(void)peripheralManager:(CBPeripheralManager?*)peripheralwillRestoreState:(NSDictionary?*)dict;?//連接回復時調(diào)用的方法?和centralManager類似
-?(void)peripheralManagerDidStartAdvertising:(CBPeripheralManager?*)peripheralerror:(nullable?NSError?*)error;?//開始發(fā)送廣播時調(diào)用的方法
-?(void)peripheralManager:(CBPeripheralManager?*)peripheral didAddService:(CBService?*)serviceerror:(nullable?NSError?*)error;?//添加服務調(diào)用的回調(diào)
-?(void)peripheralManager:(CBPeripheralManager?*)peripheralcentral:(CBCentral?*)centraldidSubscribeToCharacteristic:(CBCharacteristic?*)characteristic; //當一個central設備訂閱一個特征值時調(diào)用的方法
-?(void)peripheralManager:(CBPeripheralManager?*)peripheralcentral:(CBCentral?*)central didUnsubscribeFromCharacteristic:(CBCharacteristic?*)characteristic;?//取消訂閱一個特征值時調(diào)用的方法
-?(void)peripheralManager:(CBPeripheralManager?*)peripheral didReceiveReadRequest:(CBATTRequest?*)request; //收到讀請求時觸發(fā)的方法
-?(void)peripheralManager:(CBPeripheralManager?*)peripheral didReceiveWriteRequests:(NSArray?*)requests; //收到寫請求時觸發(fā)的方法
-?(void)peripheralManagerIsReadyToUpdateSubscribers:(CBPeripheralManager?*)peripheral;?//外設準備更新特征值時調(diào)用的方法
四、中心設備與外設對象CBCentral與CBPeripheral
????????上面介紹了中心設備管理類與外設管理類,這些類用于將設備連接建立起來,器具的數(shù)據(jù)交換的服務和一些信息則是在對應的設備對象中。
1、中心設備 CBCentral屬性與方法
@property(readonly,nonatomic)NSUUID*identifier;?//設備UUID
@property(readonly,nonatomic)NSUIntegermaximumUpdateValueLength;?//中心設備最大接收的數(shù)據(jù)長度
2、外設CAPeripheral屬性與方法
????????外設對象要比中心對象復雜的多,當centralManager連接到外設后,需要通過外設對象的代理方法進行數(shù)據(jù)交互,其中主要方法屬性如下:
@property(assign,nonatomic,nullable)id?delegate; //設置代理
@property(retain,readonly,nullable)NSString*name; //外設name
@property(retain,readonly,nullable)NSNumber *RSSI NS_DEPRECATED(NA,?NA,5_0,8_0); //信號強度
/*
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; //外設狀態(tài)
@property(retain,readonly,nullable)NSArray?*services; //所有的服務數(shù)組
-?(void)readRSSI; //獲取當前信號強度
-?(void)discoverServices:(nullableNSArray?*)serviceUUIDs; //根據(jù)服務UUID尋找服務對象
-?(void)discoverIncludedServices:(nullableNSArray?*)includedServiceUUIDs?forService:(CBService?*)service; //在服務對象UUID數(shù)組中尋找特定服務
-?(void)discoverCharacteristics:(nullableNSArray?*)characteristicUUIDs?forService:(CBService?*)service;?//在一個服務中尋找特征值
-?(void)readValueForCharacteristic:(CBCharacteristic?*)characteristic;?//從一個特征中讀取數(shù)據(jù)
//type枚舉如下
/*
typedef?NS_ENUM(NSInteger,?CBCharacteristicWriteType)?{
CBCharacteristicWriteWithResponse?=?0, //寫數(shù)據(jù)并且接收成功與否回執(zhí)
CBCharacteristicWriteWithoutResponse, //寫數(shù)據(jù)不接收回執(zhí)
};
*/
?-?(NSUInteger)maximumWriteValueLengthForType:(CBCharacteristicWriteType)typeNS_AVAILABLE(NA,9_0); //寫數(shù)據(jù)的最大長度
(void)writeValue:(NSData*)data?forCharacteristic:(CBCharacteristic?*)characteristic?type:(CBCharacteristicWriteType)type; //向某個特征中寫數(shù)據(jù)-
-?(void)setNotifyValue:(BOOL)enabled?forCharacteristic:(CBCharacteristic?*)characteristic; //為制定的特征值設置監(jiān)聽通知
-?(void)discoverDescriptorsForCharacteristic:(CBCharacteristic?*)characteristic; //尋找特征值的描述
-?(void)readValueForDescriptor:(CBDescriptor?*)descriptor; //讀取特征的描述值
-?(void)writeValue:(NSData*)data?forDescriptor:(CBDescriptor?*)descriptor; //寫特征的描述值
外設的代理方法如下:
-?(void)peripheralDidUpdateName:(CBPeripheral?*)peripheral?NS_AVAILABLE(NA,6_0); //外設名稱更改時回調(diào)的方法
-?(void)peripheral:(CBPeripheral?*)peripheraldidModifyServices:(NSArray?*)invalidatedServices?NS_AVAILABLE(NA,7_0); //外設服務變化時回調(diào)的方法
-?(void)peripheralDidUpdateRSSI:(CBPeripheral?*)peripheralerror:(nullable?NSError?*)error?NS_DEPRECATED(NA,?NA,5_0,8_0); //信號強度改變時調(diào)用的方法
-?(void)peripheral:(CBPeripheral?*)peripheraldidReadRSSI:(NSNumber?*)RSSIerror:(nullable?NSError?*)error?NS_AVAILABLE(NA,8_0); //讀取信號強度回調(diào)的方法
-?(void)peripheral:(CBPeripheral?*)peripheraldidDiscoverServices:(nullable?NSError?*)error; //發(fā)現(xiàn)服務時調(diào)用的方法
-?(void)peripheral:(CBPeripheral?*)peripheraldidDiscoverIncludedServicesForService:(CBService?*)serviceerror:(nullable?NSError?*)error; //在服務中發(fā)現(xiàn)子服務回調(diào)的方法
-?(void)peripheral:(CBPeripheral?*)peripheraldidDiscoverCharacteristicsForService:(CBService?*)serviceerror:(nullable?NSError?*)error; //發(fā)現(xiàn)服務的特征值后回調(diào)的方法
-?(void)peripheral:(CBPeripheral?*)peripheraldidUpdateValueForCharacteristic:(CBCharacteristic?*)characteristicerror:(nullable?NSError?*)error; //特征值更新時回調(diào)的方法
-?(void)peripheral:(CBPeripheral?*)peripheraldidWriteValueForCharacteristic:(CBCharacteristic?*)characteristicerror:(nullable?NSError?*)error; //向特征值寫數(shù)據(jù)時回調(diào)的方法
-?(void)peripheral:(CBPeripheral?*)peripheraldidUpdateNotificationStateForCharacteristic:(CBCharacteristic?*)characteristicerror:(nullable?NSError?*)error; //特征值的通知設置改變時觸發(fā)的方法
-?(void)peripheral:(CBPeripheral?*)peripheraldidDiscoverDescriptorsForCharacteristic:(CBCharacteristic?*)characteristicerror:(nullable?NSError?*)error; //發(fā)現(xiàn)特征值的描述信息觸發(fā)的方法
-?(void)peripheral:(CBPeripheral?*)peripheraldidUpdateValueForDescriptor:(CBDescriptor?*)descriptorerror:(nullable?NSError?*)error; //特征的描述值更新時觸發(fā)的方法
-?(void)peripheral:(CBPeripheral?*)peripheraldidWriteValueForDescriptor:(CBDescriptor?*)descriptorerror:(nullable?NSError?*)error; //寫描述信息時觸發(fā)的方法
五、服務對象CBService
????服務對象是用來管理外設提供的一些數(shù)據(jù)服務的,其中屬性如下:
@property(assign,readonly,nonatomic)?CBPeripheral?*peripheral; //對應的外設
@property(readonly,nonatomic)BOOLisPrimary; //是否是初等服務
@property(retain,readonly,nullable)NSArray?*includedServices; //包含的自服務
@property(retain,readonly,nullable)NSArray?*characteristics; //服務中的特征值
六、服務的特征值CBCharacteristic
????????通過綁定服務中的特征值來進行數(shù)據(jù)的讀寫操作,其中屬性如下:
//對應的服務對象@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?*descriptors; //是否是當前廣播的特征@property(readonly)BOOLisBroadcasted; //是否是正在通知的特征@property(readonly)BOOLisNotifying;
七、讀寫請求對象CBATTRequest
????????服務對象是外設向中心設備提供的相關數(shù)據(jù)服務,獲取到相應服務后,中心設備可以進行讀寫請求,讀寫對象屬性如下:
@property(readonly,nonatomic)?CBCentral?*central; //對應的中心設備
@property(readonly,nonatomic)?CBCharacteristic?*characteristic; //對應的特征值
@property(readwrite,copy,nullable) NSData*value; //讀寫數(shù)據(jù)值