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

前言:上文中我们主要是对本地 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



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值