This is a question regarding Android Beacon Library.
Because of Android 8 (Oreo)'s restriction on background service, the author of the library has changed the mechanism of starting background service in Android 8.
As far as I can observe, the background service needs a much longer time to start than Android 7 or below, even if the app is already started.
What I want to do is to reduce this time as much as possible.
One of the way is to force launch the background scanning if the app is opened by the user.
For instance, I want to do something like
BeaconManager.getInstanceForApplication(this).startbackgroundScanningIfNotRunning();
In onCreate() of MainActivity.
But I think there is no such API. Is there anyway to do this?
Or, is it possible for the library to handle itself (through an app update)?
While it is possible to do something like described in the question, it may not be useful. The problem is that Android 8 prevents apps from running in the background long-term. In order to manually start a scan in the background, your app must be running in the first place. If Android 8 has disallowed this, then you aren't going to be able to do it, anyway.
For an explanation of how Android 8 blocks apps from running in the background and how the library works around this, you can read this blog post if you have not already:
http://www.davidgyoungtech.com/2017/08/07/beacon-detection-with-android-8
If you have somehow managed to get your app running long-term in the background (e.g. by using a foreground service), you can always kick off a scan manually by starting beacon ranging/monitoring in foreground mode (from the library perspective), which will schedule an immediate ScanJob that will keep running and start itself over and over. To make sure you are in foreground mode, you simply need to refrain from using the BackgroundPowerSaver and don't make any calls to beaconManager.setBackgroundMode(...) to true.
Related
I'm trying to create an app that manages a BLE connection in the background (receives notifications, reconnects and subscribes to a characteristic when connection is lost and the phone is moved near the BLE device again).
Right now I have the app somewhat built - everything that I need works relatively well but only when the app is open. Although the code isn't up to standard as this is my first app and I don't have any previous experience with Java/Kotlin/Dart (this is also why I'd rather not touch anything outside of flutter).
I need the app to send me local push notifications when tabbed out of the app or when screen lock is on and as far as I understand one could do this with isolates. I've searched a lot but found one guide which is kind of hard to understand as a beginner and also uses Kotlin.
Can I get away with only dart code for background code execution (described above) and is it suitable for a beginner?
Also, let me know if you have any alternative solutions since all I need is to get a notification on my phone whenever it receives a notification from a BLE device (and also auto connecting). I've already tried this with kivy since I'm more familiar with python but with no luck.
Talking about BLE there's this plugin by the way: https://pub.dev/packages/flutter_reactive_ble
As for the isolates, they are not ideal for this even though they do run separately, they'll get killed eventually. There's also a little helper package that makes working with isolates very straightforward if you want to try it: https://pub.dev/packages/computer
And so actually for the task at hand (background execution) I would suggest researching/using this one: https://pub.dev/packages/background_fetch
I would not encourage you to use any timer-based solution that tries to wake up your phone every 15 minutes or so, since that uses unnecessary CPU time.
Now I'm not that much into Flutter, but at least on Android, the "correct" way to have an ongoing connection (auto-connect) is to have a Foreground Service in the process running, so the process does not get killed. Then subscribe to characteristic notifications as usual, and handle the values as they arrive, either in Java or Flutter (by using some Java <-> Dart bridge).
From the 2018 Google IO session Migrate your existing app to target Android Oreo and above, they mention that there is an exception for not being able to start services in the background when it comes to a response from notification actions. What exactly does this exception mean?
We are currently using a WakefulBroadcastReceiver to receive the action, which then generally starts a Service to run a network request and take action on result. I want to make sure that we are making the proper changes necessary (likely, using a foreground Service, if it is necessary. That being said, I don't want to make changes if they are not needed.
Does anyone know what the exception to not being able to start services while in the background actually means here and if there are any exceptions that may break this model (being in battery saver mode, for instance)?
In my testing on a Pixel 2 running Android P I have had no issues with this, but I want to make sure my users will not either.
for my current project I try to figure out the differences between Android and iOS. I only have knowledge in Android and absolutely no idea about iOS.
What I want to know is:
Is there something similar like Intents for iOS? Especially those which indicate changes in Wifi / BT connection like android.bluetooth.device.action.ACL_CONNECTED or android.net.wifi.STATE_CHANGE? Or is there another method to find out about connection changes even if the app is not running / in background mode?
As I understand from
IOS background service (like in Android) enable all time & https://developer.apple.com/library/ios/documentation/iphone/conceptual/iphoneosprogrammingguide/ManagingYourApplicationsFlow/ManagingYourApplicationsFlow.html, having a background service in iOS is only allowed for specific types of apps. So an app which asks for sensor values (like accelerator) on regular basis is not allowed - is this correct?
I am very thankful for answers and also further literature regarding these quesiton!
You can use an implementation of Reachability to get the notifications about Wifi connectivity, but keep in mind these won't wake up your app.
From Apple
From Cocoapods
You can use Core Bluetooth to look for connectivity events. Again these won't wake up your app. I believe you can setup a delegate to a CBCentralManager to find out about that. Check out the docs here.
However, you are correct in saying that you still need to solve the issue of background execution to keep your app awake. For that you need features that actually make background execution useful to a user or Apple won't approve your app. Here are some of your options.
If your app has actual bluetooth features you can use one of those modes (bluetooth-central and bluetooth-peripheral).
If you have a feature that warrants background audio you can use this
If you have a feature that warrants background location you can use CLLocationManager startUpdatingLocation (but this would eat up some serious battery)
You might also be able to set up a system that spams silent remote notifications and then use the remote-notification background mode. This is meant for downloading content
Also keep in mind that a user can basically disable all of these things on you at any time.
Good luck!
Im not sure if this will answer your question directly but it may be helpful. I know in Android that you can you an Intent to switch Activities. Well in iOS in order to switch to another UIViewController (iOS equivalent of activity) you would perform a segue. In prepareForSegue method you can handle what you want to do in the next UIViewController, such as passing variables etc.
You can use Background Fetch in iOS7 you can perform services while the app is asleep/in the background. This wakes the app at regular intervals in the background to perform a task, like refreshing data etc. You may be able to record the accelerometer values here. http://www.appcoda.com/ios7-background-fetch-programming/ has a good tutorial on this.
I hope this is somewhat helpful.
I hope this question meets the criteria for the site. I'll happily update otherwise.
I am, like many others, developing an application for multiple platforms using the PhoneGap (in this case Cordova 3) framework and want to keep my application alive while the use performs others tasks. I have read around this subject and seem many opinions and possible/partial solutions.
Notionally, what I want to do is allow the user to return to the application had they pressed back, home, they got a phone call, they played tetris or whatever and specifically if the app wants them back under certain conditions.
I am presently tackling this by concentrating on the Android platform and will expand out after this solved. I understand the mechanism by which Android OS will kill processes that are not visible and understand and have implemented an example background service.
I employ JQuery Mobile with collapsible content for my application.
Before I continue development, I would like to ensure I am on the right path.
I have successfully implemented pause/resume to handle phone locking and sleep. My application does not die and I can make it beep and vibrate through appropriate plugins.
My continued intention is this ...
Write a background service to perform periodic "checks" in this case position of phone relative to a known position. The intention is to inform the user when they have reached certain proximity. This would be started on the "pause" event and stopped on the "resume" or of the app is restarted.
Using Local Storage, maintain a record of state of the UI so it can be restarted in the correct state. Upon restart, the stored state will be restored and the app will look like it never went away. There is nothing in the app which continuously updates, like graphics or logs, so there is no reason to retrieve "past states" from the background service and perform a rendering of those as if they had been performed by the app, but that would not seem unreasonable should that be required on the future.
As it's possible that the app will not be running, it seems necessary for the service to create a ("something important has happened") notification that the user can click on in order to restart the application. I have found a couple of status bar notification plugins and intend to use one. I trust I will be able to beep/vibrate accordingly from the background service alo.
In addition, a "stuff is generally happening" continuous notification could be used to restart the app. I see no conflict with the above. It would be nice to find a "bring app to foreground is it's running" method, but I'm holding out no hope.
My essential question is this ...
Am I approaching this problem correctly and maintaining as much cross platform work as possible whilst not setting myself up for a fall when it comes to implementing the iOS facet of the solution?
I just don't want to miss a trick I've not located in the sea of answers to what seems like a common problem.
I want to check the database in my app every 12 hrs for rows with date column having corresponding date. This is accomplished by writing service in android. But is there any equivalent of services in iOS so that my requirement can be accomplished?
No. There is no such thing in the SDK or in iPhone/iPad in general. You can only write code that will affect the eco system of the app, not the operating system.
When your app is closed it's closed and no action will be taken until the user opens it/opens a push notification related to your app.
If the user approved location based services for your app, there are a few ways to run short background process even if your app is totally closed. One of them is by using Monitoring Shape-Based Regions which basically means if the user left region X/entered region Y
open the app and run a few commands before closing it again.
The clever way (and the only way I can think of) to accomplish what you're after in iOS is to run that service on a server and pull the data from the server when the app is opened.
In iOS7 and later you can use background fetch for this task.
You can check this tutorial:
http://code.tutsplus.com/tutorials/ios-7-sdk-working-with-background-fetch--mobile-20520
iOS Background work
Nowadays you you are able to use Background fetch or Background Processing Task or URLSessionDownload/UploadTask for doing something when app is in background more
[Xcode Background Modes]
[Background session]
You can find the solution here. Background Execution does this.
http://developer.apple.com/library/ios/#documentation/iphone/conceptual/iphoneosprogrammingguide/ManagingYourApplicationsFlow/ManagingYourApplicationsFlow.html
Added: Apple does not allow apps to run in background for all the time. It provides some finite-length time to complete your app execution. You can increase that time depending on your execution need. But that is not recommended.