【轉(zhuǎn)】iOS開發(fā)之藍(lán)牙通信

iOS開發(fā)之藍(lán)牙通訊

一、引言

????????藍(lán)牙是設(shè)備近距離通信的一種方便手段,在iPhone引入藍(lán)牙4.0后,設(shè)備之間的通訊變得更加簡(jiǎn)單。相關(guān)的藍(lán)牙操作由專門的CoreBluetooth.framework進(jìn)行統(tǒng)一管理。通過藍(lán)牙進(jìn)行通訊交互分為兩方,一方為中心設(shè)備central,一方為外設(shè)peripheral,外設(shè)通過廣播的方式向外發(fā)送信息,中心設(shè)備檢索到外設(shè)發(fā)的廣播信息,可以進(jìn)行配對(duì)連接,進(jìn)而進(jìn)行數(shù)據(jù)交互。

二、中心設(shè)備CBCentralManager

????????CBCentralManager是管理中心設(shè)備的管理類,其中重要方法如下:

//設(shè)置中心設(shè)備代理@property(assign,nonatomic,nullable)id?delegate;//中心設(shè)備當(dāng)前狀態(tài)@property(readonly)?CBCentralManagerState?state;//中心設(shè)備是否正在掃描@property(readonly)BOOLisScanningNS_AVAILABLE(NA,9_0);

? ?其中state是一個(gè)枚舉,有關(guān)藍(lán)牙是否可用的狀態(tài)如下:

typedefNS_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,};

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

//初始化方法//設(shè)置的代理需要遵守CBCentralManagerDelegate協(xié)議//queue可以設(shè)置藍(lán)牙掃描的線程?傳入nil則為在主線程中進(jìn)行-?(instancetype)initWithDelegate:(nullableid)delegate???queue:(nullabledispatch_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:(nullableid)delegate???queue:(nullabledispatch_queue_t)queue?options:(nullableNSDictionary?*)options;


//根據(jù)獲取所有已知設(shè)備-?(NSArray?*)retrievePeripheralsWithIdentifiers:(NSArray?*)identifiers;//根據(jù)服務(wù)id獲取所有連接的設(shè)備-?(NSArray?*)retrieveConnectedPeripheralsWithServices:(NSArray?*)serviceUUIDs;

在初始化管理中心完成后,會(huì)回調(diào)代理中的如下方法,我們必須實(shí)現(xiàn)如下方法:

//這個(gè)方法中可以獲取到管理中心的狀態(tài)-?(void)centralManagerDidUpdateState:(CBCentralManager?*)central;

如果上面方法中管理中心狀態(tài)為藍(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:(nullableNSArray?*)serviceUUIDs?options:(nullableNSDictionary?*)options;

//停止掃描外設(shè)-?(void)stopScan;

掃描的結(jié)果會(huì)在如下代理方法中回掉:

//peripheral?掃描到的外設(shè)//advertisementData是外設(shè)發(fā)送的廣播數(shù)據(jù)//RSSI?是信號(hào)強(qiáng)度-?(void)centralManager:(CBCentralManager?*)central?didDiscoverPeripheral:(CBPeripheral?*)peripheral?advertisementData:(NSDictionary?*)advertisementData?RSSI:(NSNumber*)RSSI;


掃描到外設(shè)后,通過下面方法可以連接一個(gè)外設(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:(nullableNSDictionary?*)options;//取消一個(gè)外設(shè)的連接-?(void)cancelPeripheralConnection:(CBPeripheral?*)peripheral;

調(diào)用過連接外設(shè)的方法后,會(huì)回掉如下代理方法:

//連接外設(shè)成功-?(void)centralManager:(CBCentralManager?*)centraldidConnectPeripheral:(CBPeripheral?*)peripheral;//連接外設(shè)失敗-?(void)centralManager:(CBCentralManager?*)centraldidFailToConnectPeripheral:(CBPeripheral?*)peripheralerror:(nullable?NSError?*)error;//斷開外設(shè)連接-?(void)centralManager:(CBCentralManager?*)centraldidDisconnectPeripheral:(CBPeripheral?*)peripheralerror:(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?*)dict;

三、外設(shè)CBPeripheralManager

????????從上面我們知道,中心設(shè)備是用來掃描周圍的外設(shè),兩臺(tái)設(shè)備的通訊中,必須有一個(gè)充當(dāng)中心設(shè)備,一個(gè)充當(dāng)外設(shè),外設(shè)是由CBPeripheralManager進(jìn)行管理,主要方法如下:

//設(shè)置外設(shè)管理中心代理@property(assign,nonatomic,nullable)id?delegate;//外設(shè)狀態(tài)?枚舉如中心設(shè)備@property(readonly)?CBPeripheralManagerState?state;//是否正在發(fā)送廣播@property(readonly)BOOLisAdvertising;//用戶的授權(quán)狀態(tài)+?(CBPeripheralManagerAuthorizationStatus)authorizationStatus;//初始化并設(shè)置代理?參數(shù)的具體含義與中心設(shè)備管理中心-?(instancetype)initWithDelegate:(nullableid)delegate???queue:(nullabledispatch_queue_t);-?(instancetype)initWithDelegate:(nullableid)delegate???queue:(nullabledispatch_queue_t)queue?options:(nullableNSDictionary?*)options;//開始發(fā)送廣播//advertisementData中可以發(fā)送的數(shù)據(jù)有約定?如下/*

對(duì)應(yīng)設(shè)置NSString類型的廣播名

NSString?*const?CBAdvertisementDataLocalNameKey;

外設(shè)制造商的NSData數(shù)據(jù)

NSString?*const?CBAdvertisementDataManufacturerDataKey;

外設(shè)制造商的CBUUID數(shù)據(jù)

NSString?*const?CBAdvertisementDataServiceDataKey;

服務(wù)的UUID與其對(duì)應(yīng)的服務(wù)數(shù)據(jù)字典數(shù)組

NSString?*const?CBAdvertisementDataServiceUUIDsKey;

附加服務(wù)的UUID數(shù)組

NSString?*const?CBAdvertisementDataOverflowServiceUUIDsKey;

外設(shè)的發(fā)送功率?NSNumber類型

NSString?*const?CBAdvertisementDataTxPowerLevelKey;

外設(shè)是否可以連接

NSString?*const?CBAdvertisementDataIsConnectable;

服務(wù)的UUID數(shù)組

NSString?*const?CBAdvertisementDataSolicitedServiceUUIDsKey;

*/-?(void)startAdvertising:(nullableNSDictionary?*)advertisementData;//停止發(fā)送廣播-?(void)stopAdvertising;//設(shè)置一個(gè)連接的具體central設(shè)備的延時(shí)?枚舉如下/*

typedef?NS_ENUM(NSInteger,?CBPeripheralManagerConnectionLatency)?{

CBPeripheralManagerConnectionLatencyLow?=?0,

CBPeripheralManagerConnectionLatencyMedium,

CBPeripheralManagerConnectionLatencyHigh

}?NS_ENUM_AVAILABLE(NA,?6_0);

*/-?(void)setDesiredConnectionLatency:(CBPeripheralManagerConnectionLatency)latency?forCentral:(CBCentral?*)central;//添加一個(gè)服務(wù)-?(void)addService:(CBMutableService?*)service;//移除一個(gè)服務(wù)-?(void)removeService:(CBMutableService?*)service;//移除所有服務(wù)-?(void)removeAllServices;//響應(yīng)中心設(shè)備的讀寫請(qǐng)求-?(void)respondToRequest:(CBATTRequest?*)request?withResult:(CBATTError)result;//更新一個(gè)連接中心設(shè)備的訂閱特征值-?(BOOL)updateValue:(NSData*)value?forCharacteristic:(CBMutableCharacteristic?*)characteristic?onSubscribedCentrals:(nullableNSArray?*)centrals;

外設(shè)代理的相關(guān)方法如下:

//這個(gè)方法是必須實(shí)現(xiàn)的?狀態(tài)可用后可以發(fā)送廣播-?(void)peripheralManagerDidUpdateState:(CBPeripheralManager?*)peripheral;//連接回復(fù)時(shí)調(diào)用的方法?和centralManager類似-?(void)peripheralManager:(CBPeripheralManager?*)peripheralwillRestoreState:(NSDictionary?*)dict;//開始發(fā)送廣播時(shí)調(diào)用的方法-?(void)peripheralManagerDidStartAdvertising:(CBPeripheralManager?*)peripheralerror:(nullable?NSError?*)error;//添加服務(wù)調(diào)用的回調(diào)-?(void)peripheralManager:(CBPeripheralManager?*)peripheraldidAddService:(CBService?*)serviceerror:(nullable?NSError?*)error;//當(dāng)一個(gè)central設(shè)備訂閱一個(gè)特征值時(shí)調(diào)用的方法-?(void)peripheralManager:(CBPeripheralManager?*)peripheralcentral:(CBCentral?*)centraldidSubscribeToCharacteristic:(CBCharacteristic?*)characteristic;//取消訂閱一個(gè)特征值時(shí)調(diào)用的方法-?(void)peripheralManager:(CBPeripheralManager?*)peripheralcentral:(CBCentral?*)centraldidUnsubscribeFromCharacteristic:(CBCharacteristic?*)characteristic;//收到讀請(qǐng)求時(shí)觸發(fā)的方法-?(void)peripheralManager:(CBPeripheralManager?*)peripheraldidReceiveReadRequest:(CBATTRequest?*)request;//收到寫請(qǐng)求時(shí)觸發(fā)的方法-?(void)peripheralManager:(CBPeripheralManager?*)peripheraldidReceiveWriteRequests:(NSArray?*)requests;//外設(shè)準(zhǔn)備更新特征值時(shí)調(diào)用的方法-?(void)peripheralManagerIsReadyToUpdateSubscribers:(CBPeripheralManager?*)peripheral;

四、中心設(shè)備與外設(shè)對(duì)象CBCentral與CBPeripheral

????????上面介紹了中心設(shè)備管理類與外設(shè)管理類,這些類用于將設(shè)備連接建立起來,器具的數(shù)據(jù)交換的服務(wù)和一些信息則是在對(duì)應(yīng)的設(shè)備對(duì)象中。

1、中心設(shè)備 CBCentral屬性與方法

//設(shè)備UUID@property(readonly,nonatomic)NSUUID*identifier;//中心設(shè)備最大接收的數(shù)據(jù)長(zhǎng)度@property(readonly,nonatomic)NSUIntegermaximumUpdateValueLength;

2、外設(shè)CAPeripheral屬性與方法

????????外設(shè)對(duì)象要比中心對(duì)象復(fù)雜的多,當(dāng)centralManager連接到外設(shè)后,需要通過外設(shè)對(duì)象的代理方法進(jìn)行數(shù)據(jù)交互,其中主要方法屬性如下:

//設(shè)置代理@property(assign,nonatomic,nullable)id?delegate;//外設(shè)name@property(retain,readonly,nullable)NSString*name;//信號(hào)強(qiáng)度@property(retain,readonly,nullable)NSNumber*RSSINS_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?*services;//獲取當(dāng)前信號(hào)強(qiáng)度-?(void)readRSSI;//根據(jù)服務(wù)UUID尋找服務(wù)對(duì)象-?(void)discoverServices:(nullableNSArray?*)serviceUUIDs;//在服務(wù)對(duì)象UUID數(shù)組中尋找特定服務(wù)-?(void)discoverIncludedServices:(nullableNSArray?*)includedServiceUUIDs?forService:(CBService?*)service;//在一個(gè)服務(wù)中尋找特征值-?(void)discoverCharacteristics:(nullableNSArray?*)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)typeNS_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è)的代理方法如下:

//外設(shè)名稱更改時(shí)回調(diào)的方法-?(void)peripheralDidUpdateName:(CBPeripheral?*)peripheral?NS_AVAILABLE(NA,6_0);//外設(shè)服務(wù)變化時(shí)回調(diào)的方法-?(void)peripheral:(CBPeripheral?*)peripheraldidModifyServices:(NSArray?*)invalidatedServices?NS_AVAILABLE(NA,7_0);//信號(hào)強(qiáng)度改變時(shí)調(diào)用的方法-?(void)peripheralDidUpdateRSSI:(CBPeripheral?*)peripheralerror:(nullable?NSError?*)error?NS_DEPRECATED(NA,?NA,5_0,8_0);//讀取信號(hào)強(qiáng)度回調(diào)的方法-?(void)peripheral:(CBPeripheral?*)peripheraldidReadRSSI:(NSNumber?*)RSSIerror:(nullable?NSError?*)error?NS_AVAILABLE(NA,8_0);//發(fā)現(xiàn)服務(wù)時(shí)調(diào)用的方法-?(void)peripheral:(CBPeripheral?*)peripheraldidDiscoverServices:(nullable?NSError?*)error;//在服務(wù)中發(fā)現(xiàn)子服務(wù)回調(diào)的方法-?(void)peripheral:(CBPeripheral?*)peripheraldidDiscoverIncludedServicesForService:(CBService?*)serviceerror:(nullable?NSError?*)error;//發(fā)現(xiàn)服務(wù)的特征值后回調(diào)的方法-?(void)peripheral:(CBPeripheral?*)peripheraldidDiscoverCharacteristicsForService:(CBService?*)serviceerror:(nullable?NSError?*)error;//特征值更新時(shí)回調(diào)的方法-?(void)peripheral:(CBPeripheral?*)peripheraldidUpdateValueForCharacteristic:(CBCharacteristic?*)characteristicerror:(nullable?NSError?*)error;//向特征值寫數(shù)據(jù)時(shí)回調(diào)的方法-?(void)peripheral:(CBPeripheral?*)peripheraldidWriteValueForCharacteristic:(CBCharacteristic?*)characteristicerror:(nullable?NSError?*)error;//特征值的通知設(shè)置改變時(shí)觸發(fā)的方法-?(void)peripheral:(CBPeripheral?*)peripheraldidUpdateNotificationStateForCharacteristic:(CBCharacteristic?*)characteristicerror:(nullable?NSError?*)error;//發(fā)現(xiàn)特征值的描述信息觸發(fā)的方法-?(void)peripheral:(CBPeripheral?*)peripheraldidDiscoverDescriptorsForCharacteristic:(CBCharacteristic?*)characteristicerror:(nullable?NSError?*)error;//特征的描述值更新時(shí)觸發(fā)的方法-?(void)peripheral:(CBPeripheral?*)peripheraldidUpdateValueForDescriptor:(CBDescriptor?*)descriptorerror:(nullable?NSError?*)error;//寫描述信息時(shí)觸發(fā)的方法-?(void)peripheral:(CBPeripheral?*)peripheraldidWriteValueForDescriptor:(CBDescriptor?*)descriptorerror:(nullable?NSError?*)error;

五、服務(wù)對(duì)象CBService

????服務(wù)對(duì)象是用來管理外設(shè)提供的一些數(shù)據(jù)服務(wù)的,其中屬性如下:

//對(duì)應(yīng)的外設(shè)@property(assign,readonly,nonatomic)?CBPeripheral?*peripheral;//是否是初等服務(wù)@property(readonly,nonatomic)BOOLisPrimary;//包含的自服務(wù)@property(retain,readonly,nullable)NSArray?*includedServices;//服務(wù)中的特征值@property(retain,readonly,nullable)NSArray?*characteristics;

六、服務(wù)的特征值CBCharacteristic

????????通過綁定服務(wù)中的特征值來進(jìn)行數(shù)據(jù)的讀寫操作,其中屬性如下:

//對(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?*descriptors;//是否是當(dāng)前廣播的特征@property(readonly)BOOLisBroadcasted;//是否是正在通知的特征@property(readonly)BOOLisNotifying;

七、讀寫請(qǐng)求對(duì)象CBATTRequest

????????服務(wù)對(duì)象是外設(shè)向中心設(shè)備提供的相關(guān)數(shù)據(jù)服務(wù),獲取到相應(yīng)服務(wù)后,中心設(shè)備可以進(jìn)行讀寫請(qǐng)求,讀寫對(duì)象屬性如下:

//對(duì)應(yīng)的中心設(shè)備@property(readonly,nonatomic)?CBCentral?*central;//對(duì)應(yīng)的特征值@property(readonly,nonatomic)?CBCharacteristic?*characteristic;//讀寫數(shù)據(jù)值@property(readwrite,copy,nullable)NSData*value;

跳轉(zhuǎn)原文

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容