Question

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

}
Was it helpful?

Solution

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];

OTHER TIPS

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top