简体   繁体   中英

iOS: Can a BLE device (not an iBeacon) wake up my app?

是否可以将常规蓝牙低功耗设备(不是iBeacon!)与我的iPhone应用程序相关联,这样当设备发送数据时,我的应用程序会被iPhone唤醒,即使iPhone被锁定且应用程序被终止(甚至没有在后台)?

As long as your app specifies Bluetooth Central background mode then it will be woken if

  • Your app has a current connection to the device and it sends data (ie the device is in range and it notifies or indicates on a characteristic)
  • Your app has a pending connection to the device and it comes into range(ie the device was out of range, but you have called connect to automatically reconnect when it comes into range)
  • Your app was scanning for specific service types and a device advertising one of these service types comes into range

The case where you app is terminated is slightly different. For these scenarios to work in this case your app must implement state preservation and restoration

Core Bluetooth supports state preservation and restoration for apps that implement the central role, peripheral role, or both.

When your app implements the central role and adds support for state preservation and restoration, the system saves the state of your central manager object when the system is about to terminate your app to free up memory (if your app has multiple central managers, you can choose which ones you want the system to keep track of). In particular, for a given CBCentralManager object, the system keeps track of:

  • The services the central manager was scanning for (and any scan options specified when the scan started)
  • The peripherals the central manager was trying to connect to or had already connected to
  • The characteristics the central manager was subscribed to

The Apple guide talks about the situation where your App is terminated due to memory pressure. It doesn't specify what happens if the app is terminated by the user "swiping up" in the app switcher - In many cases iOS takes this as an indication that the user doesn't want the app to run at all and won't restore it in this case.

There are two states for a bluetooth device to interact with your app:

  1. It has never interacted with your app before
  2. It has already connected to the use's device once and to the app once

    In either case, an iBeacon device will be able to interact with your app.

If the device hasn't connected with your app before, I'm not entirely certain if there is a way to make it work. I have tried and failed to get it to wake up the app.

However, if the bluetooth device has connected before, then you can use CBCentralManager and its delegate methods to communicate between the device and your app.

Core Bluetooth should wake up your app from the OS if registered. Once awake, it's running in the background like normal. You have up to 3 minutes to perform whatever tasks you need to.

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