iOS蓝牙交互(4.0)

 

  1. 前期准备工作:查看coreBlueTooth框架,查看一些蓝牙名词:Central(中心设备)、Peripheral(外围设备)、advertisementData(广播数据)、-RSSI: 信号强弱值、Services(服务)、Characteristic(特征);
  2. 实例化对象以及遵守代理

    

2.1设置对象

    @property (strong, nonatomic) CBCentralManager *CBManager;

    @property (strong, nonatomic) CBPeripheral *peripheral;

             遵守对应的代理:CBCentralManagerDelegate,CBPeripheralDelegate

 

    2.2实例化中心设备

    //options 后代表的是是否开启alert提示

    self.CBManager = [[CBCentralManager alloc] initWithDelegate:self queue:nil options:@{@"CBCentralManagerOptionShowPowerAlertKey":@NO}];

 

代理方法实现监控蓝牙开启状态以及连接发送数据
    3.1实时监控蓝牙打开状态

    - (void)centralManagerDidUpdateState:(CBCentralManager *)central{

        switch (central.state) {

        case 5:

    // 第一个参数填nil代表扫描所有蓝牙设备,第二个参数options也可以写nil,调用下方方法后会开始扫描周围蓝牙设备开始调用3.2中代理方法

            [self.CBManager scanForPeripheralsWithServices:nil options:@{CBCentralManagerScanOptionAllowDuplicatesKey : [NSNumber numberWithBool:YES]}];

            break;

    }

}

 

    3.2  开始扫描数据,扫描到对应数据后停止扫描

    -(void)centralManager:(CBCentralManager*)central didDiscoverPeripheral:(CBPeripheral*)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI {

             //获取广播数据中的名称

             NSString*localName=[advertisementData valueForKey:@"kCBAdvDataLocalName"];

    if ([localName hasPrefix:@"HC"]) {

        _peripheral = peripheral;

      [self.CBManager connectPeripheral:_peripheral options:nil];

        // 扫描到设备之后停止扫描

       [self.CBManager stopScan];

    }

}

    3.3连接到当前设备

    //连接外设成功,开始发现服务

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

   

        NSLog(@"成功连接 peripheral: %@ with UUID: %@",peripheral,peripheral.identifier);

        // 连接设备之后设置蓝牙对象的代理,扫描服务

        [self.peripheral setDelegate:self];

        [self.peripheral discoverServices:nil];

        NSLog(@"扫描服务");

}

    3.3  发现服务,然后根据服务查找对应的特征

    //已发现服务

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

        NSLog(@"发现服务.");

        for (CBService *s in peripheral.services) {

            if ([s.UUID.UUIDString isEqual:ServicesUUID]) {

                [peripheral discoverCharacteristics:nil forService:s];

        }

}

}

    3.4  根据服务UUID查询对应服务的特征

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

        for (CBCharacteristic *c in service.characteristics) {

            if ([c.UUID.UUIDString isEqual:CharacteristicsUUID]) {

    self.characteristic = c;   

    //把数据进行转NSData编码,转数据成功后写数据给蓝牙外设    

            NSData *data = [self returnDataWithKey:self.BleId];

            [_peripheral setNotifyValue:YES forCharacteristic:c];

            [_peripheral writeValue:data forCharacteristic:self.characteristic type:CBCharacteristicWriteWithoutResponse];

        }

3.5 读取蓝牙外设返回的数据

//获取外设发来的数据,不论是read和notify,获取数据都是从这个方法中读取。

- (void)peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error {

    if ([characteristic.UUID.UUIDString isEqual:CharacteristicsUUID]) {

        NSData * data = characteristic.value;       

        ///<d6c8d6c8 40010000 00000000 00000000 00000000>  成功返回数据

        ///<d6c8d6c8 80010000 00000000 00000000 00000000>  校验和错误返回数据

       // 比对data数据是否是返回成功的data数据或者失败的数据 ,成功返回,提示用户开门成功

}

 

 

  1. 遇到的问题:

4.1连接蓝牙设备发送数据,蓝牙设备收不到,具体原因是不清楚需要连接的硬件具体的服务以及服务对应的特征;

4.2 发送数据是需要发送先把当前的16进制数据转byte,然后转NSData类型数据;

                                    4.2.1 获取到门锁上15位ID号,15位ID字符转16进制数据:

                                    - (NSString *)hexStringFromString:(NSString *)string

                      4.2.2 转换好的15位ID号进行校验和处理,处理后拼接字符串,把拼接好的16进制字符串转byte然后再转NSData类型数据:

- (NSData *)returnDataWithKey:(NSString *)key

4.2.3 NSData数据转NSString类型:

- (NSString *)convertDataToHexStr:(NSData *)data

4.2.4 获取蓝牙外设发送的数据时,每次接收最大为20字节,获取时需要多次获取拼接data,然后再与对应的协议进行对比;

posted @ 2017-09-13 17:33  Mr.pengge  阅读(383)  评论(0编辑  收藏  举报