一、简介
1.蓝牙开发关键词
- 中心设备:就是用来扫描周围蓝牙硬件的设备,比如通过你手机的蓝牙来扫描并连接智能手环,这时候你的手机就是中心设备。
- 外设:被扫描的设备。比如当你用手机的蓝牙扫描连接智能手环的时候,智能手环就是外设。
- 广播:就是外设不停的散播蓝牙信号,让中心设备可以扫描到。
- 服务(services):外设广播和运行的时候会有服务,可以理解成一个功能模块,中心设备可以读取服务。外设可以有多个服务。
- 特征(characteristic):在服务中的一个单位,一个服务可以有多个特征,特征会有一个value,一般读写的数据就是这个value。
- UUID:区分不同的服务和特征,可以理解为服务和特征的身份证。我们可以用UUID来挑选需要的服务和特征。
2.CoreBluetooth框架
在 CoreBluetooth 中有两个主要的部分:Central 和 Peripheral。CBPeripheralManager
作为外围设备,CBCentralManager
作为中心设备。
因为蓝牙只能支持16进制,而且每次传输只能20个字节,所以要把信息流转成双方可识别的16进制。
二、中心设备 CBCentralManager
CBCentralManager
是管理中心设备的管理类。
//设置中心设备代理
@property(assign, nonatomic, nullable) id<CBCentralManagerDelegate> delegate;
//中心设备当前状态
@property(readonly) CBCentralManagerState state;
//中心设备是否正在扫描
@property(readonly) BOOL isScanning NS_AVAILABLE(NA, 9_0);
其中state是一个枚举,有关蓝牙是否可用的状态如下:
typedef NS_ENUM(NSInteger, CBManagerState) {
//状态未知
CBManagerStateUnknown = 0,
//连接断开 即将重置
CBManagerStateResetting,
//不支持蓝牙
CBManagerStateUnsupported,
//未授权蓝牙使用
CBManagerStateUnauthorized,
//蓝牙关闭
CBManagerStatePoweredOff,
//蓝牙正常开启
CBManagerStatePoweredOn,
} NS_ENUM_AVAILABLE(10_13, 10_0);
初始化方法:
//设置的代理需要遵守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;
扫描外设方法:
//serviceUUIDs用于扫描一个特点ID的外设 options用于设置一些扫描属性 键值如下
/*
//是否允许重复扫描 对应NSNumber的bool值,默认为NO,会自动去重
NSString *const CBCentralManagerScanOptionAllowDuplicatesKey;
//要扫描的设备UUID 数组 对应NSArray hovertree.com
NSString *const CBCentralManagerScanOptionSolicitedServiceUUIDsKey;
*/
- (void)scanForPeripheralsWithServices:(nullable NSArray<CBUUID *> *)serviceUUIDs options:(nullable NSDictionary<NSString *, id> *)options;
//停止扫描外设
- (void)stopScan;
连接方法:
//扫描到外设后,通过下面方法可以连接一个外设:
/*
//options中可以设置一些连接设备的初始属性键值如下
//对应NSNumber的bool值,设置当外设连接后是否弹出一个警告
NSString *const CBConnectPeripheralOptionNotifyOnConnectionKey;
//对应NSNumber的bool值,设置当外设断开连接后是否弹出一个警告
NSString *const CBConnectPeripheralOptionNotifyOnDisconnectionKey;
//对应NSNumber的bool值,设置当外设暂停连接后是否弹出一个警告 http://www.cnblogs.com/roucheng/
NSString *const CBConnectPeripheralOptionNotifyOnNotificationKey;
*/
- (void)connectPeripheral:(CBPeripheral *)peripheral options:(nullable NSDictionary<NSString *, id> *)options;
//取消一个外设的连接
- (void)cancelPeripheralConnection:(CBPeripheral *)peripheral;
其他方法:
//根据获取所有已知设备
- (NSArray<CBPeripheral *> *)retrievePeripheralsWithIdentifiers:(NSArray<NSUUID *> *)identifiers;
//根据服务id获取所有连接的设备
- (NSArray<CBPeripheral *> *)retrieveConnectedPeripheralsWithServices:(NSArray<CBUUID *> *)serviceUUIDs;
代理方法:
//1.在初始化管理中心完成后,会回调代理中的如下方法,我们必须实现如下方法:
//这个方法中可以获取到管理中心的状态
- (void)centralManagerDidUpdateState:(CBCentralManager *)central;
//2.扫描的结果会在如下代理方法中回掉:
//peripheral 扫描到的外设
//advertisementData是外设发送的广播数据
//RSSI 是信号强度 http://www.cnblogs.com/roucheng/
- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary<NSString *, id> *)advertisementData RSSI:(NSNumber *)RSSI;
//3.调用过连接外设的方法后,会回掉如下代理方法:
//连接外设成功
- (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;
//4.当管理中心恢复时会调用如下代理:
//dict中会传入如下键值对
/*
//恢复连接的外设数组
NSString *const CBCentralManagerRestoredStatePeripheralsKey;
//恢复连接的服务UUID数组
NSString *const CBCentralManagerRestoredStateScanServicesKey;
//恢复连接的外设扫描属性字典数组
NSString *const CBCentralManagerRestoredStateScanOptionsKey;
*/
- (void)centralManager:(CBCentralManager *)central willRestoreState:(NSDictionary<NSString *, id> *)dict;
三、外设 CBPeripheralManager
从上面我们知道,中心设备是用来扫描周围的外设,两台设备的通讯中,必须有一个充当中心设备,一个充当外设,外设是由 CBPeripheralManager
进行管理。
//设置外设管理中心代理
@property(assign, nonatomic, nullable) id<CBPeripheralManagerDelegate> delegate;
//外设状态 枚举如中心设备
@property(readonly) CBPeripheralManagerState state;
//是否正在发送广播
@property(readonly) BOOL isAdvertising;
//用户的授权状态
+ (CBPeripheralManagerAuthorizationStatus)authorizationStatus;
初始化方法:
//初始化并设置代理 参数的具体含义与中心设备管理中心
- (instancetype)initWithDelegate:(nullable id<CBPeripheralManagerDelegate>)delegate queue:(nullable dispatch_queue_t);
- (instancetype)initWithDelegate:(nullable id<CBPeripheralManagerDelegate>)delegate queue:(nullable dispatch_queue_t)queue options:(nullable NSDictionary<NSString *, id> *)options;
发送广播:
//advertisementData中可以发送的数据有约定 如下
/*
// 对应设置NSString类型的广播名
NSString *const CBAdvertisementDataLocalNameKey;
// 外设制造商的NSData数据
NSString *const CBAdvertisementDataManufacturerDataKey;
// 外设制造商的CBUUID数据
NSString *const CBAdvertisementDataServiceDataKey;
// 服务的UUID与其对应的服务数据字典数组
NSString *const CBAdvertisementDataServiceUUIDsKey;
// 附加服务的UUID数组
NSString *const CBAdvertisementDataOverflowServiceUUIDsKey;
// 外设的发送功率 NSNumber类型
NSString *const CBAdvertisementDataTxPowerLevelKey;
// 外设是否可以连接
NSString *const CBAdvertisementDataIsConnectable;
// 服务的UUID数组
NSString *const CBAdvertisementDataSolicitedServiceUUIDsKey;
*/
- (void)startAdvertising:(nullable NSDictionary<NSString *, id> *)advertisementData;
//停止发送广播
- (void)stopAdvertising;
其他方法:
//设置一个连接的具体central设备的延时 枚举如下:
/*
typedef NS_ENUM(NSInteger, CBPeripheralManagerConnectionLatency) {
CBPeripheralManagerConnectionLatencyLow = 0,
CBPeripheralManagerConnectionLatencyMedium,
CBPeripheralManagerConnectionLatencyHigh
} NS_ENUM_AVAILABLE(NA, 6_0);
*/
- (void)setDesiredConnectionLatency:(CBPeripheralManagerConnectionLatency)latency forCentral:(CBCentral *)central;
//添加一个服务 http://www.cnblogs.com/roucheng/
- (void)addService:(CBMutableService *)service;
//移除一个服务
- (void)removeService:(CBMutableService *)service;
//移除所有服务
- (void)removeAllServices;
//响应中心设备的读写请求
- (void)respondToRequest:(CBATTRequest *)request withResult:(CBATTError)result;
//更新一个连接中心设备的订阅特征值
- (BOOL)updateValue:(NSData *)value forCharacteristic:(CBMutableCharacteristic *)characteristic onSubscribedCentrals:(nullable NSArray<CBCentral *> *)centrals;
代理方法:
//这个方法是必须实现的 状态可用后可以发送广播
- (void)peripheralManagerDidUpdateState:(CBPeripheralManager *)peripheral;
//连接回复时调用的方法 和centralManager类似
- (void)peripheralManager:(CBPeripheralManager *)peripheral willRestoreState:(NSDictionary<NSString *, id> *)dict;
//开始发送广播时调用的方法
- (void)peripheralManagerDidStartAdvertising:(CBPeripheralManager *)peripheral error:(nullable NSError *)error;
//添加服务调用的回调
- (void)peripheralManager:(CBPeripheralManager *)peripheral didAddService:(CBService *)service error:(nullable NSError *)error;
//当一个central设备订阅一个特征值时调用的方法
- (void)peripheralManager:(CBPeripheralManager *)peripheral central:(CBCentral *)central didSubscribeToCharacteristic:(CBCharacteristic *)characteristic;
//取消订阅一个特征值时调用的方法
- (void)peripheralManager:(CBPeripheralManager *)peripheral central:(CBCentral *)central didUnsubscribeFromCharacteristic:(CBCharacteristic *)characteristic;
//收到读请求时触发的方法
- (void)peripheralManager:(CBPeripheralManager *)peripheral didReceiveReadRequest:(CBATTRequest *)request;
//收到写请求时触发的方法
- (void)peripheralManager:(CBPeripheralManager *)peripheral didReceiveWriteRequests:(NSArray<CBATTRequest *> *)requests;
//外设准备更新特征值时调用的方法
- (void)peripheralManagerIsReadyToUpdateSubscribers:(CBPeripheralManager *)peripheral;
四、中心设备与外设对象 CBCentral
与 CBPeripheral
上面介绍了中心设备管理类与外设管理类,这些类用于将设备连接建立起来,数据交换的服务和一些信息则是在对应的设备对象中。
1.中心设备 CBCentral
属性与方法
//中心设备最大接收的数据长度
@property(readonly, nonatomic) NSUInteger maximumUpdateValueLength;
2.外设 CAPeripheral
属性与方法
外设对象要比中心对象复杂的多,当 centralManager
连接到外设后,需要通过外设对象的代理方法进行数据交互,其中主要方法属性如下:
//设置代理
@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);
//读取信号强度回调的方法 柯乐义 keleyi.com
- (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;
六、服务的特征值 CBCharacteristic
通过绑定服务中的特征值来进行数据的读写操作,其中属性如下:
//对应的服务对象
@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;
七、读写请求对象 CBATTRequest
服务对象是外设向中心设备提供的相关数据服务,获取到相应服务后,中心设备可以进行读写请求,读写对象属性如下:
//对应的中心设备
@property(readonly, nonatomic) CBCentral *central;
//对应的特征值
@property(readonly, nonatomic) CBCharacteristic *characteristic;
//读写数据值
@property(readwrite, copy, nullable) NSData *value;
八、开发流程
- 新建Central Manager实例并进行监听蓝牙设备状态
- 开始搜索外围设备,通过delegate获得数据
- 连接外围设备,delegate通知连接结果
- 获得外围设备的服务,delegate获得结果
- 获得服务的特征,delegate获得结果
- 根据服务和特征给外围设备发送数据
- 根据delegate回调,从外围设备读数据
九、demo
1.中心设备
//
// DJViewController.m
// DJTestDemo
//
// Created by admin on 2021/6/8.
//
#import "DJViewController.h"
#import <CoreBluetooth/CoreBluetooth.h>
#define SERVICE_UUID @"CDD1"
#define CHARACTERISTIC_UUID @"CDD2"
@interface DJViewController ()<CBCentralManagerDelegate,CBPeripheralDelegate>
@property (nonatomic,strong)CBCentralManager *centralManager;
@property (nonatomic, strong) CBPeripheral *peripheral;
@property (nonatomic, strong) CBCharacteristic *characteristic;
@end
@implementation DJViewController
- (void)viewDidLoad {
[super viewDidLoad];
dispatch_queue_t queue = dispatch_queue_create("dj.com", DISPATCH_QUEUE_SERIAL);
_centralManager = [[CBCentralManager alloc]initWithDelegate:self queue:queue options:@{@"CBCentralManagerOptionShowPowerAlertKey":@YES}];
}
/** 读取数据 */
- (void)readData{
[self.peripheral readValueForCharacteristic:self.characteristic];
}
/** 写入数据 */
- (void)writeData{
// 用NSData类型来写入
NSData *data = [@"12345" dataUsingEncoding:NSUTF8StringEncoding];
// 根据上面的特征self.characteristic来写入数据
[self.peripheral writeValue:data forCharacteristic:self.characteristic type:CBCharacteristicWriteWithResponse];
}
#pragma mark - CBCentralManagerDelegate
/** 手机蓝牙状态 */
-(void)centralManagerDidUpdateState:(CBCentralManager *)central{
if (central.state == CBManagerStateUnknown) {
NSLog(@"系统蓝牙当前状态不明确");
}else if (central.state == CBManagerStateResetting){
NSLog(@"系统蓝牙设备正在被重置");
}else if (central.state == CBManagerStateUnsupported){
NSLog(@"系统蓝牙设备不支持");
}else if (central.state == CBManagerStateUnauthorized){
NSLog(@"系统蓝未被授权");
}else if (central.state == CBManagerStatePoweredOff){
NSLog(@"系统蓝牙关闭了,请先打开蓝牙");
}else if (central.state == CBManagerStatePoweredOn){
NSLog(@"系统蓝牙开启");
//扫描设备
[_centralManager scanForPeripheralsWithServices:@[[CBUUID UUIDWithString:SERVICE_UUID]] options:nil];
}
}
/** 扫描结果 */
-(void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary<NSString *,id> *)advertisementData RSSI:(NSNumber *)RSSI{
NSLog(@"%@",advertisementData);
if ([peripheral.name hasPrefix:@"Air"]) {
self.peripheral = peripheral;
//连接设备
[central connectPeripheral:peripheral options:nil];
//停止扫描
[self.centralManager stopScan];
}
}
/** 连接成功 */
-(void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral{
NSLog(@"连接成功");
// 设置代理
peripheral.delegate = self;
// 根据UUID来寻找服务
[peripheral discoverServices:@[[CBUUID UUIDWithString:SERVICE_UUID]]];
}
/** 连接失败 */
-(void)centralManager:(CBCentralManager *)central didFailToConnectPeripheral:(nonnull CBPeripheral *)peripheral error:(nullable NSError *)error{
NSLog(@"连接失败");
// 断开连接可以设置重新连接
[central connectPeripheral:peripheral options:nil];
}
/** 断开连接 */
- (void)centralManager:(CBCentralManager *)central didDisconnectPeripheral:(CBPeripheral *)peripheral error:(nullable NSError *)error {
NSLog(@"断开连接");
// 断开连接可以设置重新连接
[central connectPeripheral:peripheral options:nil];
}
#pragma mark - CBPeripheralDelegate
/** 发现服务 */
-(void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error{
// 遍历出外设中所有的服务
for (CBService *service in peripheral.services) {
NSLog(@"所有的服务:%@",service);
}
/// 这里仅有一个服务,所以直接获取
CBService *service = peripheral.services.lastObject;
// 根据UUID寻找服务中的特征
[peripheral discoverCharacteristics:@[[CBUUID UUIDWithString:CHARACTERISTIC_UUID]] forService:service];
}
/** 发现特征回调 */
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error {
// 遍历出所需要的特征
for (CBCharacteristic *characteristic in service.characteristics) {
NSLog(@"所有特征:%@", characteristic);
// 从外设开发人员那里拿到不同特征的UUID,不同特征做不同事情,比如有读取数据的特征,也有写入数据的特征
}
// 这里只获取一个特征,写入数据的时候需要用到这个特征
self.characteristic = service.characteristics.lastObject;
// 直接读取这个特征数据,会调用didUpdateValueForCharacteristic
[peripheral readValueForCharacteristic:self.characteristic];
// 订阅通知
[peripheral setNotifyValue:YES forCharacteristic:self.characteristic];
}
/** 订阅状态的改变 */
-(void)peripheral:(CBPeripheral *)peripheral didUpdateNotificationStateForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error {
if (error) {
NSLog(@"订阅失败");
NSLog(@"%@",error);
}
if (characteristic.isNotifying) {
NSLog(@"订阅成功");
} else {
NSLog(@"取消订阅");
}
}
/** 接收到数据回调 */
- (void)peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error {
// 拿到外设发送过来的数据
NSData *data = characteristic.value;
NSString *readStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"%@",readStr);
}
/** 写入数据回调 */
- (void)peripheral:(CBPeripheral *)peripheral didWriteValueForCharacteristic:(nonnull CBCharacteristic *)characteristic error:(nullable NSError *)error {
NSLog(@"写入成功");
}
@end
2.外设
//
// DJViewController.m
// DJTestDemo
//
// Created by admin on 2021/6/8.
//
#import "DJViewController.h"
#import <CoreBluetooth/CoreBluetooth.h>
#define SERVICE_UUID @"CDD1"
#define CHARACTERISTIC_UUID @"CDD2"
@interface DJViewController ()<CBPeripheralManagerDelegate>
@property (nonatomic, strong) CBPeripheralManager *peripheralManager;
@property (nonatomic, strong) CBMutableCharacteristic *characteristic;
@end
@implementation DJViewController
- (void)viewDidLoad {
[super viewDidLoad];
// 创建外设管理器,会回调peripheralManagerDidUpdateState方法
self.peripheralManager = [[CBPeripheralManager alloc] initWithDelegate:self queue:dispatch_get_main_queue()];
}
#pragma mark - CBPeripheralManagerDelegate
/** 手机蓝牙状态 */
-(void)peripheralManagerDidUpdateState:(CBCentralManager *)central{
if (central.state == CBManagerStateUnknown) {
NSLog(@"系统蓝牙当前状态不明确");
}else if (central.state == CBManagerStateResetting){
NSLog(@"系统蓝牙设备正在被重置");
}else if (central.state == CBManagerStateUnsupported){
NSLog(@"系统蓝牙设备不支持");
}else if (central.state == CBManagerStateUnauthorized){
NSLog(@"系统蓝未被授权");
}else if (central.state == CBManagerStatePoweredOff){
NSLog(@"系统蓝牙关闭了,请先打开蓝牙");
}else if (central.state == CBManagerStatePoweredOn){
NSLog(@"系统蓝牙开启");
// 创建Service(服务)和Characteristics(特征)
[self setupServiceAndCharacteristics];
// 根据服务的UUID开始广播
[self.peripheralManager startAdvertising:@{CBAdvertisementDataServiceUUIDsKey:@[[CBUUID UUIDWithString:SERVICE_UUID]]}];
}
}
/** 中心设备读取数据的时候回调 */
- (void)peripheralManager:(CBPeripheralManager *)peripheral didReceiveReadRequest:(CBATTRequest *)request {
// 请求中的数据,这里把文本框中的数据发给中心设备
request.value = [self.textField.text dataUsingEncoding:NSUTF8StringEncoding];
// 成功响应请求
[peripheral respondToRequest:request withResult:CBATTErrorSuccess];
}
/** 中心设备写入数据的时候回调 */
- (void)peripheralManager:(CBPeripheralManager *)peripheral didReceiveWriteRequests:(NSArray<CBATTRequest *> *)requests {
// 写入数据的请求
CBATTRequest *request = requests.lastObject;
// 把写入的数据显示在文本框中
self.textField.text = [[NSString alloc] initWithData:request.value encoding:NSUTF8StringEncoding];
}
/** 订阅成功回调 */
-(void)peripheralManager:(CBPeripheralManager *)peripheral central:(CBCentral *)central didSubscribeToCharacteristic:(CBCharacteristic *)characteristic {
NSLog(@"%s",__FUNCTION__);
}
/** 取消订阅回调 */
-(void)peripheralManager:(CBPeripheralManager *)peripheral central:(CBCentral *)central didUnsubscribeFromCharacteristic:(CBCharacteristic *)characteristic {
NSLog(@"%s",__FUNCTION__);
}
/** 创建服务和特征 */
- (void)setupServiceAndCharacteristics {
// 创建服务
CBUUID *serviceID = [CBUUID UUIDWithString:SERVICE_UUID];
CBMutableService *service = [[CBMutableService alloc] initWithType:serviceID primary:YES];
// 创建服务中的特征
CBUUID *characteristicID = [CBUUID UUIDWithString:CHARACTERISTIC_UUID];
CBMutableCharacteristic *characteristic = [
[CBMutableCharacteristic alloc]
initWithType:characteristicID
properties:
CBCharacteristicPropertyRead |
CBCharacteristicPropertyWrite |
CBCharacteristicPropertyNotify
value:nil
permissions:CBAttributePermissionsReadable |
CBAttributePermissionsWriteable
];
// 特征添加进服务
service.characteristics = @[characteristic];
// 服务加入管理
[self.peripheralManager addService:service];
// 为了手动给中心设备发送数据
self.characteristic = characteristic;
}
/** 通过固定的特征发送数据到中心设备 */
- (void)didClickPost {
BOOL sendSuccess = [self.peripheralManager updateValue:[@"111" dataUsingEncoding:NSUTF8StringEncoding] forCharacteristic:self.characteristic onSubscribedCentrals:nil];
if (sendSuccess) {
NSLog(@"数据发送成功");
}else {
NSLog(@"数据发送失败");
}
}
@end
网友评论