Android Question Track Speed even when the B4XPage/App is swiped in background

Guenter Becker

Active Member
Licensed User
Hello,
yet I am starting a project where to continously track the speed of a car. For example, if the speed is ahead 10km/h the app will capture the current dateTime and if it is below 10km/h as well. The App should do this even if an other App is running in the foreground. Comming back to the App the elapsed time between speed-start and speed-stopp should be computed.

I read the posts about Background Task and I read the posted information around Google Playservice. Also I still remember a comment from Erel that a B4XPage is still working even it is swiped. Now I am a little bit confused what is really needed and what is the best way to go?

I don't need the location only the speed therefore should it good to use GPS or better Device inbuild Accelerator Sensor?
How is the best way to realize the tracking even if the App is in the background including condition to run on Devices with Android 20+?

Thank you in advance for your help.
 

Brian Dean

Well-Known Member
Licensed User
Longtime User
I think that you will have to use GPS. An accelerometer does not measure speed, and although it is theoretically possible to mathematically integrate an accelerometer signal to give velocity it would require a very accurate accelerometer and similarly accurate integration procedures to get the level of continuous tracking that I think your application needs. Anyway, using GPS is going to be much easier.

As to tracking in the background, I am not an expert but I would think that the usually quoted Background Location Tracking is the obvious place to start. I have used this approach myself (for an mp3 player) and it is extraordinary persistent!
 
Upvote 0

emexes

Expert
Licensed User
it would require a very accurate accelerometer
and gyroscope (unless the phone is always oriented in a fixed direction, but we're in a car, so that's pretty unlikely)

Having said that, I did once use the accelerometer(s) as a sanity check on the GPS speed and bearing, as in: if you are measuring a constant 1 g(ravity) then your speed and direction should also be reasonably constant (unchanging).
 
Upvote 0

Guenter Becker

Active Member
Licensed User
Thank you to all of you,
especially the comment from Erel is very helpfull because up to now it was not clear (to me) that a B4XPages App does not need a Background Process because it is still working in the Background. I hope to understand right that this also means GPS Signals a continously evaluated and can be computed even if the App is in Background Mode.
 
Upvote 0

Guenter Becker

Active Member
Licensed User
Her again,
hey Erel now I'm confused. I started the example Project of the FusedLocation Provider comming from the Forum on my Samsung Device. This is a B4XPages Project. Works well. I modified it to save speed values to a list if the speed is above 40 . Than tested it in my car. It works well but if I switch to another App I got the Message "Activity Paused but not stopped" and no Location/Speed is captured until I closed the other App and return to the FLP Project. Doing so I got the Message "Activity resumed" and location capture moves on.

I think that I did not understand your comment well. It is looking that I need a special foreground process to realize a background capture of the location/speed as you have described it in your Background Process Tutorial. Is that correct?
 
Upvote 0

emexes

Expert
Licensed User
I think that I did not understand your comment well. It is looking that I need a special foreground process to realize a background capture of the location/speed as you have described it in your Background Process Tutorial. Is that correct?

You've probably already spotted these, but just in case... something to chew on until Erel is back from lunch break:

https://www.b4x.com/android/forum/threads/b4xpages-gps-background-process.127817/

https://www.b4x.com/android/forum/threads/background-location-tracking.99873/
 
Upvote 0

emexes

Expert
Licensed User
yet I am starting a project where to continously track the speed of a car.

Continuously as in 100% coverage by GPS speed reading?

This makes it sound as if you just need to know when the vehicle starts and stops moving:

For example, if the speed is ahead 10km/h the app will capture the current dateTime and if it is below 10km/h as well.

in which case, if the phone has (3 x) accelerometers, then you maybe you can use those to detect when the phone starts moving, and then when that happens, switch on the GPS, which might take 30 seconds to get an up-to-date fix, but:

presumably you know the location of when you were stopped, and you know that you started moving 30 seconds ago and where you are now, and (average) speed = distance / time

would that be good enough for your purpose?

or for slightly better accurace: you know you started at 0 speed, and in 30 seconds you've travelled x metres and are up to y speed, so you could assume constant or typical acceleration, and work it out based on that, eg say:

from a standing start 30 seconds ago, our first GPS reading is that we've travelled 200m and are now at 60 km/h

a truck accelerates say 80 km/h in 20 seconds = 4 km/h second, thus 15 seconds of acceleration time x average 30 km/h = 125 metres and the other 75 metres... hang on, don't need to do all this, if after 30 seconds we are at 60 km/h then you could interpolate that after 5 seconds we were at your 10 km/h stationary/moving threshold, and presumably within a couple of seconds is close enough.
 
Last edited:
Upvote 0

emexes

Expert
Licensed User
Sorry, got a bit sidetracked. Where I was heading is that GPS is a major battery drain and something that Android would be keen to discourage use of.

So perhaps using accelerometers to keep GPS switched off until it's actually needed, might help you fly under the radar of Android's power-conservation strategy.
 
Upvote 0
Top