【问题标题】:How do I get the Signal Strength value from an iBeacon packet on iOS using Objective-C如何使用 Objective-C 从 iOS 上的 iBeacon 数据包中获取信号强度值
【发布时间】:2019-03-18 05:54:26
【问题描述】:

iBeacon 协议将信号强度或测量功率作为数据包的最后一个字节。有没有办法获得这个值?

【问题讨论】:

    标签: ios objective-c ibeacon signal-strength clbeacon


    【解决方案1】:

    不幸的是,iOS 没有提供读取这个值的方法。 CoreLocation 不提供对该字段的访问,CoreBluetooth 阻止访问 iBeacon 广告的原始字节。具有讽刺意味的是,您可以在 MacOS、Android、Windows 和 Linux 设备上读取此字节,但在 iOS 上却不行。

    您可以读取 CLBeacon rssi 属性,该属性为您提供检测到的信号强度。但您可能知道,这与信标数据包内传输的测量功率字节不同,信标数据包会告诉您 1 米处的预期信号强度。

    iOS 不允许访问该字段,这很令人沮丧。

    【讨论】:

      【解决方案2】:

      根据苹果的官方文档,RSSI被认为是信号强度。

      Instance Property
      rssi
      The received signal strength of the beacon, measured in decibels.
      
      Declaration
      @property(readonly, nonatomic) NSInteger rssi;
      

      在Objective-c代码中,需要添加两个header

      #import <CoreLocation/CoreLocation.h>
      #import <CoreBluetooth/CoreBluetooth.h>
      

      在 .m 中你应该添加他们需要的委托:

      CBPeripheralManagerDelegate,
      CLLocationManagerDelegate
      

      那么你应该创建三个对象

      @property(nonatomic, strong)CLBeaconRegion *beacon; //iBeacon device be scaned
      @property(nonatomic, strong)CLLocationManager *locationManager;//location manager
      @property (strong, nonatomic) CBPeripheralManager *peripheralManager;//periphera manager
      

      locationManager 的实例如下:

       _locationManager = [[CLLocationManager alloc] init];
      _locationManager.delegate = self;
      [_locationManager requestWhenInUseAuthorization];//set location be allow when use
      

      beacon 实例化如下:

      _beacon = [[CLBeaconRegion alloc] initWithProximityUUID:[[NSUUID alloc] initWithUUIDString:@"FDA50693-A4E2-4FB1-AFCF-C6EB07647825"] identifier:@"media"];
      //FDA50693-A4E2-4FB1-AFCF-C6EB07647825 this modified be your need scaned device's UUID
      

      peripheralManager 像这样安装:

      self.peripheralManager = [[CBPeripheralManager alloc] initWithDelegate:self queue:nil options:nil];
      

      在viewdidload中,调整定位服务是否ok,然后进行下一步操作:

      BOOL enable = [CLLocationManager locationServicesEnabled];
      if (enable) {
          if ([CLLocationManager isMonitoringAvailableForClass:[CLBeaconRegion class]])
          {
              [self.locationManager requestAlwaysAuthorization];
              [self.locationManager startMonitoringForRegion:_beacon];
              [self.locationManager startRangingBeaconsInRegion:_beacon];
      
          }
      }
      

      当找到 iBeacon 设备时,可以调用这个委托方法:

      //find IBeacon device then scan
      - (void)locationManager:(CLLocationManager *)manager didRangeBeacons:(NSArray*)beacons i    nRegion:(CLBeaconRegion *)region{
      //if not we need found deice then stop scan
      if (![[region.proximityUUID UUIDString]           
      isEqualToString:@"12334566-7173-4889-9579-954995439125"]) {
      [_locationManager stopMonitoringForRegion:region];
      [_locationManager stopRangingBeaconsInRegion:region];
      }
      //print all IBeacon information
      for (CLBeacon *beacon in beacons) {
         NSLog(@"rssi is : %ld", beacon.rssi);// this is signal strength
         NSLog(@"beacon.proximity %ld", beacon.proximity);
      }
      

      }

      这个beacon.rssi是信号强度,希望对你有帮助。

      【讨论】:

      • 感谢您提供的所有信息。我实际上已经实现了你展示的内容。我仍然希望以某种方式将 1m 处的实际测量信号强度暴露在某个地方,因为这是获得准确距离的唯一方法。靠近的 iO 还可以,但不是很好。我知道测量到的信号强度在字节流中,只是希望 Apple 能够公开它,以便我们能够得到它。再次感谢您的回答。
      • 好的,如果要测量的信号强度。希望苹果暴露。
      猜你喜欢
      • 2019-01-06
      • 1970-01-01
      • 2011-12-29
      • 1970-01-01
      • 2015-05-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多