Switching from iBeacon to BLE in real time?

2019-08-28 07:36发布

Our beacon hardware was programmed to be iBeacon + writeable BLE . So i can discover it with iBeacon , and also connect to it via BLE.

I would like to scan for iBeacon, and when i enter the region (app is in background) , and the delegate is being called, he will than switch to BLE and connect to the device with BLE :

- (void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region {
    // here connect as usual via bluetooth
}

So,as i said, i can separately write/discover the hardware, but when i am trying to connect to the module, from the delegate via BLE ( when app in background,i enter the region,delegate is ON, than in background trying to connect with BLE, its not working, although i can in background do other things when inside a region, for example, connect to a server .)

Is there anything i can do to make it happen? i was also trying to stop the beacon region monitoring, but even so, he can't connect to the BLE from background .

1条回答
The star\"
2楼-- · 2019-08-28 08:29

to do stuff in bg you need to request time from the os. you then get an identifier.

//identifier
@property(nonatomic, assign) UIBackgroundTaskIdentifier backgroundTaskIdentifier;

request it by calling:

if(!self.backgroundTaskIdentifier) {
    self.backgroundTaskIdentifier = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
        NSLog(@"expired, fail, throw!!!");
        @throw [NSException exceptionWithName:@"BeaconServiceKilled" reason:@"Expired in bg" userInfo:nil];
    }];
}

when done then release it

if(self.backgroundTaskIdentifier) {
    [[UIApplication sharedApplication] endBackgroundTask:self.backgroundTaskIdentifier];
    self.backgroundTaskIdentifier = 0;
}
查看更多
登录 后发表回答