BLE device keep connect with iPhone when application terminate
Asked Answered
C

1

9

I got a problem with BLE on iOS. My case as below: I'm developing application connect to device BLE 4.0. My problem is incoming call function. The scenario of this function is after connected between device BLE and application, when phone is received a phone call, my application will send data to BLE device and make it vibrate and light blinking.

Current I tried 4 case below, but successful in 3 case and the rest case is fail:

  • Case 1:(successful) When app on Foreground --> Incoming call --> App can send vibrate and light blinking message to BLE device

  • Case 2:(successful) When click home and app go to background--> Incoming call --> App can send vibrate and light blinking message to BLE device

  • Case 3:(successful) When click home and app go to background and click sleep mode button --> Incoming call --> App can send vibrate and light blinking message to BLE device

  • Case 4:(FAIL) When click double on home button and termianate app on background (remove app on task manager)--> Incoming call --> App cannot send vibrate and light blinking message to BLE device

Do anyone get same this case before and have any experience, idea for this help?

Thanks for your support!

Confiteor answered 6/9, 2016 at 15:59 Comment(6)
I think you're overstating the problem... Isn't the issue that the app isn't running and is therefore not receiving incoming call alerts rather than that it can't talk to the device?Aeschylus
Thanks for your advance! Do you have any idea for my problem? I though case 4 maybe device BLE and phone need communication directly.Confiteor
I'm facing same problem ? Did you find a solution ? @ConfiteorBiodynamics
did you get the solution for this ??Dosi
If the app is killed, so does the Connection with the Bluetooth device. Hence the App cannot send data to the BLE device.Procreate
@Confiteor I am also having same issue, if you fixed this issue Could you help me to solve this issue.Preclude
H
11

On iOS, there are two ways an app is completely terminated:-

  1. System terminates the app (because for example the app is not used for a long time and the system kills it to save memory).
  2. The user terminates/kills the app.

For the first scenario, you need to add support for State Preservation and Restoration. From Documentation Archive from Apple:-

Because state preservation and restoration is built in to Core Bluetooth, your app can opt in to this feature to ask the system to preserve the state of your app’s central and peripheral managers and to continue performing certain Bluetooth-related tasks on their behalf, even when your app is no longer running. When one of these tasks completes, the system relaunches your app into the background and gives your app the opportunity to restore its state and to handle the event appropriately.

Basically, you need to add code to support this feature as it is not enabled by default. Please have a look at the link below, specifically the section "Adding Support for State Preservation and Restoration":-

Core Bluetooth Background Processing for iOS Apps

For the second scenario, if the user terminates/kills the app, then there is no way for the app to restore or relaunch itself. There is a security and behavioural reason for this in that if there is a bug or an issue and you force killed the app, you wouldn't want your app to somehow relaunch itself, because otherwise you'll end in an infinite loop and you will never be able to fully terminate the buggy app. Please have a look at the following link that further explains this:-

https://www.raywenderlich.com/1395-state-restoration-tutorial-getting-started https://medium.com/arkulpa/ios-stay-connected-to-an-external-ble-device-as-much-as-possible-699d434846d2

This is also mentioned indirectly in the Documentation Archive:-

Apps that support background execution may be relaunched by the system to handle incoming events. If an app is terminated for any reason other than the user force quitting it, the system launches the app when one of the following events happens...

I hope this helps.

Hardihood answered 14/2, 2019 at 16:15 Comment(2)
That's not what he's asking :( He was asking what happens when the user terminates the application. Is there any way we can manage a background service like android does, despite the application being force-quit/terminated by the user.Chur
Oh apologies I misunderstood that part. In that case there's actually no way for the app to somehow relauch/restore from being terminated. There is a security reason for this in that if there is a bug or an issue and you force killed the app, you wouldn't want your app to somehow relaunch itself, because otherwise you'll end in an infinite loop and you will never be able to fully terminate the buggy app. I will update my answer to reflect that.Hardihood

© 2022 - 2024 — McMap. All rights reserved.