I am developing the app where app can behave as both Central and Peripheral in foreground and background. That is working fine by using state preservation and restoration.
Now the next requirement is like both app (Central & Peripheral) are nearby but should connect automatically only when there is push notification fired from backend. So as soon as push notification arrive, app should wake up, Start advertising as Peripheral and Central look for peripheral with fixed service UUID.
I tried to initialise both peripheral and central role on app wakeup by push notification but it never get connect with each other and exchange data.
So question is how can i achieve this waking up app by Push notification and then connect with other iphone's peripheral and other iphone's central to current iphone and exchange data.
Here i am trying to wake up app when notification arrives :
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any],fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
localPeripheralManager = PeripheralManager(delegate: self)
localCentralmanager = CentrallManager(delegate: self)
completionHandler(UIBackgroundFetchResult.newData)
}
There will be a couple roadblocks for this design. Although you may observe this work in your testing, especially when testing attached to Xcode, but in real life you will find both the wake-up method and the Bluetooth connection method will not work as reliably as you might be expecting.
First is the the issue of having your app wake-up on demand. Wake up of apps using content-available pushes are heavily throttled. You can expect 1-2 wakeup per hour as a best case scenario in the hands of your users. Actual throttling will depend on the state of the device, how many push wake-ups have been performed on the device, data use, battery state, etc. In either case, this cannot be assumed to be a reliable wake-up on demand mechanism for an app.
Secondly, Bluetooth connections are going to be difficult to establish when both apps are in the background. On the peripheral side, in general, advertising is done on a best effort basis. As Bluetooth is a shared resource, when other apps and system resources need to use Bluetooth, advertising for apps in the background will slow down, and may even stop for short periods of time. The rate of advertising is directly related to the probability and time required to be discovered by a scanning device.
On the central side, the scan rate also drops once the app is in the background. Scanning rate is also directly related to a timely discovery. Also, both these rates will further slow down when the phone screen goes off, and even further down after a while when the phone goes to sleep mode.
Unfortunately, all these restrictions and slowdowns end up making the discovery process very slow, to the point of becoming unusable. It could potentially take minutes, if not longer. So, if your use case is that the two devices detect each other promptly with your app on demand (assuming the push notification woke it up) it will probably not going to work as you expect it did.