美文网首页
iOS蓝牙连接读取和写入信息(二)

iOS蓝牙连接读取和写入信息(二)

作者: 幻风0628 | 来源:发表于2019-05-14 09:55 被阅读0次

    蓝牙读取信息过程:连接-查询服务-发现服务、搜索该服务包含的特征值-发现特征值-读取特征值(读取所需外设信息,包含major、minor、rssi、电量等等)

    蓝牙外设类CBPeripheral (代码中的所有特征值UUID均向硬件厂商获取)

    @property (strong, nonatomic) CBPeripheral *beaconPeripheral; 
     self.beaconServiceUUID = [CBUUID UUIDWithString:@"xxxxxxxxxxx"]; //服务UUID需要找硬件厂商获取
    

    连接代码

     self.beaconPeripheral.delegate = self;
      [self.bluetoothManager connectPeripheral:self.beaconPeripheral options:nil];
    

    连接代理-CBCentralManagerDelegate

    -(void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral
    {
        NSLog(@"蓝牙连接成功 %@",peripheral.name);
        [self.beaconPeripheral discoverServices:[NSArray arrayWithObject:self.beaconServiceUUID]];//连接成功后查询beacon服务
    }
    -(void)centralManager:(CBCentralManager *)central didDisconnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error
    {
        NSLog(@"蓝牙断开连接 %@",peripheral.name);
    }
    -(void)centralManager:(CBCentralManager *)central didFailToConnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error
    {
        NSLog(@"连接失败 %@",peripheral.name);
    }
    

    发现beacon服务代理--CBPeripheralDelegate

    -(void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error
    {
        for(CBService *service in peripheral.services)
        {
            if ([service.UUID isEqual:self.beaconServiceUUID]) {
                //发现服务,搜索特征值
                [self.beaconPeripheral discoverCharacteristics:nil forService:service];
            }
        }
    }
    -(void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error
    {
        if ([service.UUID isEqual:self.beaconServiceUUID]) {
            for(CBCharacteristic *characteristic in service.characteristics)
            {
               //majorminor值
                if ([characteristic.UUID isEqual:self.majorMinorCharacteristicUUID]) {
                    self.majorMinorCharacteristic = characteristic;
                    [self.beaconPeripheral readValueForCharacteristic:characteristic];//读取
                }
                if ([characteristic.UUID isEqual:self.rssiCharacteristicUUID]) {
                    self.rssiCharacteristic = characteristic;
                    [self.beaconPeripheral readValueForCharacteristic:characteristic];
                }
            }
        }
    }
    //获取特征值value
    -(void)peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error
    {
        if ([characteristic.UUID isEqual:self.majorMinorCharacteristicUUID]) {
            NSString *major = [NSString stringWithFormat:@"%hu",[self decodeMajor:characteristic.value]];
            NSString *minor = [NSString stringWithFormat:@"%hu",[self decodeMinor:characteristic.value]];
        }
        else if ([characteristic.UUID isEqual:self.rssiCharacteristicUUID]) {
            NSString *rssi = [NSString stringWithFormat:@"%hhd",[self decodeRSSI:characteristic.value]];
       }
    }
    

    写入操作

    写入major、minor

      uint16_t major = (uint16_t)majorVlue;
      uint16_t minor = (uint16_t)minorValue;
      uint8_t majorMinor[4];
      majorMinor[0] = ((major >> 8) & 0xFF);
      majorMinor[1] = (major & 0xFF);
      majorMinor[2] = ((minor >> 8) & 0xFF);
      majorMinor[3] = (minor & 0xFF);
      NSData *data = [NSData dataWithBytes:majorMinor length:4];
      [self.beaconPeripheral writeValue:data forCharacteristic:self.majorMinorCharacteristic type:CBCharacteristicWriteWithResponse];
    

    写入rssi

     int8_t rssi = (int8_t)rssiValue;
        NSLog(@"rssi before save: %hhd",rssi);
        NSData *data = [NSData dataWithBytes:&rssi length:1];
        [self.beaconPeripheral writeValue:data forCharacteristic:self.rssiCharacteristic type:CBCharacteristicWriteWithResponse];
    

    写入回调

    -(void)peripheral:(CBPeripheral *)peripheral didWriteValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error
    {
         //如果写入成功,再读取一次,进入didUpdateValueForCharacteristic回调,刷新数据
          [self.beaconPeripheral readValueForCharacteristic:characteristic];
    }
    

    相关文章

      网友评论

          本文标题:iOS蓝牙连接读取和写入信息(二)

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