Core Bluetooth and backgrounding: Detection of a device and triggering an action, even after being days in background mode?

后端 未结 3 1358
猫巷女王i
猫巷女王i 2021-01-30 02:38

I\'ve written an app that needs to get informed when a certain Bluetooth Low Energy device comes within range. If the BLE device gets noticed my app just stores a timestamp.

相关标签:
3条回答
  • 2021-01-30 03:06

    As of iOS 7, your use case is now easy to support. Before iOS 7, your application could register for notifications about that peripheral, and it would be woken up in the background when the system had notification to deliver. However, if the system came under memory pressure while your app was backgrounded, or was rebooted, it wouldn't be relaunched. iOS 7 added state restoration to CBCentralManager and CBPeripheralManager, so now the OS will relaunch your application in a limited capacity even if it wasn't running due to either of the aforementioned conditions. See the CoreBluetooth guide for more info.

    In short, for your use case, you could do the following:

    • Continue to support bluetooth-central as a background execution mode.
    • Opt into state preservation and restoration, as documented here under "Adding Support for State Preservation and Restoration".
    0 讨论(0)
  • 2021-01-30 03:16

    No, iOS gives no guarantee that your app keeps alive in the background. The docs say:

    However, this method may be called in situations where the application is running in the background (not suspended) and the system needs to terminate it for some reason.

    (Documentation of applicationWillTerminate)

    0 讨论(0)
  • 2021-01-30 03:19

    Use IOS7 BLE State Preservation & Restoration

    If your app is terminated by IOS, due to memory pressure(this is why your app can't work after days), it can't handle bluetooth delegates anymore. In this case, if you used State Preservation & Restoration, your app can be relaunched to background to run again, also for only 10s. After 10s, it would move to suspended state. Only in this situation, CBCentralManager's willRestoreState can be triggered.

    Good luck.

    0 讨论(0)
提交回复
热议问题