蓝牙API

如果你进来了,点下关注行不行_

CBCentralManager 中心设备管理类#

//设置中心设备代理
@property(assign, nonatomic, nullable) id<CBCentralManagerDelegate> delegate;
//中心设备当前状态
@property(readonly) CBCentralManagerState state;
//中心设备是否正在扫描
@property(readonly) BOOL isScanning NS_AVAILABLE(NA, 9_0);

CBCentralManagerState 设备状态#

typedef NS_ENUM(NSInteger, CBCentralManagerState) {
    //状态未知
    CBCentralManagerStateUnknown = 0,
    //连接断开 即将重置
    CBCentralManagerStateResetting,
    //该平台不支持蓝牙
    CBCentralManagerStateUnsupported,
    //未授权蓝牙使用
    CBCentralManagerStateUnauthorized,
    //蓝牙关闭
    CBCentralManagerStatePoweredOff,
    //蓝牙正常开启
    CBCentralManagerStatePoweredOn,
};

初始化CBCentralManager方法#

//初始化方法
//设置的代理需要遵守CBCentralManagerDelegate协议
//queue可以设置蓝牙扫描的线程 传入nil则为在主线程中进行
- (instancetype)initWithDelegate:(nullable id<CBCentralManagerDelegate>)delegate
queue:(nullable dispatch_queue_t)queue;
//此方法同上 在options字典中用于进行一些管理中心的初始化属性设置
//字典中支持的键值如下
/*
 NSString * const CBCentralManagerOptionShowPowerAlertKey 对应一个NSNumber类型的bool值,用于设置是否在关闭蓝牙时弹出用户提示
 NSString * const CBCentralManagerOptionRestoreIdentifierKey 对应一个NSString对象,设置管理中心的标识符ID
 */
- (instancetype)initWithDelegate:(nullable id<CBCentralManagerDelegate>)delegate
queue:(nullable dispatch_queue_t)queue
options:(nullable NSDictionary<NSString *, id> *)options;

根据获取连接设备列表#

//�根据服务NSUUID,获取所有设备,并返回一个列表
- (NSArray<CBPeripheral *> *)retrievePeripheralsWithIdentifiers:(NSArray<NSUUID *> *)identifiers;
//根据服务id获取所有连接的设备
- (NSArray<CBPeripheral *> *)retrieveConnectedPeripheralsWithServices:(NSArray<CBUUID *> *)serviceUUIDs;

初始化CBCentralManager后,会自动回调代理方法#

//这个方法中可以获取到管理中心的状态
- (void)centralManagerDidUpdateState:(CBCentralManager *)central;

扫描蓝牙设备方法#

// serviceUUIDs     :  用于扫描一个特点ID的外设
// options          :  用于设置一些扫描属性 键值如下
/*
 //是否允许重复扫描 对应NSNumber的bool值,默认为NO,会自动去重
 NSString *const CBCentralManagerScanOptionAllowDuplicatesKey;
 //要扫描的设备UUID 数组 对应NSArray
 NSString *const CBCentralManagerScanOptionSolicitedServiceUUIDsKey;
 */
- (void)scanForPeripheralsWithServices:(nullable NSArray<CBUUID *> *)serviceUUIDs options:(nullable NSDictionary<NSString *, id> *)options;

停止蓝牙扫描的方法#

- (void)stopScan;

扫描成功的方法#

// peripheral 扫描到的外设
// advertisementData是外设发送的广播数据
// RSSI 是信号强度
- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary<NSString *, id> *)advertisementData RSSI:(NSNumber *)RSSI;

连接设备的方法#

/*
 options中可以设置一些连接设备的初始属性键值如下
 //对应NSNumber的bool值,设置当外设连接后是否弹出一个警告
 NSString *const CBConnectPeripheralOptionNotifyOnConnectionKey;
 //对应NSNumber的bool值,设置当外设断开连接后是否弹出一个警告
 NSString *const CBConnectPeripheralOptionNotifyOnDisconnectionKey;
 //对应NSNumber的bool值,设置当外设暂停连接后是否弹出一个警告
 NSString *const CBConnectPeripheralOptionNotifyOnNotificationKey;
 */
- (void)connectPeripheral:(CBPeripheral *)peripheral options:(nullable NSDictionary<NSString *, id> *)options;

取消连接设备的方法#

//取消一个外设的连接
- (void)cancelPeripheralConnection:(CBPeripheral *)peripheral;

连接后处理的方法#

//连接外设成功
- (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral;
//连接外设失败
- (void)centralManager:(CBCentralManager *)central didFailToConnectPeripheral:(CBPeripheral *)peripheral error:(nullable NSError *)error;
//断开外设连接
- (void)centralManager:(CBCentralManager *)central didDisconnectPeripheral:(CBPeripheral *)peripheral error:(nullable NSError *)error;

当管理中心恢复时会调用如下方法#

//dict中会传入如下键值对
/*
 //恢复连接的外设数组
 NSString *const CBCentralManagerRestoredStatePeripheralsKey;
 //恢复连接的服务UUID数组
 NSString *const CBCentralManagerRestoredStateScanServicesKey;
 //恢复连接的外设扫描属性字典数组
 NSString *const CBCentralManagerRestoredStateScanOptionsKey;
 */
- (void)centralManager:(CBCentralManager *)central willRestoreState:(NSDictionary<NSString *, id> *)dict;

外设的设备的方法,(找服务,找特征)#

//设置代理
@property(assign, nonatomic, nullable) id<CBPeripheralDelegate> delegate;
//外设name
@property(retain, readonly, nullable) NSString *name;
//信号强度
@property(retain, readonly, nullable) NSNumber *RSSI NS_DEPRECATED(NA, NA, 5_0, 8_0);
//外设状态
/*
 typedef NS_ENUM(NSInteger, CBPeripheralState) {
 CBPeripheralStateDisconnected = 0,//未连接
 CBPeripheralStateConnecting,//正在链接
 CBPeripheralStateConnected,//已经连接
 CBPeripheralStateDisconnecting NS_AVAILABLE(NA, 9_0),//正在断开连接
 } NS_AVAILABLE(NA, 7_0);
 */
@property(readonly) CBPeripheralState state;
//所有的服务数组
@property(retain, readonly, nullable) NSArray<CBService *> *services;
//获取当前信号强度
- (void)readRSSI;
//根据服务UUID寻找服务对象
- (void)discoverServices:(nullable NSArray<CBUUID *> *)serviceUUIDs;
//在服务对象UUID数组中寻找特定服务
- (void)discoverIncludedServices:(nullable NSArray<CBUUID *> *)includedServiceUUIDs forService:(CBService *)service;
//在一个服务中寻找特征值
- (void)discoverCharacteristics:(nullable NSArray<CBUUID *> *)characteristicUUIDs forService:(CBService *)service;
//从一个特征中读取数据
- (void)readValueForCharacteristic:(CBCharacteristic *)characteristic;
//写数据的最大长度
//type枚举如下
/*
 typedef NS_ENUM(NSInteger, CBCharacteristicWriteType) {
 CBCharacteristicWriteWithResponse = 0,//写数据并且接收成功与否回执
 CBCharacteristicWriteWithoutResponse,//写数据不接收回执
 };
 */
- (NSUInteger)maximumWriteValueLengthForType:(CBCharacteristicWriteType)type NS_AVAILABLE(NA, 9_0);
//向某个特征中写数据
- (void)writeValue:(NSData *)data forCharacteristic:(CBCharacteristic *)characteristic type:(CBCharacteristicWriteType)type;
//为制定的特征值设置监听通知
- (void)setNotifyValue:(BOOL)enabled forCharacteristic:(CBCharacteristic *)characteristic;
//寻找特征值的描述
- (void)discoverDescriptorsForCharacteristic:(CBCharacteristic *)characteristic;
//读取特征的描述值
- (void)readValueForDescriptor:(CBDescriptor *)descriptor;
//写特征的描述值
- (void)writeValue:(NSData *)data forDescriptor:(CBDescriptor *)descriptor;

外设方法的回调,代理方法#

//外设名称更改时回调的方法
- (void)peripheralDidUpdateName:(CBPeripheral *)peripheral NS_AVAILABLE(NA, 6_0);
//外设服务变化时回调的方法
- (void)peripheral:(CBPeripheral *)peripheral didModifyServices:(NSArray<CBService *> *)invalidatedServices NS_AVAILABLE(NA, 7_0);
//信号强度改变时调用的方法
- (void)peripheralDidUpdateRSSI:(CBPeripheral *)peripheral error:(nullable NSError *)error NS_DEPRECATED(NA, NA, 5_0, 8_0);
//读取信号强度回调的方法
- (void)peripheral:(CBPeripheral *)peripheral didReadRSSI:(NSNumber *)RSSI error:(nullable NSError *)error NS_AVAILABLE(NA, 8_0);
//发现服务时调用的方法
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(nullable NSError *)error;
//在服务中发现子服务回调的方法
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverIncludedServicesForService:(CBService *)service error:(nullable NSError *)error;
//发现服务的特征值后回调的方法
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(nullable NSError *)error;
//特征值更新时回调的方法
- (void)peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic error:(nullable NSError *)error;
//向特征值写数据时回调的方法
- (void)peripheral:(CBPeripheral *)peripheral didWriteValueForCharacteristic:(CBCharacteristic *)characteristic error:(nullable NSError *)error;
//特征值的通知设置改变时触发的方法
- (void)peripheral:(CBPeripheral *)peripheral didUpdateNotificationStateForCharacteristic:(CBCharacteristic *)characteristic error:(nullable NSError *)error;
//发现特征值的描述信息触发的方法
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverDescriptorsForCharacteristic:(CBCharacteristic *)characteristic error:(nullable NSError *)error;
//特征的描述值更新时触发的方法
- (void)peripheral:(CBPeripheral *)peripheral didUpdateValueForDescriptor:(CBDescriptor *)descriptor error:(nullable NSError *)error;
//写描述信息时触发的方法
- (void)peripheral:(CBPeripheral *)peripheral didWriteValueForDescriptor:(CBDescriptor *)descriptor error:(nullable NSError *)error;

CBService服务的方法#

//对应的外设
@property(assign, readonly, nonatomic) CBPeripheral *peripheral;
//是否是初等服务
@property(readonly, nonatomic) BOOL isPrimary;
//包含的自服务
@property(retain, readonly, nullable) NSArray<CBService *> *includedServices;
//服务中的特征值
@property(retain, readonly, nullable) NSArray<CBCharacteristic *> *characteristics;

Characteristic特征的方法#

//对应的服务对象
@property(assign, readonly, nonatomic) CBService *service;
//特征值的属性 枚举如下
/*
 typedef NS_OPTIONS(NSUInteger, CBCharacteristicProperties) {
 CBCharacteristicPropertyBroadcast,//允许广播特征
 CBCharacteristicPropertyRead,//可读属性
 CBCharacteristicPropertyWriteWithoutResponse,//可写并且接收回执
 CBCharacteristicPropertyWrite,//可写属性
 CBCharacteristicPropertyNotify,//可通知属性
 CBCharacteristicPropertyIndicate,//可展现的特征值
 CBCharacteristicPropertyAuthenticatedSignedWrites,//允许签名的特征值写入
 CBCharacteristicPropertyExtendedProperties,
 CBCharacteristicPropertyNotifyEncryptionRequired,
 CBCharacteristicPropertyIndicateEncryptionRequired
 };
 */
@property(readonly, nonatomic) CBCharacteristicProperties properties;
//特征值的数据
@property(retain, readonly, nullable) NSData *value;
//特征值的描述
@property(retain, readonly, nullable) NSArray<CBDescriptor *> *descriptors;
//是否是当前广播的特征
@property(readonly) BOOL isBroadcasted;
//是否是正在通知的特征
@property(readonly) BOOL isNotifying;
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 157,198评论 4 359
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 66,663评论 1 290
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 106,985评论 0 237
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,673评论 0 202
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 51,994评论 3 285
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,399评论 1 211
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,717评论 2 310
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,407评论 0 194
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,112评论 1 239
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,371评论 2 241
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 31,891评论 1 256
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,255评论 2 250
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 32,881评论 3 233
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,010评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,764评论 0 192
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,412评论 2 269
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,299评论 2 260

推荐阅读更多精彩内容