How does ibeacon work on background?

2019-09-20 15:56发布

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条回答
一夜七次
2楼-- · 2019-09-20 16:58

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;
查看更多
登录 后发表回答