藍(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,獨(dú)一的標(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è)備信號(hào)

/*!
 *  @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ā)送一個(gè)沒有響應(yīng)的寫服務(wù).  
 *  2、如果值為 NO,如果值被設(shè)置為YES時(shí),當(dāng)前寫服務(wù)被沖刷,方法peripheralIsReadyToSendWriteWithoutResponse:將會(huì)被調(diào)用
 */
@property(readonly) BOOL canSendWriteWithoutResponse;

三、方法

1、讀信號(hào)強(qiáng)度

/*!
 *  @method readRSSI
 *
 *  @discussion 當(dāng)連接成功,檢索當(dāng)前連接的信號(hào)強(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ù),這樣的話會(huì)比較慢,不推薦
 *  @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 獲取向一個(gè)寫服務(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é)果將會(huì)回調(diào) peripheral:didWriteValueForCharacteristic:error:方法
 * 2、如果指定為CBCharacteristicWriteWithoutResponse類型,同時(shí)canSendWriteWithoutResponse為NO時(shí),則數(shù)據(jù)將盡最大努力,但不會(huì)被保證成功。
 *                      
 */
- (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ù)改變時(shí)候

 *  服務(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    返回錯(cuò)誤原因.
 *
 *  @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        錯(cuò)誤原因
 *
 *  @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        錯(cuò)誤原因.
 *
 *  @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ù)特征值時(shí)調(diào)用
 *
 */
- (void)peripheralIsReadyToSendWriteWithoutResponse:(CBPeripheral *)peripheral;
/*!
 *  @method peripheral:didOpenL2CAPChannel:error:
 *
 *  @param peripheral       當(dāng)前設(shè)備
 *  @param channel          CBL2CAPChanne
 *  @param error            錯(cuò)誤信息
 *
 *  @discussion             該方法為openL2CAPChannel: 回調(diào)
 */
- (void)peripheral:(CBPeripheral *)peripheral didOpenL2CAPChannel:(nullable CBL2CAPChannel *)channel error:(nullable NSError *)error;
最后編輯于
?著作權(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)容