简体   繁体   中英

iOS8 - BLE wake up app

Is it possible to wake up the iOS app at the exact time (or after some delay) and communicate with peripheral? My goal is to set an alarm and communicate with peripheral when it's time to ring. The only solution that I can guessed is to send a remote (push) notification at that time which will wake up the app and then it can communicate with BLE device. But that way is not acceptable because there are no guarantee that the push will be delivered at the exact time or will be delivered at all.
So is it possible to awake the app at the exact time which uses CoreBluetooth?

AFAIK, this is not possible given your restrictions. You can't wake up an app that is backgrounded anytime you want. The only thing remotely close to this is if the BLE peripheral was an iBeacon that the app was region monitoring for, and when you entered the region your app would be awoken with a didEnterRegion event. In your case, it doesn't sound like it's an iBeacon and it sounds like it'll be nearby the entire time.

You simply can't wake up an iOS app exactly when you want to.

You can sent a local notification to trigger at a specific time using the fireDate property. That said, it will only trigger the local notification. You can't kick off background search for a BTLE peripheral as a result of a local (or push) notification. The user would have to launch your app.

A BTLE peripheral can be searched for in the background, but if your app isn't running in the background, that obviously won't work. If your peripheral happens to be an iBeacon, iOS will bring your app to life even if it's not running in the background if you enter that iBeacon's range. You can use a CLBeaconRegion to achieve this effect.

Core Bluetooth now supports background restoration . It takes a little work to get right though. If iOS terminates your app because of memory pressure, it will attempt to restore your app in the background when it detects a connection attempt, including any services and characteristics you were advertising.

Firstly, you need to set UIBackgroundModes in your info.plist file to bluetooth-peripheral

Then you need to give your CBPeripheralManager a unique identifier (I do this in application:didFinishLaunching:withOptions AppDelegate.m ):

 myCentralManager =
        [[CBCentralManager alloc] initWithDelegate:self queue:dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0) options:@{ CBCentralManagerOptionRestoreIdentifierKey:@"myCentralManagerIdentifier" }];

Then you need to implement the peripheral restoration delegate method:

- (void)peripheralManager:(CBPeripheralManager *)peripheral
         willRestoreState:(NSDictionary *)dict
{
    NSArray *services = dict[CBPeripheralManagerRestoredStateServicesKey];

    // Loop through services
    for (CBMutableService *service in services) {

        // Then characteristics
        for (CBCharacteristic *characteristic in service.characteristics) {

            // Then any centrals that were subscribed
            for (CBCentral* central in characteristic.subscribedCentrals) {

            }
        }
    }
}

At this point your CBPerpheralManager is advertising again, so be careful about overwriting the characteristics again if you're saving them into properties.

You can simulate a memory termination by invoking:

kill(getpid(), SIGKILL);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM