问题
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:
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;
}
来源:https://stackoverflow.com/questions/27105765/switching-from-ibeacon-to-ble-in-real-time