ios - CLBeacon : How can I get the distance from the IBeacons?

标签 ios objective-c xcode ibeacon

如何获取 iBeacon 的距离?我能够获取它们的proximity,但如何获取与 CLBeacon 的距离?我使用过 Estimote SDK,它给出了距离值,但我不知道如何使用 CLBeacon 获取它。

 - (void)locationManager:(CLLocationManager *)manager
        didRangeBeacons:(NSArray *)beacons inRegion:(CLBeaconRegion *)region{

    if (self.beaconRegion) {
        if([beacons count] > 0)
        {
            //get closes beacon and find its major
          CLBeacon *beacon = [beacons objectAtIndex:0];
       }
   }
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:nil];

    /*
     * Fill the table with beacon data.
     */
    ESTBeacon *beacon = [self.beaconsArray objectAtIndex:indexPath.row];
    beacon.delegate=self;
    cell.textLabel.text = [NSString stringWithFormat:@"Major: %@, Minor: %@", beacon.major, beacon.minor];
    cell.detailTextLabel.text = [NSString stringWithFormat:@"Distance: %.2f", [beacon.distance floatValue]];
    [beacon connectToBeacon];
    return cell;
}
-(void)beaconConnectionDidSucceeded:(ESTBeacon *)beacon{
    [beacon writeBeaconProximityUUID:@"B9407F30-F5F8-466E-AFF9-25556B57FE6D" withCompletion: ^(NSString *value, NSError *error){
        if(error){
            NSLog(@"Got error here: %@",[error description]);
        }else{
            NSLog(@"Congratulation! you've got sucessfully written UUID in beacon");
        }
    }];

}

最佳答案

精度属性应该给你一个距离:

CLBeacon *beacon = [beacons objectAtIndex:0];
beacon.accuracy

如果它不起作用,请确保调用 startRangingBeaconsInRegion:

[_locationManager startMonitoringForRegion:region];
[_locationManager startRangingBeaconsInRegion:region];

关于ios - CLBeacon : How can I get the distance from the IBeacons?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24035600/

相关文章:

ios - 是否可以下载应用程序商店中不再存在的应用程序的 ipa?

ios - 尝试运行我的应用程序的第二个目标时的 SIGABRT

iphone - NSDate、NTP 和载波时间

ios - 在没有与设备配对的 Apple-ID 的情况下签署应用程序以进行两步验证

ios - 在 Xcode 9(测试版)中将 pod 与 Playgrounds 结合使用

android - 退出 Expo 后无法运行 React Native 应用程序

ios - 快速覆盖属性

ios - 错误 : "Expected selector for Objective-C method" and "Expected method body" on #import <Foundation/Foundation. h>

objective-c - Xcode/LLDB : How to get information about an exception that was just thrown?

ios - 根据 UIPanGestureRecognizer 翻译值更改 UIImageView 中的图像