How does ibeacon work on background?

2019-09-20 16:10发布

问题:

I want to create a order when someone enters into the iBeacon region on app background,but I have a problem when the app on background.

I know if user open "location" and "bluetooth" and enter into region, app will detect the ibeacon.But after entering into region, user open "bluetooth",the app can't receive the entry notification(sometime work) and can't invoke the function locationManager:(CLLocationManager *)manager didRangeBeacons:(NSArray *)beacons inRegion:(CLBeaconRegion *)region so the order can't be created.

Does anyone have experience on it?

回答1:

For more realistic and accurately get bluetooth state value, override its delegate to controller. Core bluetooth framework will help here to get desire accuracy.

Add <CoreBluetooth/CoreBluetooth.h> framework in project.

Use method

- (void)peripheralManagerDidUpdateState:(CBPeripheralManager *)peripheral;

to check bluetooth is powered on?

Peripheral Manager State will gives you more details about peripheral state and related delegates.

So when ever device has turn on it's bluetooth, above delegate method will be called.

And here you can manually start to check for region updates.

For Exmaple:

_locationManager = [[CLLocationManager alloc] init];
[_locationManager setDelegate:self];

_beaconRegion = [[CLBeaconRegion alloc] initWithProximityUUID:uuid identifier:<Identifier>];


- (void)peripheralManagerDidUpdateState:(CBPeripheralManager *)peripheral {

    if (peripheral.state == CBPeripheralStateConnected) {
        [self.locationManager startRangingBeaconsInRegion:self.beaconRegion];
    }
}

The method startRangingBeaconsInRegion will invoke the method:

- (void)locationManager:(CLLocationManager *)manager didRangeBeacons:(NSArray *)beacons inRegion:(CLBeaconRegion *)region;