0


我想知道如果我的信标接近度是即时的,为什么我没有收到通知——当应用程序处于后台时。在前台模式下,一切正常。

#import "ESTViewController.h"
#import <ESTBeaconManager.h>

@interface ESTViewController () <ESTBeaconManagerDelegate>

@property (nonatomic, strong) ESTBeaconManager* beaconManager;
@property (nonatomic, strong) ESTBeacon* selectedBeacon;
@property (weak, nonatomic) IBOutlet UILabel *outputLabel;

@end

@implementation ESTViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    /////////////////////////////////////////////////////////////
    // setup Estimote beacon manager

    // craete manager instance
    self.beaconManager = [[ESTBeaconManager alloc] init];
    self.beaconManager.delegate = self;
    self.beaconManager.avoidUnknownStateBeacons = YES;

    // create sample region with major value defined
    ESTBeaconRegion* region = [[ESTBeaconRegion alloc] initWithProximityUUID:ESTIMOTE_PROXIMITY_UUID identifier: @"EstimoteSampleRegion" ];

    NSLog(@"TODO: Update the ESTBeaconRegion with your major / minor number and enable background app refresh in the Settings on your device for the NotificationDemo to work correctly.");

    // start looking for estimote beacons in region
    [self.beaconManager startMonitoringForRegion:region];
    [self.beaconManager startRangingBeaconsInRegion:region];

    [self.beaconManager requestStateForRegion:region];

    // setup view
    self.outputLabel.text = @"Seachring for reagion";

}

-(void)beaconManager:(ESTBeaconManager *)manager
   didDetermineState:(CLRegionState)state
           forRegion:(ESTBeaconRegion *)region
{

    // NSLog(@"Region: %@", region);

    if(state == CLRegionStateInside)
    {
        NSLog(@"State: CLRegionStateInside");

    }
    else
    {
        NSLog(@"State: CLRegionOutside");
    }
}

-(void)beaconManager:(ESTBeaconManager *)manager
     didRangeBeacons:(NSArray *)beacons
            inRegion:(ESTBeaconRegion *)region
{
    if([beacons count] > 0)
    {

        self.selectedBeacon = [beacons firstObject]; // get the closest


        NSString* labelText = [NSString stringWithFormat:
                               @"Major: %i, Minor: %i\nRegion: ",
                               [self.selectedBeacon.major unsignedShortValue],
                               [self.selectedBeacon.minor unsignedShortValue]];

        // calculate and set new y position
        switch (self.selectedBeacon.proximity)
        {
            case CLProximityUnknown:
                labelText = [labelText stringByAppendingString: @"Unknown"];
                break;
            case CLProximityImmediate:
                labelText = [labelText stringByAppendingString: @"Immediate"];

                [self fireNotification];

                break;
            case CLProximityNear:
                labelText = [labelText stringByAppendingString: @"Near"];
                break;
            case CLProximityFar:
                labelText = [labelText stringByAppendingString: @"Far"];
                break;

            default:
                break;
        }

        self.outputLabel.text = labelText;
    }
}


-(void)fireNotification {
    NSLog(@"Fire Notification from background");

    // present local notification
    UILocalNotification *notification = [[UILocalNotification alloc] init];
    notification.alertBody = @"You are very close to the beacon";
    notification.soundName = UILocalNotificationDefaultSoundName;

    [[UIApplication sharedApplication] presentLocalNotificationNow:notification];

    // Request a server...
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
4

1 回答 1

4

对于 iOS,iBeacons 的测距通常只在前台工作。检查的代码CLProximityImmediate在一个范围回调方法中。

如果您的应用程序在后台,它只会在进入/退出受监控的 iBeacon 区域后 5 秒内收到测距回调。

这一原则适用于标准 iOS CoreLocation API。当您使用专有的 Estimote SDK 时,同样的限制也适用,因为它是标准 API 的包装器。

于 2014-01-29T18:30:37.720 回答