How to send messages to a connected bluetooth device in iOS in the background - ios

I'm using BLECommander to connect to a bluetooth device in the foreground and my code is heavily based off this sample app. I believe my app would use both bluetooth-central and bluetooth-peripheral. Additionally, I would ping in the background every couple of hours to retrieve any new missing data. I am fine with connecting to a device in the foreground; I simply want to send messages to it in the background and retrieve data.
I have read the apple documentation on using core bluetooth to operate in the background.

If you read the apple documentation, you should know that your options are quite limited on iOS on what you can do in general when your app is in background.
However, there could be ways to achieve what you want. If you have control over the other bluetooth device you can make it change the BLE advertisement from time to time. When you scan on iOS, you will also get a scan result in background (but only once for a certain advertisement!) and can then connect to the device and read/write.
Hope this helps!

Related

ios background ping every minute

I have looked at some posts within this domain (e.g. iOS background fetch custom interval) and I am concluding that what I need to do is not possible.
I have an external device communicating to ios over bluetooth (NFC chip card reader). The external device goes to sleep if not pinged every minute (59 seconds).
When the app is in the foreground I can manage the pinging. When in the background I want to continue to ping. Is this impossible?
The ping operation is fast; it is just to send a message and I dont need a response back to ios.
(What works really well is the opposite, bluetooth communication can flow from the device to ios from the background. That is done via UIBackgroundModes in Info.plist to bluetooth-peripheral.)
Please note that I am feeding the iphone from external power in the application here. It is an industrial application; and the normal iphone usecase is not applicable.
Short answer, no. The best iOS offers is back ground fetch. But there is no guarantee when background delegate method is called. You can specify a timespan as a guide but it primarily based on how often the app is accessed.
https://www.raywenderlich.com/143128/background-modes-tutorial-getting-started

How to Save data when application is in background bluetooth mode?

I know, from documentation, that iOS application can scan for BLE devices when it is in background mode. It can also retrieve data form peripherals devices.
Question
What can I do with this data. My purpose is to save this data (after deserialize of course) to core data. How can I do that?
If this is not possible, what is the recommended way to do it? (NSUserDefaults and after sync with CoreData?)
(iOS10, Swift3)
While in background an app registered to BLE event will be awaken by the system in occurrence of BLE related event that you are registered for. Then the appropriate delegate methods will be called as documented in the official apple documentation.
Events that will awake your app from the BLE are notifications, new peripheral if you are in discovery mode, new connection events (connection/disconnection).
From the moment the app is awaken by the system you have roughly 8 seconds to execute your code and respond to the BLE update. In this time window you can perform any kind of operation you wish, including core data related jobs.
Please consider to read this document released by apple to help developers to develop amazing apps that works with BLE: https://developer.apple.com/library/content/documentation/NetworkingInternetWeb/Conceptual/CoreBluetooth_concepts/CoreBluetoothBackgroundProcessingForIOSApps/PerformingTasksWhileYourAppIsInTheBackground.html

Mi Band 1S vibration on incoming call handling on iOS

SO. Initial data:
Device Xiaomi Mi Band 1 or 1S
Device paired with iPhone
Xiaomi iOS app has option "Vibrate on incoming calls"
How they implement it? It continue works if you reboot device or force quit an app. It is unusual for default BLE background. Any ideas how this app handle events?
For people who vote for closing this question. This is a very interesting issue which require some specific knowledge about bluetooth and background. For example there are three bluetooth background modes: external-accessory for old-school non-ble blueooth, bluetooth-central and bluetooth-peripheral. I have done a lot of experiments about last two. But it does not work as you expected after documentation reading. I have found few questions about this topic here on SO. All of them report about pure predictable for bluetooth background. And after all hours on this issue you see on Mi Band and them official absolutely legal app in App Store. And this App keeps connection or has some other "magic" mechanism to handle calls and initiate reaction on device. At a minimum it is curious. Is it? At a minimum it is useful for developers who works with peripheral devices.
It looks like Apple Notification Center Service (ANCS) integration, not Bluetooth Background + Core Telephony.
The purpose of the Apple Notification Center Service (ANCS) is to give Bluetooth accessories (that connect to iOS devices through a Bluetooth low-energy link) a simple and convenient way to access many kinds of notifications that are generated on iOS devices.
Documentation
CategoryIDIncomingCall is about incoming call

Background Bluetooth with iOS device

I am working on developing a bluetooth peripheral to work with my iOS device. I need to make the iOS app receive data whilst it's in background and process that data as it comes. Looking through Apple's CoreBluetooth framework, I can see how the background execution modes can be used. Now to save power, I want the iOS device to only connect to the bluetooth peripheral at a certain time (without need of user interaction). I've looked through Local Notifications on iOS and it has very limited functionality and don't think it provides what I need.
So is there anyway to wake up an app at 6pm and ask the application to start scanning for bluetooth devices? And then execute other code once device is connected? All this without user interaction.
Any suggestions would be appreciated!
Thanks!
You can't really schedule operations to occur at a specific time in iOS (aside from local notification, which as you said isn't what you need).
You can use background fetch mode to periodically allow your app to check for new data. You can set an interval (although this is only a guideline to iOS, not a strict schedule) for how often your app is woken.
When iOS calls your app delegate performFetchWithCompletionHandler method you can check the current time and decide whether you want to transfer data. If not then you can quickly return UIBackgroundFetchResultNoData. If you do get new data then you can retrieve it before returning UIBackgroundFetchResultNewData

Wake up app from background when wifi conectivity

I need my app to wake up when the phone has wifi conection so it can upload pictures to a server.
I've checked the background modes and the closest mode I could choose is voip, but that wouldnt be right. I know that Google Analytics sends the info about the usage of the app while it has wifi connection, even if it's in the background, so there must be a way to do that.
Do you have any ideas?
Thanks.
From what I have read, it is not possible to do so. If you read through the Apple developer's guide, it indicates that only certain functions are available as background tasks, and this isn't one of them. Check out the section "Declaring Your App’s Supported Background Tasks" in the following document: http://developer.apple.com/library/ios/#documentation/iphone/conceptual/iphoneosprogrammingguide/ManagingYourApplicationsFlow/ManagingYourApplicationsFlow.html
The following was a similar post:
iOS app background upload trigger for wifi connection

Resources