xcode CLBeacon:我如何获得与 IBeacons 的距离?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24035600/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 04:59:51  来源:igfitidea点击:

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

iosobjective-cxcodeibeacon

提问by Sunny Shah

How can I get the distance from the iBeacons? I am able to get theirproximity, but how can I get the distance from the CLBeacon? I have worked with Estimote SDK, which gives the distance value, but I don't know how I can get it with the CLBeacon.

如何获得与 iBeacons 的距离?我能够得到他们的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");
        }
    }];

}

回答by Greg

Accuracy property should give you a distance:

Accuracy 属性应该给你一个距离:

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

If it doesn't work make sure you call startRangingBeaconsInRegion:

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

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

回答by Anticro

The accuracy property is Apple's estimation, how exact your own calculation (based on the beacon's TX and RSSI) can be. If it is "0.1" this means, your calculated distance is probably not farther away than 10cm. But if the accuracy says "1.5" and your distance calculation results to 3m, this means, the beacon may be in a range of 1.5 to 4.5 meters.

精度属性是 Apple 的估计,您自己的计算(基于信标的 TX 和 RSSI)的精确度。如果它是“0.1”,这意味着,您计算出的距离可能不会超过 10 厘米。但是,如果精度为“1.5”并且您的距离计算结果为 3m,这意味着信标可能在 1.5 到 4.5 米的范围内。

My guess: The accuracy property measures the current RSSI and is calculated by the difference from the average RSSI of former discoveries.

我的猜测:accuracy 属性衡量当前的RSSI,并通过与以前发现的平均RSSI 的差异来计算。

To answer your question: Can you read Java? I don't have the C code at hand right now...

回答你的问题:你会读Java吗?我现在手头没有 C 代码...

    final double ratio_dB = txPower - rssi;
    final double ratio_linear = Math.pow(10, (ratio_dB / 10));
    final double r = Math.sqrt(ratio_linear);

r is the estimated distance in meters. txPower is the beacon's Tx-Power (with AltBeacons, this is the payload's byte one after the 20 bytes "Beacon ID").

r 是以米为单位的估计距离。txPower 是信标的 Tx-Power(对于 AltBeacons,这是 20 字节“信标 ID”之后的第一个有效载荷字节)。