CoreBluetooth中,须要用到的类和协议(完整导图):html
蓝牙分类中心端和外设端(完整导图)。数组
中心端(接收端)app
1 .建立中心端控制器(CBCentralManager)
2 .扫描设备(Discover)
3 .链接 (Connect)
4 .获取Service和Characteristicide
- 扫描Service (一个service中包含一个或多个Characteristic)
- 获取Service中Characteristic
- 获取Characteristic的值
5 . 数据交互(explore and interact)测试
- 订阅Characteristic的通知
6 . 断开连接ui
外设端(发送端)atom
- 建立Peripheral管理对象
- 建立Service和Characteristic树
- 发送广告
- 处理读写请求和订阅
蓝牙状态 spa
typedef NS_ENUM(NSInteger, CBManagerState) { CBManagerStateUnknown = 0, CBManagerStateResetting, CBManagerStateUnsupported, //不支持 CBManagerStateUnauthorized, //未受权 CBManagerStatePoweredOff, //关闭 CBManagerStatePoweredOn, //蓝牙打开状态 } NS_ENUM_AVAILABLE(NA, 10_0);
链接状态命令行
/*! * @enum CBPeripheralState * * @discussion Represents the current connection state of a CBPeripheral. * */ typedef NS_ENUM(NSInteger, CBPeripheralState) { CBPeripheralStateDisconnected = 0, CBPeripheralStateConnecting, CBPeripheralStateConnected, CBPeripheralStateDisconnecting NS_AVAILABLE(NA, 9_0), } NS_AVAILABLE(NA, 7_0);
CBCentralManagerDelegate 3d
@required // 更新CentralManager状态,参数central就是当前的Manager。 - (void)centralManagerDidUpdateState:(CBCentralManager *)central; @optional // 蓝牙状态的保存和恢复,进入后台和从新启动有关 - (void)centralManager:(CBCentralManager *)central willRestoreState:(NSDictionary<NSString *, id> *)dict; //扫描外部设备 - (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary<NSString *, id> *)advertisementData RSSI:(NSNumber *)RSSI; //与外设完成链接 - (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;
CBPeripheralDelegate
@optional //peripheral更新 - (void)peripheralDidUpdateName:(CBPeripheral *)peripheral NS_AVAILABLE(NA, 6_0); //服务更新时触发 - (void)peripheral:(CBPeripheral *)peripheral didModifyServices:(NSArray<CBService *> *)invalidatedServices NS_AVAILABLE(NA, 7_0); //更新RSSI,过期用下一代替 - (void)peripheralDidUpdateRSSI:(CBPeripheral *)peripheral error:(nullable NSError *)error NS_DEPRECATED(NA, NA, 5_0, 8_0); // RSSI值 - (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; //发现服务中的Characteristic - (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(nullable NSError *)error; //更新Characteristic - (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;
CBPeripheralManagerDelegate
@required //相似CBCentralManager - (void)peripheralManagerDidUpdateState:(CBPeripheralManager *)peripheral; @optional - (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; //订阅 - (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;
从上面了解了蓝牙开发的基本流程和API结构,那下面咱们一块儿看看中心端的开发步骤。
//建立CentralManager - (void)startUpCentralManager { _centralManager = [[CBCentralManager alloc] initWithDelegate: self queue: nil]; _peripherals = [NSMutableArray array]; //用于存放扫描的外设 }
建立CentralManager会调用Delegate方法:
#pragma mark - CBCentralManagerDelegate // 实现: 确保支持BL和有效的Central设备 - (void)centralManagerDidUpdateState:(CBCentralManager *)central { switch (central.state) { case CBManagerStatePoweredOn: //只有此状态下才能够进行扫描设备 [self scan]; break; case CBManagerStateUnknown: break; case CBManagerStateResetting: break; case CBManagerStateUnsupported: break; case CBManagerStateUnauthorized: break; case CBManagerStatePoweredOff: break; default: break; } }
- (void)scan { [self.centralManager scanForPeripheralsWithServices: nil options: nil]; NSLog(@"Scanning started"); }
serviceUUIDs: 是一个CBUUID类型的数组,每一个CBUUID表明一个service的UUID。使用这个参数能够限制扫描内容,若是设置为nil,则表示搜索所有外设。
options: 定义扫描 ,字典。
- CBCentralManagerScanOptionAllowDuplicatesKey : 布尔值,无重复过滤的扫描。默认是NO。若是设置为YES,对电池有不利影响。
- CBCentralManagerScanOptionSolicitedServiceUUIDsKey 想要扫描的Service UUIDs数组(NSArray)。
另外,在
bluetooth-central
后台模式下,option将被忽略。这个问题后面会详细讲解。
当启动扫描后,每次扫描到一个外设就会调用delegate方法:centralManager: didDiscoverPeripheral: advertisementData: RSSI:
- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary<NSString *, id> *)advertisementData RSSI:(NSNumber *)RSSI { NSLog(@"%@", peripheral.name); //查找到设备并持有它,不然不会保存 [self.peripherals addObject: peripheral]; }
- peripheral : 扫描到的设备
- advertisementData: 字典包含全部的广告数据
- RSSI:received signal strength indicator,单位分贝。表示信号强度。
到这一步咱们扫描到外部设备,那接下来就是创建链接了。
经过扫描,咱们发现了目标外设,接下来创建链接。
- (void)startUpConnect { [self.centralManager connectPeripheral: self.peripheral options: nil]; }
self.peripheral : 目标外设
options :字典,用来定制链接行为
- CBConnectPeripheralOptionNotifyOnConnectionKey
- CBConnectPeripheralOptionNotifyOnDisconnectionKey
- CBConnectPeripheralOptionNotifyOnNotificationKey
进行链接一般会出现两种状况:成功、失败。
链接成功
本地链接成功,会调用方法:
- (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral { NSLog(@"连接设备名称为:%@", peripheral.name); // 设置代理 peripheral.delegate = self; //发现服务 [peripheral discoverServices: nil]; }
链接失败
//连接失败 - (void)centralManager:(CBCentralManager *)central didFailToConnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error { NSLog(@"链接名称:%@ 失败,缘由:%@", peripheral.name, error.localizedDescription); }
另外,既然能够创建链接,那么确定能够断开链接。
断开链接
//断开连接 - (void)centralManager:(CBCentralManager *)central didDisconnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error { NSLog(@"与外设断开连接:%@, %@", peripheral.name, error.localizedDescription); }
若是断开链接不是由
cancelPeripheralConnection:
发起的,error会给出详细信息。须要注意的是,当断开链接,全部的services, characteristics和Characteristic descriptions都是无效的。
建立链接成功后,在delegate方法中
// 设置代理 peripheral.delegate = self; //发现服务 [peripheral discoverServices: nil];
能够经过
CBUUID
指定的服务。
CBPeripheralDelegate
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error { if (error) { NSLog(@"发现服务错误:%@", error.localizedDescription); return; } // 一个外设会发送多个服务 for (CBService *service in peripheral.services) { //扫描每一个service的Characteristic,经过Characteristic的UUID来指定查找那个Characteristic。 [peripheral discoverCharacteristics: nil forService: service]; } }
注:一个外设包含多个Service,能够经过Service的UUID来区分。一个Service包含多个Characteristic,经过Characteristic的UUID来区分。
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error { if (error) { NSLog(@"发现特征错误:%@", error.localizedDescription); return; } for (CBCharacteristic *characteristic in service.characteristics) { //直接读取Characteristic值,此时调用peripheral: didUpdateValueForCharacteristic: error: [peripheral readValueForCharacteristic: characteristic]; //另外一种状况,订阅特征。此时调用 peripheral: didUpdateNotificationStateForCharacteristic: error: [peripheral setNotifyValue:YES forCharacteristic: characteristic]; } }
//给指定的特征设置通知 - (void)peripheral:(CBPeripheral *)peripheral didUpdateNotificationStateForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error { if (error) { NSLog(@"Error changing notification state : %@", error.localizedDescription); } if (![characteristic.UUID isEqual:[CBUUID UUIDWithString:@""]]) { return; } if (characteristic.isNotifying) { NSLog(@"Notification began on :%@", characteristic); } else { NSLog(@"Notification stoped on : %@ Disconnecting", characteristic); [self.centralManager cancelPeripheralConnection: peripheral]; } }
//readValueCharacteristic时调用 - (void)peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error { //打印出characteristic的UUID和值 //!注意,value的类型是NSData,具体开发时,会根据外设协议制定的方式去解析数据 NSLog(@"特性ID::%@ 值:%@",characteristic.UUID, characteristic.value); }
- (void)writeValue { NSData *data = [@"Test" dataUsingEncoding:NSUTF8StringEncoding]; [self.peripheral writeValue: data forCharacteristic: self.characteristic type: CBCharacteristicWriteWithResponse]; }
发送数据
- value: 写入值
- characteristic : 被写入的特征
- type: 写入类型,是否有应答
typedef NS_ENUM(NSInteger, CBCharacteristicWriteType) { CBCharacteristicWriteWithResponse = 0, CBCharacteristicWriteWithoutResponse, };当type为CBCharacteristicWriteWithResponse时,调用delegate方法:
peripheral: didWriteValueForCharacteristic: error:
。
//建立PeripheralManager - (void)startUpPeripheralManager { _peripheralManager = [[CBPeripheralManager alloc] initWithDelegate:self queue: nil]; } //惟一@required方法 - (void)peripheralManagerDidUpdateState:(CBPeripheralManager *)peripheral { switch (peripheral.state) { case CBManagerStatePoweredOn: // 建立服务和特征 [self buildService]; break; case CBManagerStateUnknown : break; case CBManagerStateResetting: break; case CBManagerStateUnsupported: break; case CBManagerStateUnauthorized: break; case CBManagerStatePoweredOff: break; default: break; } }
- (void)buildService { //建立Characteristic self.transferCharacteristic = [[CBMutableCharacteristic alloc] initWithType:[CBUUID UUIDWithString:TRANSFER_CHARACTERISTIC_UUID] properties:CBCharacteristicPropertyNotify value: nil permissions:CBAttributePermissionsReadable]; //建立服务 CBMutableService *transferService = [[CBMutableService alloc] initWithType:[CBUUID UUIDWithString:TRANSFER_SERVICE_UUID] primary:YES]; //将特征添加到服务中 transferService.characteristics = @[self.transferCharacteristic]; //将服务添加到PeripheralManager中,调用delegate方法: peripheralManager: didAddService: error: [self.peripheralManager addService: transferService]; }
建立Characteristic
- UUID : 惟一标识
- properties:属性
- value : 设置nil, 动态设置。
- permission : 权限
属性
typedef NS_OPTIONS(NSUInteger, CBCharacteristicProperties) { //特征值能够使用特征描述广播 CBCharacteristicPropertyBroadcast = 0x01, //特征值能够被读取,经过readValueForCharacteristic: 来读取 CBCharacteristicPropertyRead = 0x02, // 能够被读写,经过writeValue: forCharacteristic: type: 写入特征值,无应答标识写入成功。 CBCharacteristicPropertyWriteWithoutResponse = 0x04, // 能够被写入,有应答标识写入成功 CBCharacteristicPropertyWrite = 0x08, //容许通知特征值,无应答表示接收 CBCharacteristicPropertyNotify = 0x10, //容许通知特征值,有应答表示接收 CBCharacteristicPropertyIndicate = 0x20, CBCharacteristicPropertyAuthenticatedSignedWrites = 0x40, CBCharacteristicPropertyExtendedProperties = 0x80, //只有信任的设备接收特征值通知 CBCharacteristicPropertyNotifyEncryptionRequired NS_ENUM_AVAILABLE(NA, 6_0) = 0x100, CBCharacteristicPropertyIndicateEncryptionRequired NS_ENUM_AVAILABLE(NA, 6_0) = 0x200 };
属性特征,能够组合使用。
权限
typedef NS_OPTIONS(NSUInteger, CBAttributePermissions) { // 可读 CBAttributePermissionsReadable = 0x01, // 可写 CBAttributePermissionsWriteable = 0x02, // 信任可读 CBAttributePermissionsReadEncryptionRequired = 0x04, // 信任可写 CBAttributePermissionsWriteEncryptionRequired = 0x08 } NS_ENUM_AVAILABLE(NA, 6_0);
UUID : 惟一标识
isPrimary:YES:主服务;NO:次服务添加特征到characteristics数据
调用delegate方法: peripheralManager: didAddService: error:
//开始广告 - (void)startAdvertise { [self.peripheralManager startAdvertising:@{CBAdvertisementDataServiceUUIDsKey : @[[CBUUID UUIDWithString:TRANSFER_SERVICE_UUID]]}]; } //中止广告 - (void)stopAdvertise { [self.peripheralManager stopAdvertising]; }
开始广告
advertisementData: 可选字典,包含想要广告的数据。两种类型:
- CBAdvertisementDataLocalNameKey :对应名称
- CBAdvertisementDataServiceUUIDsKey : 对应UUID
调用delegate方法:peripheralManagerDidStartAdvertising: error:
- (void)peripheralManager:(CBPeripheralManager *)peripheral central:(CBCentral *)central didSubscribeToCharacteristic:(CBCharacteristic *)characteristic { NSLog(@"当前Characteristic被订阅"); }
当characteristic配置为notify或indicate,将唤起次方法。
- (void)peripheralManager:(CBPeripheralManager *)peripheral central:(CBCentral *)central didUnsubscribeFromCharacteristic:(CBCharacteristic *)characteristic { NSLog(@"Central unsubscribed from characteristic"); }
当central移除特征的notification/indications时调用,取消订阅回调。
- (void)peripheralManager:(CBPeripheralManager *)peripheral didReceiveReadRequest:(CBATTRequest *)request { } - (void)peripheralManager:(CBPeripheralManager *)peripheral didReceiveWriteRequests:(NSArray<CBATTRequest *> *)requests { }
当接收到中心端的读写请求式,会触发这个方法。
在该方法被调用时,能够在方法内经过PeripheralManager调用:respondToRequest: withResult:
来响应中心端的读写请求。
plist文件中,设置UIBackgroundModes:
- bluetooth-central
- bluetooth-peripheral
当设置为此模式,容许APP切入后台后还能进行蓝牙服务。依然能进行扫描,链接,交互数据等。
进入后台CBCentralManagerScanOptionAllowDuplicatesKey
扫描被忽略。
当设置为此模式,容许APP进行读写,链接中心端等。CBAdvertisementDataLocalNameKey
被忽略,本地外设名不在广播。
PeripheralManager
#import "LQPeripheralManager.h" #import <CoreBluetooth/CoreBluetooth.h> static NSString *const ServiceUUID1 = @"FFF0"; static NSString *const notifyCharacteristicUUID = @"FFF1"; static NSString *const readwriteCharacteristicUUID = @"FFF2"; static NSString *const ServiceUUID2 = @"FFE0"; static NSString *const readCharacteristicUUID = @"FFE1"; static NSString *const LocalNameKey = @"Owenli"; @interface LQPeripheralManager ()<CBPeripheralManagerDelegate> @property (nonatomic, strong) CBPeripheralManager *peripheralManager; @property (nonatomic, strong) NSTimer *timer; @property (nonatomic, assign) NSInteger index; @end @implementation LQPeripheralManager + (instancetype)shareInstance { static LQPeripheralManager *peripheral; static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ peripheral = [[self alloc] init]; }); return peripheral; } - (instancetype)init { if (self = [super init]) { _peripheralManager = [[CBPeripheralManager alloc] initWithDelegate:self queue:nil]; _index = 0; } return self; } /** * @Description 初始化化UUID和服务信息 */ - (void)setup { //characteristic字段描述 CBUUID *cbuuidCharacteristicUserDescriptionStringUUID = [CBUUID UUIDWithString:CBUUIDCharacteristicUserDescriptionString]; /* 能够通知的Characteristic properities : CBCharacteristicPropertyNotify permissions : CBAttributePermissionsReadable */ CBMutableCharacteristic *notifyCharacteristic = [[CBMutableCharacteristic alloc] initWithType:[CBUUID UUIDWithString:notifyCharacteristicUUID] properties:CBCharacteristicPropertyNotify value:nil permissions:CBAttributePermissionsReadable]; /* 可读写的characteristic prperitise: CBCharacteristicPropertyWrite | CBCharacteristicPropertyRead permisssions : CBAttributePermissionsReadable | CBAttributePermisssionWriteable */ CBMutableCharacteristic * readwriteCharacteristic = [[CBMutableCharacteristic alloc] initWithType:[CBUUID UUIDWithString:readwriteCharacteristicUUID] properties:CBCharacteristicPropertyWrite | CBCharacteristicPropertyRead value:nil permissions:CBAttributePermissionsReadable | CBAttributePermissionsWriteable]; // 设置descriptor CBMutableDescriptor *readwriteCharacteristicDescription1 = [[CBMutableDescriptor alloc] initWithType:cbuuidCharacteristicUserDescriptionStringUUID value:@"name"]; [readwriteCharacteristic setDescriptors:@[readwriteCharacteristicDescription1]]; /* 只读Characteristic properties: CBCharacteristicPropertyRead permisssions: CBAttributePermissionsReadable */ CBMutableCharacteristic *readCharacteristic = [[CBMutableCharacteristic alloc] initWithType:[CBUUID UUIDWithString:readCharacteristicUUID] properties:CBCharacteristicPropertyRead value:nil permissions:CBAttributePermissionsReadable]; // 第一个Service CBMutableService *service1 = [[CBMutableService alloc] initWithType:[CBUUID UUIDWithString:ServiceUUID1] primary:YES]; [service1 setCharacteristics:@[notifyCharacteristic, readwriteCharacteristic]]; //第二个Service CBMutableService *service2 = [[CBMutableService alloc] initWithType:[CBUUID UUIDWithString:ServiceUUID2] primary:YES]; [service2 setCharacteristics:@[readCharacteristic]]; //添加到periperal此时会调用,peripheralManager: didAddService: error: [self.peripheralManager addService:service2]; [self.peripheralManager addService:service1]; } #pragma mark - PeripherManagerDelegate //检测蓝牙状态, - (void)peripheralManagerDidUpdateState:(CBPeripheralManager *)peripheral { switch (peripheral.state) { case CBManagerStatePoweredOn: //初始化 [self setup]; break; case CBManagerStatePoweredOff: NSLog(@"powered off"); break; default: break; } } - (void)peripheralManager:(CBPeripheralManager *)peripheral didAddService:(CBService *)service error:(NSError *)error { if (error) { NSLog(@"%@", error.localizedDescription); return; } //添加服务后,开始广播 //自动回调: peripheralManagerDidStartAdvertising: error: [self.peripheralManager startAdvertising:@{ CBAdvertisementDataServiceUUIDsKey : @[[CBUUID UUIDWithString:ServiceUUID1], [CBUUID UUIDWithString:ServiceUUID2]], CBAdvertisementDataLocalNameKey : LocalNameKey }]; } //通知发送广播 - (void)peripheralManagerDidStartAdvertising:(CBPeripheralManager *)peripheral error:(NSError *)error { if (error) { NSLog(@"%@", error.localizedDescription); } NSLog(@"start advert"); } - (void)peripheralManager:(CBPeripheralManager *)peripheral central:(CBCentral *)central didSubscribeToCharacteristic:(CBCharacteristic *)characteristic { NSLog(@"subscribe data of : %@", characteristic.UUID); //分配定时任务 self.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(sendData:) userInfo:characteristic repeats:YES]; } - (void)peripheralManager:(CBPeripheralManager *)peripheral central:(CBCentral *)central didUnsubscribeFromCharacteristic:(CBCharacteristic *)characteristic { NSLog(@"unsubscrible: %@", characteristic.UUID); [self.timer invalidate]; } - (void)sendData:(NSTimer *)timer { CBMutableCharacteristic *characteristic = timer.userInfo; if ([self.peripheralManager updateValue:[[NSString stringWithFormat:@"send data : %ld", _index] dataUsingEncoding: NSUTF8StringEncoding] forCharacteristic:characteristic onSubscribedCentrals:nil]) { NSLog(@"发送成功"); _index ++; } else { NSLog(@"发送数据错误"); } } //中心设备读取请求 - (void)peripheralManager:(CBPeripheralManager *)peripheral didReceiveReadRequest:(CBATTRequest *)request { NSLog(@"readRequest"); if (request.characteristic.properties & CBCharacteristicPropertyRead) { NSData *data = [[NSString stringWithFormat:@"by characteristic request"] dataUsingEncoding:NSUTF8StringEncoding]; [request setValue:data]; //对请求做出成功响应 [self.peripheralManager respondToRequest:request withResult:CBATTErrorSuccess]; } else { [self.peripheralManager respondToRequest:request withResult:CBATTErrorWriteNotPermitted]; } } //写入请求 - (void)peripheralManager:(CBPeripheralManager *)peripheral didReceiveWriteRequests:(NSArray<CBATTRequest *> *)requests { NSLog(@"writeRequest"); CBATTRequest *request = requests.firstObject; if (request.characteristic.properties & CBCharacteristicPropertyWrite) { CBMutableCharacteristic *charateristic = (CBMutableCharacteristic *)request.characteristic; charateristic.value = request.value; NSLog(@"receive data :%@", [[NSString alloc] initWithData:charateristic.value encoding:NSUTF8StringEncoding]); [self.peripheralManager respondToRequest:request withResult:CBATTErrorSuccess]; } else { [self.peripheralManager respondToRequest:request withResult:CBATTErrorWriteNotPermitted]; } } - (void)peripheralManagerIsReadyToUpdateSubscribers:(CBPeripheralManager *)peripheral { NSLog(@"peripheralManagerIsReadyToUpdateSubscribers"); } @end
使用LighBlue测试外设端
命令行生成UUID
方法:uuidgen
。
Mac测试软件:LighBlue,能够用来测试iOS端外设。