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
大致步骤如下:
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
相关文章推荐
- IOS CoreBluetooth系列二:实战之本地 Central 和远程 Peripheral
- Core Bluetooth框架之一:Central与Peripheral
- Core Bluetooth框架之一:Central与Peripheral
- IOS CoreBluetooth系列一:理论
- Core Bluetooth框架之一:Central与Peripheral
- 【转】Core Bluetooth框架之一:Central与Peripheral
- IOS CoreBluetooth系列四:一个简单的BlueTooth管理类
- 小米手环iOS开发实战(一):iOS蓝牙框架CoreBluetooth
- iOS 上的蓝牙框架 - Core Bluetooth for iOS
- iOS本地通知和远程通知
- ios学习--ios本地通知和远程通知
- iOS 上的蓝牙框架 - Core Bluetooth for iOS
- iOS CoreBluetooth 教程
- iOS CoreBluetooth 教程
- iOS CoreBluetooth 教程
- IOS本地,APNS远程推送(具体过程)
- Core Bluetooth for iOS 6蓝牙技术
- iOS远程和本地视频播放-----MPMoviePlayerController
- ios本地通知和远程通知
- IOS播放视频本地的和远程的