How to determine the velocity of an Android device? - android

What is the easiest way to find out how fast the Android device is traveling?
Also, is there a way to register an intent for speed? Example: intent if the device goes more than 20 miles an hour.

The easiest way is to get the speed of the device is from the GPS, more specifically the time it takes to travel between 2 points. Luckily this is already done for you with the Location class: Location.getSpeed() which returns a float with the speed in m/s.
See the Android developer reference for more info

you can calculate speed precisely by multiplying acceleration to time it was applied for. this method is very sensitive so you will have to add some border check to filter shakes.

Related

Android.Location vs. Accelerometer

i'm looking for the best way to implement a road navigator that gets the velocity, distance between 2 points using the smartphones techniques.
through my searches i found 2 different tech. using either the accelerometer or android API (android.Location).
some opinions said that using accelerometer wont give me an accurate results because there will be so much noises as a bumpy roads, buildings...etc and calculations will be so complex.
on the other hand using the Android API (android.Location) means i should always be connected to the GPS, doesn't that affect on the battery?!! and as i found i can't open the GPS by myself i used always ask the user for a permission(so boring). and is there an limitation
So kindly help to take a decision, is it useful to be connected 24 hours on GPS in order to update the device location.. or to use the accelerometer??
You definitely should use GPS. The battery will be strained but you get much more accurate result. Sensor should be used only when GPS is not available and for a short distance.
Using GPS you can call location.hasSpeed() and if positive call location.getSpeed() together with getBearing() will give you the velocity.

A "speedometer" that doesn't use GPS

How to calculate speed without requiring GPS? I have heard of something called "sensor technology" but am not sure what that means. I am asking because I want to make an app, but I don't think it is battery efficient to use GPS, plus it is for a long-term project so I want the app to continuously run without having to manually turn it on or turn on GPS.
GPS is by far the most accurate way to do this.
You can get access to the accelerometers, but they are not remotely accurate enough for this type of application.
The only other way I can think of would be to get one of those bluetooth adapters that you plug into your car's diagnostic port, and get the speedometer input from there. Then, you're wasting power on bluetooth as well.
Just buy a power adapter for your phone to run on the vehicle.
I tried to measure the speed using Android's accelerometer sensor for an indoor-navigation project but that failed miserably. I intented to derive the current speed from the acceleration/deceleration over time and so in the end calculate movements along the X, Y and Z-axis, but that didn't work out at all. The accelerometer sensors of common Android devices are way too inaccurate for that kind of usage.
So, you will have to use GPS. To reduce battery drain, you can increase the intervals (time and/or distance) in which the LocationManager notifies your App. See the minTime and minDistance parameters of it's requestLocationUpdates() method.
Android allows you to use cellular phone antennas and wi-fi access point antennas as reference points to determine your current location without using the GPS (see: locationManager.NETWORK_PROVIDER).
Once determined your position in two different moments/points you can easily calculate your speed in your code.
I don't think one can calculate speed without the assistance of GPS. Speed depends on distance and time. I am not sure if we can calculate distance just like that.
Anyway look at http://developer.android.com/reference/android/hardware/Sensor.html
There we have different sensor types. Perhaps you can make use of one of those.
I think what you are referring to would be considered "dead reckoning" (http://en.wikipedia.org/wiki/Dead_reckoning) and is possible, but will lead to drift and incorrect speed information, especially if the user of your app doesn't have the device mounted securely to whatever is moving.
Would it be a valid use case for your app to go to sleep and turn off the GPS? You might be able to devise a way to turn the GPS back on when you bring the app out of suspension. If you need the location always, then GPS is your best bet. People understand that GPS based apps require power and will come up with a way to plug it in to use it long term.
Perhaps you should think about the use cases for your app and ask if people are willing to drain their battery power for whatever functionality you will offer.
Using acceleration and time you can calculate change in velocity but you don't necessarily know the starting velocity. Plus the accelerometer in an iPhone probably isn't accurate enough for this anyway, but it's not a bad shout.

How to get the most accurate possible speed from GPS in Android

How can I get an accurate speed from GPS in Android?
Yes, I am aware of the location.getSpeed() method in the Location class. Problem is, the default implementation returns 0.0 as speed: apparently that is the default behavior.
What I'm currently doing, is as follows, consider location objects a and b, where a is taken first, b later:
a.distanceTo(b)/(b.getTime()-a.getTime());
(simplified for readability, original code deals with history ArrayList)
Problem is that this is somewhat inaccurate: under normal circumstances, the data points are so close to one another that the GPS inaccuracy really becomes an issue. Either I would need to reduce the update frequency or calculate the speed relative to a point further away. The former I don't want to do, as I want to get as high a frequency as possible, but perhaps I could filter the points to calculate speed against based on their distance to one another?
The optimal solution, which I assumed the getSpeed() method would do, would be to calculate the speed against the GPS satellites themselves, thus getting a more accurate result.
Am I using the getSpeed() wrong somehow?
Since your keeping a history why not...
Get the current location and time
Find the speed between current and last ~10
Take an average of your results
Use the formula you stated to determine average speed but makes sure your two points are in a straight line. You could see if the user is still traveling in the same direction by calling Location.getBearing(). If it is close enough you could assume they traveled in a straight line. If not just discard the result.
Keep in mind this speed will be affected by any stops such as stop signs or stop lights. Sample as often as possible and discard any obvious outliers.
The emulator apparently always answers 0 as speed, but the real device
should not. Do you have the same issue on the real device? – Stefan
Mar 20 at 8:21
Stefan's answer was actually correct. Apparently the emulator does not give the speed, as that's not contained in the GPX file input as the testing data. So if you want to show speed, test on a real device and go for a jog, it'll work (for most devices).
Below are some thoughts for other methods of detecting speed, but not strictly relevant, but might be interesting if you're working with GPS.
Due to the relative inaccuracy of GPS, particularly at slow speeds or curvy roads the speed is hard to calculate: either the distance between data points is so short GPS inaccuracy comes to play, or so long it becomes inaccurate when not moving straight. Also, if the minimum distance between data points to calculate speed is long, at slow speeds the update interval becomes a problem.
There are ways around this problem, such as using the getAccuracy() method to calculate minimum safe distance between data points and using it dynamically, filtering data points based on maximum acceleration and deceleration values, movement direction and so on. You can also calculate a rolling average to calm down the changes a little and get a pretty good idea of what's what.
The above methods may be useful also even if you don't calculate speed based on distance covered, as sometimes the GPS seems to return speed as 0, even when you're moving. I used acceleration/deceleration figures from F1 cars as filters :)

Calculate speed between two location points in Android

This seems like it should be simple enough, but I can't seem to get a clear example of how to calculate my speed using Android's Location Services.
All I want to do is calculate my speed based on my previous and current GPS position. getSpeed() doesn't work, because it needs to be set at some point using setSpeed().
So is there any sample code out there that I can run in Eclipse against my Android emulator running a GPX file, that will show me my pace in any measure (meters, feet, miles, etc., doesn't matter - I can always convert this to what I need).
Thanks in advance,
Alex
Do you just mean something like this? Gives you the speed in m/s.
oldLocation.distanceTo(newLocation) / (newLocation.getTime() - oldLocation.getTime())
getSpeed() doesn't work, because it needs to be set at some point using setSpeed().
(In case you are using an actual device -- ). No, the speed is determined by the GPS chipset and it should be more accurate than getting the distance and time between two consecutive locations. You do not need to setSpeed() . Call hasSpeed() to see if the speed is valid.
(In case you're not using an actual device)
I've had good success using the mock location provider where you can set the Location object you want with setSpeed() in meters/second
Here are some ways of doing this :
Mocking using NMEA
Mocking using code

When GPS is not available how to get notified when the phone is in motion?

I would like to detect when the phone is in motion, but not all kind of motion. For example picking up or waving the phone should not trigger.
I would like ideally to run a code when the phone/person is in > "walking" state. What options I have?
you can use a combination of the accelerometer and the digital compass, in phones that have them, to determine a speed and direction.
If all you need to do is determine if the person is walking, all you need is the accelerometer. Just process its output for foot steps.
There are plenty of tutorials on the web for detecting foot steps with an accelerometer.
You can also get your location from Wifi-Networks and Cell Towers. All location providers in Android are subclasses of android.location.LocationProvider. That's probably a good place to start. I don't know that either of those would be best for you, as their "range" can be several hundreds of feet wide.
Android accelerometer provides us the feature to know the acceleration using SensorEvent class. So use the object of the class, and handle onSensorChanged(), to determine the movement in the device.
x = sensorEvent.values[0];
States the acceleration in x direction.
So what you may be interested in is, find acceleration in x,y and z directions, and try to calculate the Average and standard deviation for the last 10 such samples.
Working on the standard deviation will surely get you to the right point! If it goes to 0, it means device is still. If SD>0.5 for more than 15sec's or so..it means device is continously moving! Lemme know if you need more help!
Cheers,
Nithin

Categories

Resources