Is there any way to "wake-up" (relaunch) an iOS application (a background task actually, not to show the application) whenver a bluetooth device is connected?
For example, the iOS OS kills the application due to memory lack. Then a couple of hours later the user connects his bluetooth device. Can the iOS notify my application that the bluetooth device is connected and run a background task?
In Android I see that I can create a BroadcastReceiver that is registered for when a bluetooth device is connected. I need something equivalent to that for iOS.
If you have a pending connect for your BLE peripheral and you have specified BLE central background mode then yes, your app will be relaunched. You also need to implement state preservation and restoration as described in the Core Bluetooth programming guide in the event that your app is actually killed (not just in the background).
Note that in iOS a user cannot connect to a BLE peripheral directly - your app is responsible for identifying the target BLE peripheral and issuing the connect command. This is described in the programming guide.
Related
I was searching for my Bluetooth problem a few days but I did not find any useful solution. I would like reading advertising data more precisely manufacturing data from peripheral devices via ios application when it is in the background. In my case, the peripheral devices are Nordic nrf51822 chips and a central device is ios phone. I have successfully implemented the Bluetooth background mode and it works well on the ios device. The Event handler DiscoveredPeripheral is called when the new peripheral device is discovered. But the problem is that it is called only first-time the peripheral device becomes visible. If I turn off and on the peripheral device the DiscoverdPeripheral event is not called anymore. I know that this is ios restriction, but I do not know how can I handle this ios limitation. In the end, I would like that app in the background starts scanning for peripheral devices with specific UUID periodically, for example, every 10minutes. At this point, I would like to ask if this scenario is possible to implement in ios if so I appreciate for any help.
The Event handler DiscoveredPeripheral is called when the new peripheral device is discovered.
This isn't correct. It will only be called once per call to scanForPeripherals. When the device connects, you should stop scanning. When the device disconnects, which you should see as centralManager(_:didDisconnectPeripheral:error:), you can begin scanning again.
Even better, if you only want to reconnect to the same device, don't scan at all. Just call connect for that peripheral UUID. It will continue running while you're in the background and wake you up when the connect succeeds.
To push this further, implement Bluetooth State Restoration, which will allow you to automatically reconnect even after reboots.
Currently, I'm testing Bluetooth in peripheral mode on iOS. It manages to work in background, but if user uses other applications and system decides to dealloc bluetooth application, app is not relaunched when it needs to react to new bluetooth events.
Any idea how to make app respond to bluetooth events in this scenario?
Tested on iOS 11.1.1
Prototype apps (with replication steps) to test bluetooth in peripheral modes.
I'm trying to achieve the following scenario:
Open the app and connect to a BLE Peripheral. Working
Send the app to the background and keep the connection alive. Working
Turn off the BLE Peripheral and the app is notified of the disconnection. Working
Turn on the BLE Peripheral and the app is notified of the disconnection. Working
Turn off again the BLE Peripheral. Working
Wait 2 days with the app in background and turn on the BLE Peripheral, but the app does nothing. NOT Working
How can I achieve this last step? iOS kills the background apps when needed, but is there a way of getting notified when a previously-connected peripheral is discovered? I read something about CB State Restoration but I don't really know if this will solve this situation.
Core Bluetooth state preservation / restoration is exactly what you want. If your app is suspended, it will start you app if a peripheral connects / disconnects or a notification arrives. It is however broken by design in the way that your app will stop being woken up if Bluetooth is restarted for any reason. Examples are Bluetooth crashes (happens every now and then), if the user toggles Bluetooth or flight mode, or if the phone is restarted.
I am planning to develop an iOS application using CoreBluetooth framework which monitors a pedometer peripheral continuously and counts the footsteps.
I know that if backgroud execution mode is set to BLE Central, the application will continue to receive BLE events even in the background.
Apple documentation states that in case the app gets terminated due to low memory, the system can keep track of BLE events for a particular Central Manager if state preservation and restoration is adopted.
Assume I have an iOS application that operates in Central mode. The app is subscribed to receive notification from a Pedometer when ever the footstep characteristic changes.
I have adopted the following in my app.
BLE Central background mode
BLE State preservation/restoration for Central Manager
I start my app, Scan, Pair and Connect to the pedometer and the app starts receiving footsteps.
My Questions:
Now if the iPhone reboots, Will I continue to receive BLE events so that my app will be launched in the background without the user having to manually launch the application again and connect to the pedometer?
If the app is terminated by the user explicitly using the multitasking gesture, Will the app be able to receive BLE events without the user having to manually launch the application again and connect to the pedometer?
Is there a way to launch my application on iOS boot up?
Now if the iPhone reboots, Will I continue to receive BLE events so that my app will be launched in the background without the user having to manually launch the application again and connect to the pedometer?
Your app won't receive BLE events because all apps are started in the terminated state after a reboot (despite remaining in the app switcher). The user will have to manually launch your app at least once after reboot in order for it to use BLE.
If the app is terminated by the user explicitly using the multitasking gesture, Will the app be able to receive BLE events without the user having to manually launch the application again and connect to the pedometer?
Same as above, the app has entered the terminated state, so it won't be able to communicate using BLE until it is explicitly launched again.
Is there a way to launch my application on iOS boot up?
iOS doesn't provide any way to launch your application on boot.
The one exception to all of this that I can find is iBeacons. If your app registers to receive updates for a specific iBeacon, iOS will launch your app when it finds it (even after a reboot or if the user explicitly kills it from the switcher). When you get the iBeacon callback you can start all your BLE logic and it will then run in the background per usual. Of course this means you need to advertise as an iBeacon on your pedometer, which may or may not be feasible.
Bear in mind the detection of the iBeacon is pretty finicky, especially after a reboot. You have little guarantee as to how fast or even if you will be delivered the iBeacon callback to start your app. But it's something.
Source
I am trying to make an Application using iBeacon in which One device will work as beacon Broadcaster and another one as Receiver.I gone through the similar thing in Run iPhone as an iBeacon in the background but the solution that present there is not for API of iBeacon so i am asking this thing again.
I succeed in being notified on Detector Device(receiver) even the App is in Background or in Quit mode. but Whenever Broadcaster Device's App goes in Background it stops Broadcasting packets and become no more as beacon. When it comes back in Foreground it again works fine. So the issue is Broadcaster has to be always in Foreground to Act as Advertiser or Broadcaster.
Is there anyway to advertise/Broadcast packets of iBeacon's even when App is in background mode.
I have enabled capabilities "Location Update" and "Acts as a Bluetooth LE accessory" from xCode.
Unfortunately, it is simply not possible to advertise as an iBeacon in the background on iOS. For an explanation as to why, see here: Can we start iBeacon transmitter in background?
Apple doesn't authorize it, it would be a mean to track an user which is quite against Apple current policy to preserve users' privacy.
The capabilities "Acts as a Bluetooth LE accessory" is to enable your application to continue being used as a bluetooth device (with a connection) when the app go in background mode. Advertising isn't a connected usage of bluetooth and isn't concerned by this capabilities.