How to trigger Core Bluetooth state preservation and restoration

本小妞迷上赌 提交于 2019-11-29 02:25:55

NB Thanks to user1785784 for sharing Apple's QA1962 - Conditions Under Which Bluetooth State Restoration Will Relaunch An App which describes new Bluetooth behaviour in iOS 11. This document should be considered the answer to this question, although I think it incorrectly claims that iOS 10 would relaunch an app that has been force quit. (I haven't tested that on an iOS 10 device, but it would have been a departure from iOS 9. Can anyone confirm?).

Killing the app manually (B) from the task switcher, ensures your app will not be launched automatically until the user explicitly opens it again.

C doesn't work either, I think only VOIP apps are launched automatically after restart, and then only after the device is unlocked.

I don't know any D.

I use A.

First, to implement Bluetooth State Restoration, make sure you've

  1. added bluetooth-central as a UIBackgroundModes to your Info.plist
  2. set a CBCentralManagerOptionRestoreIdentifierKey when initing your CBCentralManager
  3. implemented the -(void)centralManager:willRestoreState: callback in your CBCentralManager delegate.

Then you're ready to test state restoration:

  1. get the app to some known state (say bluetooth powered on, some device connected/connecting)
  2. kill the app in Xcode
  3. watch the logs or set a launch breakpoint
  4. make a change in the bluetooth state, e.g. by
    • toggling airline mode
    • taking a bluetooth device out of range (to avoid walking, I put mine in a conductor/Faraday Cage/coffee pot)
    • bring the device back into range
    • interacting with device, e.g. by pressing a button/having a pulse
  5. watch your state restoration code be called

NB: application:didFinishLaunchingWithOptions: will be called first, and you must immediately init your CBCentralManager as described above. Then centralManager:willRestoreState: will be called.

Just figured this out recently with the help of Apple Tech. Also given/have a nice link that shows the different ways to cause the app to restart without user intervention.

I did it by causing the app to crash suddenly with the following snippet of swift code. This causes the app to restart and the call the 'willRestoreState' callback.

DispatchQueue.main.asyncAfter(deadline: .now() + 5)
        {
            print("Killing app")
            // CRASH
            if ([0][1] == 1){
                exit(0)
            }
            exit(1)
        }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!