藍(lán)牙開發(fā)系列四:CBPeripheral詳解

一昂勉、宏定義

1、外設(shè)狀態(tài)

typedef NS_ENUM(NSInteger, CBPeripheralState) {
    CBPeripheralStateDisconnected = 0,//斷開
    CBPeripheralStateConnecting,//正在連接
    CBPeripheralStateConnected,//已連接
    CBPeripheralStateDisconnecting NS_AVAILABLE(10_13, 9_0),//正在斷開連接
} NS_AVAILABLE(10_9, 7_0);

2村象、設(shè)備寫服務(wù)類型

typedef NS_ENUM(NSInteger, CBCharacteristicWriteType) {
    CBCharacteristicWriteWithResponse = 0,//有響應(yīng)
    CBCharacteristicWriteWithoutResponse,//無響應(yīng)
};

二攒至、屬性

1迫吐、設(shè)備的UUID

//CBPeripheral繼承自CBPeer,獨一的標(biāo)示該設(shè)備的id
@property(readonly, nonatomic) NSUUID *identifier NS_AVAILABLE(10_13, 7_0);

2熙宇、外設(shè)代理

/*!
 *  @property delegate
 *
 *  @discussion 用來接收設(shè)備事件的代理
 */
@property(weak, nonatomic, nullable) id<CBPeripheralDelegate> delegate;

3溉浙、設(shè)備名稱

/*!
 *  @property name
 *
 *  @discussion 設(shè)備的名稱
 */
@property(retain, readonly, nullable) NSString *name;

4、設(shè)備信號

/*!
 *  @property RSSI
 *
 *  @discussion 最后一次讀取到的設(shè)備強(qiáng)度
 *
 *  @廢棄 被 peripheral:didReadRSSI:error:替代
 */
@property(retain, readonly, nullable) NSNumber *RSSI NS_DEPRECATED(10_7, 10_13, 5_0, 8_0);

5烈拒、設(shè)備狀態(tài)

/*!
 *  @property state
 *
 *  @discussion 外設(shè)的連接狀態(tài)
 */
@property(readonly) CBPeripheralState state;

6荆几、設(shè)備服務(wù)

/*!
 *  @property services
 *
 *  @discussion 數(shù)組赊时,內(nèi)含掃描到的設(shè)備的服務(wù)
 */
@property(retain, readonly, nullable) NSArray<CBService *> *services;

7、是否支持無響應(yīng)寫服務(wù)

/*!
 *  @property canSendWriteWithoutResponse
 *
 *  @discussion
 *  1诞吱、如果值為YES竭缝,遠(yuǎn)程設(shè)備有空間發(fā)送一個沒有響應(yīng)的寫服務(wù).  
 *  2抬纸、如果值為 NO,如果值被設(shè)置為YES時,當(dāng)前寫服務(wù)被沖刷,方法peripheralIsReadyToSendWriteWithoutResponse:將會被調(diào)用
 */
@property(readonly) BOOL canSendWriteWithoutResponse;

三膜蛔、方法

1皂股、讀信號強(qiáng)度

/*!
 *  @method readRSSI
 *
 *  @discussion 當(dāng)連接成功命黔,檢索當(dāng)前連接的信號強(qiáng)度÷咽罚回調(diào)方法為 peripheral:didReadRSSI:error:
 *
 *  @see        peripheral:didReadRSSI:error:
 */
- (void)readRSSI;

2搜立、掃描服務(wù)特征值

/*!
 *  @method discoverServices:
 *
 *  @param serviceUUIDs 需要掃描的設(shè)備的服務(wù)的id啄踊,如果為nil,則掃描所有的服務(wù)
 *
 *  @discussion         掃描發(fā)現(xiàn)設(shè)備所有可用的服務(wù)
 *
 *  @see            :掃描回調(diào)方法peripheral:didDiscoverServices:
 */
- (void)discoverServices:(nullable NSArray<CBUUID *> *)serviceUUIDs;

3址晕、發(fā)現(xiàn)內(nèi)聯(lián)服務(wù)

/*!
 *  @method discoverIncludedServices:forService:
 *
 *  @param includedServiceUUIDs  需要發(fā)現(xiàn)的服務(wù)service中的服務(wù)id列表顿锰,如果為nil硼控,則掃描服務(wù)內(nèi)所有的服務(wù),這樣的話會比較慢牢撼,不推薦
 *  @param service              服務(wù)
 *
 *  @discussion                 發(fā)現(xiàn)指定服務(wù)service內(nèi)的服務(wù)
 *
 *  @see        回調(diào)方法:               peripheral:didDiscoverIncludedServicesForService:error:
 */
- (void)discoverIncludedServices:(nullable NSArray<CBUUID *> *)includedServiceUUIDs forService:(CBService *)service;

4熏版、發(fā)現(xiàn)服務(wù)特征值

/*!
 *  @method discoverCharacteristics:forService:
 *
 *  @param characteristicUUIDs  數(shù)組,內(nèi)含需要被發(fā)現(xiàn)的所有特征值類型再膳,如果為nil曲横,則為所有特征值。
 *  @param service              服務(wù)
 *
 *  @discussion                 發(fā)現(xiàn)指令服務(wù)的服務(wù)特征值
 *
 *  @see            回調(diào)方法為:          peripheral:didDiscoverCharacteristicsForService:error:
 */
- (void)discoverCharacteristics:(nullable NSArray<CBUUID *> *)characteristicUUIDs forService:(CBService *)service;

5胳喷、讀服務(wù)特征值

/*!
 *  @method readValueForCharacteristic:
 *
 *  @param characteristic   需要讀取的服務(wù)特征值
 *
 *  @discussion             讀取服務(wù)特征值的值吭露,調(diào)用該方法則讀取該方法前最新的藍(lán)牙系統(tǒng)緩存的從外設(shè)讀取的數(shù)據(jù)
 *
 *  @see        回調(diào)方法:           peripheral:didUpdateValueForCharacteristic:error:
 */
- (void)readValueForCharacteristic:(CBCharacteristic *)characteristic;

6尊惰、獲取寫服務(wù)支持最大字節(jié)數(shù)

/*!
*  @method     maximumWriteValueLengthForType:
*
*  @discussion 獲取向一個寫服務(wù)可發(fā)送的最大字節(jié)數(shù)
*
*  @see        該寫服務(wù)可通過調(diào)用writeValue:forCharacteristic:type:寫數(shù)據(jù)
*/
- (NSUInteger)maximumWriteValueLengthForType:(CBCharacteristicWriteType)type NS_AVAILABLE(10_12, 9_0);

7弄屡、向設(shè)備寫數(shù)據(jù)

/*!
 *  @method writeValue:forCharacteristic:type:
 *
 *  @param data             待寫數(shù)據(jù)
 *  @param characteristic   寫服務(wù)特征
 *  @param type             寫服務(wù)的類型(有/無響應(yīng))
 *
 *  @discussion             向指定服務(wù)寫數(shù)據(jù),
 *   1迈嘹、如果指定CBCharacteristicWriteWithResponse類型全庸,寫入結(jié)果將會回調(diào) peripheral:didWriteValueForCharacteristic:error:方法
 * 2壶笼、如果指定為CBCharacteristicWriteWithoutResponse類型,同時canSendWriteWithoutResponse為NO時保礼,則數(shù)據(jù)將盡最大努力责语,但不會被保證成功。
 *                      
 */
- (void)writeValue:(NSData *)data forCharacteristic:(CBCharacteristic *)characteristic type:(CBCharacteristicWriteType)type;

代理方法

/*!
*  @method peripheralDidUpdateName:
*
*  @param peripheral      需要更新名稱的設(shè)備
*
*  @discussion         該方法被觸發(fā)當(dāng)設(shè)備的名稱改變
*/
- (void)peripheralDidUpdateName:(CBPeripheral *)peripheral NS_AVAILABLE(10_9, 6_0);
/*!
 *  @method peripheral:didModifyServices:
 *
 *  @param peripheral     需要更新的設(shè)備
 *  @param invalidatedServices  The services that have been invalidated
 *
 *  @discussion         該方法觸發(fā)當(dāng)設(shè)備的服務(wù)改變時候

 *  服務(wù)可以被重新發(fā)現(xiàn)通過discoverServices: 方法
 */
- (void)peripheral:(CBPeripheral *)peripheral didModifyServices:(NSArray<CBService *> *)invalidatedServices NS_AVAILABLE(10_9, 7_0);
/*!
 *  @method peripheralDidUpdateRSSI:error:
 *
 *  @param peripheral   需要更新的設(shè)備.
 *  @param error    返回錯誤原因.
 *
 *  @discussion     該方法是readRSSI: 的回調(diào)
 *
 *  @deprecated         使 {@link peripheral:didReadRSSI:error:}代替了
 */
- (void)peripheralDidUpdateRSSI:(CBPeripheral *)peripheral error:(nullable NSError *)error NS_DEPRECATED(10_7, 10_13, 5_0, 8_0);
/*!
 *  @method peripheral:didReadRSSI:error:
 *
 *  @param peripheral   需要更新的設(shè)備
 *  @param RSSI         設(shè)備的RSSI.
 *
 *  @discussion         該方法是readRSSI: 的回調(diào)
 */
- (void)peripheral:(CBPeripheral *)peripheral didReadRSSI:(NSNumber *)RSSI error:(nullable NSError *)error NS_AVAILABLE(10_13, 8_0);
/*!
 *  @method peripheral:didDiscoverServices:
 *
 *  @param peripheral   當(dāng)前設(shè)備.
 *  @param error        錯誤原因
 *
 *  @discussion         該發(fā)放為 discoverServices:回調(diào)
 *
 */
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(nullable NSError *)error;
/*!
 *  @method peripheral:didDiscoverIncludedServicesForService:error:
 *
 *  @param peripheral   當(dāng)前設(shè)備
 *  @param service      設(shè)備服務(wù)
 *  @param error        錯誤原因.
 *
 *  @discussion         該方法為 discoverIncludedServices:forService: 的回調(diào)
 */
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverIncludedServicesForService:(CBService *)service error:(nullable NSError *)error;
/*!
*  @method peripheral:didDiscoverCharacteristicsForService:error:
*
*  @param peripheral   The peripheral providing this information.
*  @param service      The <code>CBService</code> object containing the characteristic(s).
*  @param error        If an error occurred, the cause of the failure.
*
*  @discussion         This method returns the result of a @link discoverCharacteristics:forService: @/link call. If the characteristic(s) were read successfully, 
*                      they can be retrieved via <i>service</i>'s <code>characteristics</code> property.
*/
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(nullable NSError *)error;
/*!
 *  @method peripheral:didUpdateValueForCharacteristic:error:
 *
 *  @param peripheral       The peripheral providing this information.
 *  @param characteristic   A <code>CBCharacteristic</code> object.
 *  @param error            If an error occurred, the cause of the failure.
 *
 *  @discussion             This method is invoked after a @link readValueForCharacteristic: @/link call, or upon receipt of a notification/indication.
 */
- (void)peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic error:(nullable NSError *)error;
/*!
 *  @method peripheral:didWriteValueForCharacteristic:error:
 *
 *  @param peripheral       The peripheral providing this information.
 *  @param characteristic   A <code>CBCharacteristic</code> object.
 *  @param error            If an error occurred, the cause of the failure.
 *
 *  @discussion             This method returns the result of a {@link writeValue:forCharacteristic:type:} call, when the <code>CBCharacteristicWriteWithResponse</code> type is used.
 */
 - (void)peripheral:(CBPeripheral *)peripheral didWriteValueForCharacteristic:(CBCharacteristic *)characteristic error:(nullable NSError *)error;
/*!
 *  @method peripheral:didUpdateNotificationStateForCharacteristic:error:
 *
 *  @param peripheral       The peripheral providing this information.
 *  @param characteristic   A <code>CBCharacteristic</code> object.
 *  @param error            If an error occurred, the cause of the failure.
 *
 *  @discussion             This method returns the result of a @link setNotifyValue:forCharacteristic: @/link call. 
 */
- (void)peripheral:(CBPeripheral *)peripheral didUpdateNotificationStateForCharacteristic:(CBCharacteristic *)characteristic error:(nullable NSError *)error;
/*!
 *  @method peripheral:didDiscoverDescriptorsForCharacteristic:error:
 *
 *  @param peripheral       The peripheral providing this information.
 *  @param characteristic   A <code>CBCharacteristic</code> object.
 *  @param error            If an error occurred, the cause of the failure.
 *
 *  @discussion             This method returns the result of a @link discoverDescriptorsForCharacteristic: @/link call. If the descriptors were read successfully, 
 *                          they can be retrieved via <i>characteristic</i>'s <code>descriptors</code> property.
 */
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverDescriptorsForCharacteristic:(CBCharacteristic *)characteristic error:(nullable NSError *)error;
/*!
 *  @method peripheral:didUpdateValueForDescriptor:error:
 *
 *  @param peripheral       The peripheral providing this information.
 *  @param descriptor       A <code>CBDescriptor</code> object.
 *  @param error            If an error occurred, the cause of the failure.
 *
 *  @discussion             This method returns the result of a @link readValueForDescriptor: @/link call.
 */
- (void)peripheral:(CBPeripheral *)peripheral didUpdateValueForDescriptor:(CBDescriptor *)descriptor error:(nullable NSError *)error;
/*!
 *  @method peripheral:didWriteValueForDescriptor:error:
 *
 *  @param peripheral       The peripheral providing this information.
 *  @param descriptor       A <code>CBDescriptor</code> object.
 *  @param error            If an error occurred, the cause of the failure.
 *
 *  @discussion             This method returns the result of a @link writeValue:forDescriptor: @/link call.
 */
- (void)peripheral:(CBPeripheral *)peripheral didWriteValueForDescriptor:(CBDescriptor *)descriptor error:(nullable NSError *)error;
/*!
 *  @method peripheralIsReadyToSendWriteWithoutResponse:
 *
 *  @param peripheral   當(dāng)前設(shè)備
 *
 *  @discussion        該方法被調(diào)用,當(dāng) writeValue:forCharacteristic:type失敗吹害,設(shè)備再次可以發(fā)送服務(wù)特征值時調(diào)用
 *
 */
- (void)peripheralIsReadyToSendWriteWithoutResponse:(CBPeripheral *)peripheral;
/*!
 *  @method peripheral:didOpenL2CAPChannel:error:
 *
 *  @param peripheral       當(dāng)前設(shè)備
 *  @param channel          CBL2CAPChanne
 *  @param error            錯誤信息
 *
 *  @discussion             該方法為openL2CAPChannel: 回調(diào)
 */
- (void)peripheral:(CBPeripheral *)peripheral didOpenL2CAPChannel:(nullable CBL2CAPChannel *)channel error:(nullable NSError *)error;
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末它呀,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子下隧,更是在濱河造成了極大的恐慌谓媒,老刑警劉巖,帶你破解...
    沈念sama閱讀 221,695評論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件土辩,死亡現(xiàn)場離奇詭異拷淘,居然都是意外死亡指孤,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,569評論 3 399
  • 文/潘曉璐 我一進(jìn)店門逝嚎,熙熙樓的掌柜王于貴愁眉苦臉地迎上來详恼,“玉大人,你說我怎么就攤上這事挽铁〕ň颍” “怎么了?”我有些...
    開封第一講書人閱讀 168,130評論 0 360
  • 文/不壞的土叔 我叫張陵,是天一觀的道長赫冬。 經(jīng)常有香客問我,道長膛薛,這世上最難降的妖魔是什么补鼻? 我笑而不...
    開封第一講書人閱讀 59,648評論 1 297
  • 正文 為了忘掉前任,我火速辦了婚禮咨跌,結(jié)果婚禮上锌半,老公的妹妹穿的比我還像新娘。我一直安慰自己拳喻,他們只是感情好冗澈,可當(dāng)我...
    茶點故事閱讀 68,655評論 6 397
  • 文/花漫 我一把揭開白布陋葡。 她就那樣靜靜地躺著,像睡著了一般捌归。 火紅的嫁衣襯著肌膚如雪岭粤。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 52,268評論 1 309
  • 那天巾兆,我揣著相機(jī)與錄音角塑,去河邊找鬼淘讥。 笑死,一個胖子當(dāng)著我的面吹牛蒲列,可吹牛的內(nèi)容都是我干的蝗岖。 我是一名探鬼主播,決...
    沈念sama閱讀 40,835評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼拭宁,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了杰标?” 一聲冷哼從身側(cè)響起腔剂,我...
    開封第一講書人閱讀 39,740評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎袜漩,沒想到半個月后湾碎,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 46,286評論 1 318
  • 正文 獨居荒郊野嶺守林人離奇死亡座掘,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,375評論 3 340
  • 正文 我和宋清朗相戀三年溢陪,在試婚紗的時候發(fā)現(xiàn)自己被綠了睛廊。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 40,505評論 1 352
  • 序言:一個原本活蹦亂跳的男人離奇死亡擂橘,死狀恐怖盒件,靈堂內(nèi)的尸體忽然破棺而出现使,到底是詐尸還是另有隱情,我是刑警寧澤改览,帶...
    沈念sama閱讀 36,185評論 5 350
  • 正文 年R本政府宣布宝当,位于F島的核電站,受9級特大地震影響俐东,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜蚌吸,卻給世界環(huán)境...
    茶點故事閱讀 41,873評論 3 333
  • 文/蒙蒙 一砌庄、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧佩微,春花似錦萌焰、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,357評論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽违施。三九已至,卻和暖如春留潦,著一層夾襖步出監(jiān)牢的瞬間辣往,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,466評論 1 272
  • 我被黑心中介騙來泰國打工坊萝, 沒想到剛下飛機(jī)就差點兒被人妖公主榨干…… 1. 我叫王不留许起,地道東北人。 一個月前我還...
    沈念sama閱讀 48,921評論 3 376
  • 正文 我出身青樓,卻偏偏與公主長得像猛频,于是被迫代替她去往敵國和親蛛勉。 傳聞我的和親對象是個殘疾皇子厉亏,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 45,515評論 2 359

推薦閱讀更多精彩內(nèi)容