一昂勉、宏定義
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;