美文网首页
BLE蓝牙开发 (二) 手机作为中心设备开发

BLE蓝牙开发 (二) 手机作为中心设备开发

作者: Maggie的小蜗居 | 来源:发表于2017-05-25 16:04 被阅读81次

中心设备连接外设流程

1. 初始化中心设备管理器
2. 检测蓝牙开启的状态
3. 蓝牙可用情况下扫描外设
4. 接收到外设广播
5. 连接外设
6. 扫描外设中的服务和特征
7.与外设数据交互
8. 订阅Characteristic的通知
9.断开连接
1. 建立中心设备管理器
#import <CoreBluetooth/CoreBluetooth.h>

#define CHARACTERISTIC_UUID     @"1801"
#define SERVICE_BIKE_UUID       @"FFA0"

@interface ViewController ()<CBCentralManagerDelegate,CBPeripheralDelegate> 

@property (nonatomic, strong) CBCentralManager  *centerMgr;
@property (nonatomic, strong) NSMutableArray *peripherals;
@property (nonatomic, strong) CBPeripheral      *peripheral;
@property (nonatomic, strong) CBCharacteristic  *characteristic;
@property (nonatomic, weak) IBOutlet UITextField *dataTextField;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.peripherals = [[NSMutableArray alloc] init];
    self.centerMgr = [[CBCentralManager alloc] initWithDelegate:self queue:dispatch_get_main_queue()];

}

2. 检测蓝牙状态

扫描时services参数为nil时扫描出所有的外设 2.根据指定的服务UUID扫描出对设 (遇到情况制定了UUID之后扫描不到外设了,不知道为什么!!)

#pragma mark - CBCentralManagerDelegate -
- (void)centralManagerDidUpdateState:(CBCentralManager *)central {
    /*
     CBManagerStateUnknown = 0,     // 未知
     CBManagerStateResetting,       // 重置中
     CBManagerStateUnsupported,     // 不支持
     CBManagerStateUnauthorized,    // 未验证
     CBManagerStatePoweredOff,      // 关闭
     CBManagerStatePoweredOn,       // 开启
     */
    if(central.state == CBManagerStatePoweredOn) {
        //扫描外设 
//        [central scanForPeripheralsWithServices:@[[CBUUID UUIDWithString:SERVICE_BIKE_UUID]] options:@{CBCentralManagerScanOptionAllowDuplicatesKey : @YES }];

        [central scanForPeripheralsWithServices:nil
                                        options:nil];
    }
}
3. 发现外设之后,连接外设,并根据服务的UUID找到特征值

每个外设都有一个UUID,为identifier.UUIDString,这个UUID根据手机的不同而不同,但每一台手机的identifier.UUIDString都是一样

// 当发现外设后回调
- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary<NSString *, id> *)advertisementData RSSI:(NSNumber *)RSSI {
    
    
    //连接外设
    
    //过滤出自己需要的外设,可根据设备的名称,设备的ServiceUUID 或者mac地址
    if ([peripheral.name isEqualToString:@"名称"]) {
        self.peripheral = peripheral; //过滤出来的外设需要保存下来,持有它,可用来读写数据

        [self.centerMgr connectPeripheral:peripheral options:nil];
    }
    
    [self.peripherals addObject:peripheral];
    NSLog(@"peripheral:%@%@",peripheral.name,peripheral.identifier.UUIDString);
}

4.连接外设之后对应的回调
// 当外设被连接以后回调
- (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral {
    peripheral.delegate = self;
    //扫描外设中的服务
    [peripheral discoverServices:@[[CBUUID UUIDWithString:SERVICE_BIKE_UUID]]];
}

//断开连接的情况下发起重连
- (void)centralManager:(CBCentralManager *)central didDisconnectPeripheral:(CBPeripheral *)peripheral error:(nullable NSError *)error {
    [central connectPeripheral:peripheral options:nil];
}

5. 连接外设,读取外设值 CBPeripheralDelegate
// 当外设的服务被发现后回调
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(nullable NSError *)error {
    //扫描服务中的指定特征
    for (CBService *service in peripheral.services) {
        [peripheral discoverCharacteristics:@[[CBUUID UUIDWithString:CHARACTERISTIC_UUID]] forService:service];
    }
}

// 当我们发现特征后回调
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(nullable NSError *)error {
    //便利特征进行交互    
    for (CBCharacteristic *c in service.characteristics) {
        
        if ([c.UUID isEqual:[CBUUID UUIDWithString:@"1801"]]) {
            
            self.characteristic = c;
            
            [self.peripheral readValueForCharacteristic:c];
            //设为YES之后 外设特征值有变化就会执行 didUpdateValueForCharacteristic回调方法
            [self.peripheral setNotifyValue:YES forCharacteristic:c];

        }

    }
    
}

// 具体某个特征值发生改变后回调
- (void)peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic error:(nullable NSError *)error {
    
    NSLog(@"didUpdateValueForCharacteristic:%@",characteristic.value);
//    NSString *ssss = [[NSString alloc] initWithData:[characteristic value] encoding:NSUTF8StringEncoding];
    //
    Byte * resultByte = (Byte *)[characteristic.value bytes];
    for(int i=0;i < [characteristic.value length];i++) {
        printf("testByteFF02[%d] = %d\n",i,resultByte[i]);
        self.dataTextField.text = [NSString stringWithFormat:@"%d",resultByte[i]];
    }
    
}

// 当某个特征值被写入时回调
- (void)peripheral:(CBPeripheral *)peripheral didWriteValueForCharacteristic:(CBCharacteristic *)characteristic error:(nullable NSError *)error {
    
    NSLog(@"didWriteValueForCharacteristic:%@",characteristic.value);
    [self.peripheral readValueForCharacteristic:characteristic];
}

与硬件交互都是以Byte[]数组来交互

6.中心设备向外设发送指令
- (IBAction)writeData:(id)sender {
    
    // 写入数据  发送一个0702(清除数据的指令)
    
    Byte byte[] = {7,2};
    // 需要有响应 CBCharacteristicWriteWithResponse 会执行didWriteValueForCharacteristic回调
    [self.peripheral writeValue:[NSData dataWithBytes:byte length:2] 
    forCharacteristic:self.characteristic type:CBCharacteristicWriteWithResponse];
}
7.订阅characteristic
-(void)notifyCharacteristic:(CBPeripheral *)peripheral
                characteristic:(CBCharacteristic *)characteristic{
        //设置通知,数据通知会进入:didUpdateValueForCharacteristic方法
        [peripheral setNotifyValue:YES forCharacteristic:characteristic];
 
    }
 
    //取消通知
    -(void)cancelNotifyCharacteristic:(CBPeripheral *)peripheral
                 characteristic:(CBCharacteristic *)characteristic{
 
         [peripheral setNotifyValue:NO forCharacteristic:characteristic];
    }

相关文章

  • BLE蓝牙开发 (二) 手机作为中心设备开发

    中心设备连接外设流程 1. 建立中心设备管理器 2. 检测蓝牙状态 扫描时services参数为nil时扫描出所...

  • iOS BLE Scan

    iOS扫描BLE设备的方法 iOS在使用CoreBluetooth框架进行BLE开发时,通常作为中心设备(Mast...

  • iOS蓝牙开发

    iOS蓝牙开发有三个框架 蓝牙开发有两种模式:1.手机作为中心设备,获取外设的数据2.手机作为外设,对外提供数据(...

  • 蓝牙开发-中心设备开发

    蓝牙开发中心设备开发外设开发 概念理解 中心设备: 主动连接其他蓝牙设备的一方,可以通过蓝牙指令操作蓝牙设备或读取...

  • android4.0低功耗Ble详解

    首先我们要知道Ble是在android4.3被引入的 Ble开发分为俩个设备 1.中心设备:中心设备就是我们的手机...

  • iOS蓝牙开发总结

    蓝牙开发有两种模式: 中心者模式:苹果手机作为中心设备,连接其他设备,从其他设备接受数据或者进行传输,比如连接体重...

  • 微信小程序 BLE开发

    一、前言 微信小程序蓝牙开发官方文档地址 二、小程序BLE开发步骤 1、初始化蓝牙适配器及开始搜索目标设备 2、监...

  • BLE蓝牙开发 (三) 手机作为外设开发

    peripheral模式的流程: 实现步骤: 1. 初始化外设 2. 检测外设的蓝牙状态后创建服务(service...

  • Android零散技术点

    Android BLE 蓝牙开发入门 逐步指导新手进行 Android ble 蓝牙的开发,避免踩坑。Androi...

  • Device is not authorized to use

    用 React Native 开发 APP 的时候,需要和蓝牙设备交互,于是选了 React Native BLE...

网友评论

      本文标题:BLE蓝牙开发 (二) 手机作为中心设备开发

      本文链接:https://www.haomeiwen.com/subject/drczxxtx.html