Switching from iBeacon to BLE in real time?

守給你的承諾、 提交于 2019-12-25 01:24:10

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!