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

霸气de小男生 提交于 2019-12-22 13:44:11

问题


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.

 - (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");
        }
    }];

}

回答1:


Accuracy property should give you a distance:

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

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

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



回答2:


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.

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

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

    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").



来源:https://stackoverflow.com/questions/24035600/clbeacon-how-can-i-get-the-distance-from-the-ibeacons

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!