蓝牙(2) Core Bluetooth

 Core Bluetooth使用步骤

 

 1.建立中心设备

 2.扫描外设

 3.链接外设

 4.扫描外设中的服务和特征

 5.利用特征与外设做数据交互

 6.断开连接

 

 1.导入框架#import <CoreBluetooth/CoreBluetooth.h>

 2.创建中心设备  

 2-1.创建对象CBCentralManager *manger = [[CBCentralManager alloc]init];

 [manger scanForPeripheralsWithServices:nil options:nil];

 2-2.实现代理manger.delegate = self;

 2-3.创建一个数组

 2-4实现代理方法

 3.连接所有的外设

 -(void)start{

 for (CBPeripheral *peripheral in _dataArray) {

 //连接外设

 [manger connectPeripheral:peripheral options:nil];

 

 }

 

 }

4.实现代理

 -(void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral{

 扫描外设中的服务

 [ peripheral discoverServices:nil];

 }

连接外部设备

 -(void)centralManager:(CBCentralManager *)central didDisconnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error{

 

 }

 -(void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error{

扫描到服务就会调用

服务所在的外设,获取外设中所有扫描的服务

 NSArray *service =  peripheral.services;

 for (CBService *ser in service) {

过滤不需要的服务,拿到需要的服务,从需要的服务中查找需要的特征

 if ([ser.UUID.UUIDString isEqualToString:@""]) {

从外设中扫描

 [peripheral discoverCharacteristics:nil forService:ser];

 }

 }

 }

 -(void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error{

 拿到服务中所有的特征,拿到需要的特征处理

 NSArray *characteristics = service.characteristics;

 for (CBCharacteristic *character in characteristics ) {

 if ([character.UUID.UUIDString isEqualToString:@""]) {

 NSLog(@"设置");

 }

 }

 }

你可能感兴趣的:(core,BlueTooth)