您的位置:首页 > 产品设计 > UI/UE

IOS CoreBluetooth系列三:实战之远程 Central 和本地 Peripheral

2016-11-16 14:14 351 查看
前言:上文中我们主要是对本地 Central 和远程 Peripheral,接下来主要讲的是远程 Central 和本地 Peripheral。

大致步骤如下:

1、首先需要导入<CoreBluetooth/CoreBluetooth.h>这个框架,并在info配置Privacy - Bluetooth Peripheral Usage Description权限;

2、其次需要遵循CBPeripheralManagerDelegate这个代理;

3、创建一个CBPeripheralManager类;

self.manager = [[CBPeripheralManager alloc] initWithDelegate:self queue:nil];

4、检查是否支持BLE标准
#pragma mark 检验是否支持BLE标准
- (void)peripheralManagerDidUpdateState:(CBPeripheralManager *)peripheral
{
switch (peripheral.state) {
case CBPeripheralManagerStatePoweredOn:
[self setupSerive];
break;
default:
NSLog(@"Peripheral Manager did change state");
break;
}
}5、支持的话,需要设置服务
#pragma mark 设置服务
- (void)setupSerive
{
//1.设置特征
CBUUID *characteristicUUID = [CBUUID UUIDWithString:KCharacteristicUUID];
self.customCharacteristic = [[CBMutableCharacteristic alloc] initWithType:characteristicUUID properties:CBCharacteristicPropertyNotify value:nil permissions:CBAttributePermissionsReadable];

//2.设置服务
CBUUID *serviceUUID = [CBUUID UUIDWithString:KServiceUUID];
self.customServive = [[CBMutableService alloc] initWithType:serviceUUID primary:YES];

//3.将服务添加至CBPeripheralManager中
[self.customServive setCharacteristics:@[self.customCharacteristic]];
[self.manager addService:self.customServive];
}

6、实现代理
//当周边管理者开始广播服务,他的代理接收-peripheralManagerDidStartAdvertising:error: 消息,并且当中央预定了这个服务,他的代理接收 -peripheralManager:central:didSubscribeToCharacteristic:消息,这儿是你给中央生成动态数据的地方
- (void)peripheralManager:(CBPeripheralManager *)peripheral didAddService:(CBService *)service error:(NSError *)error
{
if (error == nil) {
[self.manager startAdvertising:@{CBAdvertisementDataLocalNameKey: @"ICServer",CBAdvertisementDataServiceUUIDsKey: @[[CBUUID UUIDWithString:KServiceUUID]]}];
}
}

demo详细代码
#import "ViewController.h"
#import <CoreBluetooth/CoreBluetooth.h>

//在terminal中执行uuidgen
static NSString *const KServiceUUID = @"9830BC16-5CE7-43CD-996F-74E9110C4CEA";
static NSString *const KCharacteristicUUID = @"F6F6B7E0-897D-4F51-959F-387A94BF440E";

@interface ViewController ()<CBPeripheralManagerDelegate>

@property (nonatomic,strong) CBPeripheralManager *manager;

@property (nonatomic,strong) CBMutableCharacteristic *customCharacteristic;

@property (nonatomic,strong) CBMutableService *customServive;

@end

@implementation ViewController

- (void)viewDidLoad {
[super viewDidLoad];

self.manager = [[CBPeripheralManager alloc] initWithDelegate:self queue:nil];

}

#pragma mark 检验是否支持BLE标准
- (void)peripheralManagerDidUpdateState:(CBPeripheralManager *)peripheral
{
switch (peripheral.state) {
case CBPeripheralManagerStatePoweredOn:
[self setupSerive];
break;
default:
NSLog(@"Peripheral Manager did change state");
break;
}
}

#pragma mark 设置服务
- (void)setupSerive
{
//1.设置特征
CBUUID *characteristicUUID = [CBUUID UUIDWithString:KCharacteristicUUID];
self.customCharacteristic = [[CBMutableCharacteristic alloc] initWithType:characteristicUUID properties:CBCharacteristicPropertyNotify value:nil permissions:CBAttributePermissionsReadable];

//2.设置服务
CBUUID *serviceUUID = [CBUUID UUIDWithString:KServiceUUID];
self.customServive = [[CBMutableService alloc] initWithType:serviceUUID primary:YES];

//3.将服务添加至CBPeripheralManager中
[self.customServive setCharacteristics:@[self.customCharacteristic]];
[self.manager addService:self.customServive];
}

//当周边管理者开始广播服务,他的代理接收-peripheralManagerDidStartAdvertising:error: 消息,并且当中央预定了这个服务,他的代理接收 -peripheralManager:central:didSubscribeToCharacteristic:消息,这儿是你给中央生成动态数据的地方
- (void)peripheralManager:(CBPeripheralManager *)peripheral didAddService:(CBService *)service error:(NSError *)error
{
if (error == nil) {
[self.manager startAdvertising:@{CBAdvertisementDataLocalNameKey: @"ICServer",CBAdvertisementDataServiceUUIDsKey: @[[CBUUID UUIDWithString:KServiceUUID]]}];
}
}

@end
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: