Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

}
like image 389
Sunny Shah Avatar asked Jan 10 '23 11:01

Sunny Shah


1 Answers

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];
like image 147
Greg Avatar answered Jan 20 '23 10:01

Greg