Display the NMEA sentence? - android

Any one help to display the NMEA sentence like $GPRMC .....
I am using the below code:
public class SampleNMEA extends Activity implements LocationListener, GpsStatus.Listener,GpsStatus.NmeaListener{
LocationManager lm;
TextView output;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
output = (TextView)findViewById(R.id.out);
lm = (LocationManager) getSystemService(LOCATION_SERVICE);
lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 60000,10,this);
final Location location=lm.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
output.append("\nBelow Data is about the your location:");
output.append("\n\n\nLatit:"+location.getLatitude());
output.append("\n\nLongitude:"+location.getLongitude());
output.append("\n\nTime: "+location.getTime());
}
public void onLocationChanged(Location location)
{
output.append("\n\n\nLatitude:"+location.getLatitude());
output.append("\n\nLongitude:"+location.getLongitude());
output.append("\n\nTime: "+location.getTime());
}
public void onProviderDisabled(String arg0) {
// TODO Auto-generated method stub
}
public void onProviderEnabled(String arg0) {
// TODO Auto-generated method stub
}
public void onStatusChanged(String arg0, int arg1, Bundle arg2)
{
lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 60000,10,this);
final Location location=lm.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
output.append("\n\n\nLatitude:"+location.getLatitude());
output.append("\n\nLongitude:"+location.getLongitude());
output.append("\n\nTime: "+location.getTime());
}
public void onGpsStatusChanged(int event)
{
}
public void onNmeaReceived(long timestamp, String nmea)
{
output.append("NMEA Sentence: "+nmea);
}
}
Any one help I added permissions in manifest.xml
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"></uses-permission>

Change your provider to GPS_PROVIDER.
You can get the nmea sentence by Gps provider.
Change this line
**lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 60000,10,this);**
to
**lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 60000,10,this);**
Now check in onNmeaReceived() method.

Just add a NmeaListener
public class SampleNMEA extends Activity implements LocationListener, GpsStatus.Listener,GpsStatus.NmeaListener{
LocationManager lm;
TextView output;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
output = (TextView)findViewById(R.id.out);
lm = (LocationManager) getSystemService(LOCATION_SERVICE);
lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 60000,10,this);
final Location location=lm.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
output.append("\nBelow Data is about the your location:");
output.append("\n\n\nLatit:"+location.getLatitude());
output.append("\n\nLongitude:"+location.getLongitude());
output.append("\n\nTime: "+location.getTime());
//add listener here
lm.AddNmeaListener(this);
}

I have Samsung Galaxy Spica and yes, it doesn't work. However, I've installed 'ultra gps logger' (link) and NMEA data is correctly read. Perhaps author of this tool used different solution for reading NMEA which means that is possible.

Are you calling addNmeaListener(GpsStatus.NmeaListener) to set that class as a listener? If not, you should!
To do so, call it like lm.addNmeaListener(this);.
Good luck &
Regards

The conclusion being reached in this discussion here, is that NMEA doesn't work on some phones (the onNmeaReceived event never fires). ...but would love to be corrected on this.

If fakeGps is in list of installed apps, be sure to delete it and restart device in order to start receiving Nmea info. You can install free Nmea Info monitor from play market to test Nmea receiver on your device.

Related

Real time data update

My app uses google map to show user's location in real time. Therefore I would like it to be updated with every data change on my Parse server.
I've looked all over the net and as I see it I have 3 options:
using syncAdapter - the problem is that everywhere it is written that it does not meant for real time data transfer (I don't really understand why), on other hand it must be more efficient than updating every 5 sec.
using asyncTask - the problem is that it probably consumes a lot of battery and data usage to run it every 5 sec.
using service - same problem as asyncTask.
As i'm very confused, please help me understand what is the appropriate way to implement my real time map.
Thank's all
The best way i know is to use LocationListener to update the location only if it has been changed. You can use the onLocationChanged() method to update the location.
public class LocationHelper {
private LocationManager locationManager;
private Location currentLocation;
public LocationHelper(Context context) {
locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
locationManager.removeUpdates(locationListener);
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
}
public void setLocation(Location location) {
currentLocation = location;
}
private final LocationListener locationListener = new LocationListener() {
#Override
public void onLocationChanged(Location location) {
setLocation(location);//Only update location if it has changed.
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {}
#Override
public void onProviderEnabled(String provider) {}
#Override
public void onProviderDisabled(String provider) {}
};
}
If the location hasn't been changed, you can just remember the last known location.

onLocationChanged not called on some devices

This is a question which has been asked numerous times but I could not find a solution that always works.
I am developing an application using the Fused location provider.
In the onConnected() method, I am requesting for location updates and the application logic will be initiated once a location fix is generated and onLocationChanged() is called. (Please refer to my code below).
Problem onLocationChanged() method is never called on some devices. I use a Samsung Tab 2 and a Samsung Galaxy Grand for testing. This code works perfectly fine on the Tab 2 but does not work on Grand. By does not work, I mean that locationClient gets connected but onLocationChanged() is never called.
Earlier, I used the location manager for getting location and in that implementation, the same problem occurred. So, I tried implementing the fused location provider but I still get the same problem.
Can anyone help me out with this issue? Is there something I am missing here?
public class MainActivity extends Activity implements GooglePlayServicesClient.ConnectionCallbacks, OnConnectionFailedListener, LocationListener {
LocationClient locationclient;
LocationRequest lr;
Location loc1;
static String address;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
locationclient = new LocationClient(this,this,this);
locationclient.connect();
}
#Override
public void onConnected(Bundle arg0) {
// TODO Auto-generated method stub
lr=LocationRequest.create();
lr.setInterval(100);
locationclient.requestLocationUpdates(lr, this);
Log.d("LocationClient","On Connected");
}
#Override
public void onDisconnected() {
// TODO Auto-generated method stub
locationclient.disconnect();
}
#Override
public void onConnectionFailed(ConnectionResult arg0) {
// TODO Auto-generated method stub
}
#Override
public void onLocationChanged(Location loc) {
// TODO Auto-generated method stub
// Application Logic
Log.d("LocationClient","Last Known Location LC:" + loc.getLatitude() + "," + loc.getLongitude());
}
}
I observed same behavior on Galaxy Nexus and recognized two things causing onLocationChanged() to not being called. There are
Corresponding location source is not enabled in System Settings. For PRIORITY_HIGH_ACCURACY the GPS satellites must be enabled, for PRIORITY_BALANCED_POWER_ACCURACY Wi-Fi & mobile network location must be enabled.
Even for the enabled location sources there can be no valid location information available. For instance, PRIORITY_HIGH_ACCURACY source is enabled, but there is no (or not good) GPS reception (e.g. device is inside a building). Or PRIORITY_BALANCED_POWER_ACCURACY source is enabled, but both Wi-Fi and mobile data are switched off. In those cases location cannot be detected and onLocationChanged() will not be called.
To fix #1 I had to check whether required location sources are enabled even before connecting to Google Services API and if they are switched off, then I notified a user asked him/her to allow location access.
To solve #2 I decided to use LocationRequest.setExpirationDuration(). I set timeout at about 10 seconds, and immediately after calling requestLocationUpdates() I post a callback delayed by same 10 seconds. If delayed callback is called before requestLocationUpdates(), this means location cannot be detected due to reason #2. I do three retries and then show user an error message.
Android documentation is not great at this place, that's why I hope this helps.
lr.setInterval(100);
with it your interval is 0,1 second so it very hard for gps chipset to detect location. Your interval should be > 1 second.(with me it is > 10)
locationRequest it should be set:
setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)
because PRIORITY_BALANCED_POWER_ACCURACY in outdoor it doesn't get location . Code will look like :
lr=LocationRequest.create();
lr.setInterval(5 * 1000);// 5s
lr.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
locationclient.requestLocationUpdates(lr, this);
Hope this help.
See if you're getting a passed location by testing for if (loc == null). Even though it's the same provider, maybe one device is coming back null and bombing out, resulting in a look like it's not firing.
This maybe non-sense but I have faced this problem before, someone suggested that you should create your own LocationListener instead of letting your MainActivity implements LocationListener. Basically this:
locationclient.requestLocationUpdates(lr, your_location_listener);
Hope this helps.
I solved this by setting both parameters, minimum distance and minimum time between updates to 0.
As per what I have tested on 2 devices, now onLocationChanged() is being called regularly after requesting for updates.
try to remove and reinstall "google play services". Install it from google market link bellow,
google play services
Here Check this code out...
public class MyActivity extends Activity implements LocationListener {
double destLat, destLong;
LocationManager locationManager;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
}
#Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
destLat = location.getLatitude();
destLong = location.getLongitude();
}
#Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
#Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}

How to determine GPS getting unlocked while running the application in android?

I need to check whether GPS is locked in my application. I am able to check whether GPs is enabled in the phone. But once the GPS is locked, while running the application, it gets unlocked and I noticed that the GPS icon of the phone is blinking. I need to show an animation similar to that of phone in my application to acknowledge the user the GPS is unlocked. Anybody who knows the solution please post it.
Thanks in advance....
This is what I use. Start your animation when you requestLocationUpdates and end it when you get the onLocationChanged call. See my code below, it should solve your issue.
public class YourLocationClass implements LocationListener {
private LocationManager lm;
public void getLocation(Context ctx) {
lm = (LocationManager) ctx.getSystemService(Context.LOCATION_SERVICE);
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 15000, 25, this);
//START YOUR ANIMATION
}
public void onLocationChanged(Location location) {
//HERE YOU STOP YOUR ANIMATION AND GET NEW LOCATION
}
public void onProviderDisabled(String provider) {
// DO SOME STUFF
}
public void onProviderEnabled(String provider) {
// DO SOME STUFF
}
public void onStatusChanged(String provider, int status, Bundle extras) {
// DO SOME STUFF
}
}

Getting an updated location in Android

I'm using the code shown below to get an updated value for location every time a button is clicked. When my activity is resumed I get an update every second, so that when I call getLastKnownLocation I expect to have a location that have been updated in the last second.
Is that the correct way to do that?
I would expect the onLocationChanged event to be triggered every time I execute a 'geo fix' command (or max after 1s since I request update every 1s), but it's only triggered the first time. Why?
Any help/suggestion welcome!
Thanks
package org.digitalfarm.atable;
...
public class Atable extends Activity {
private Button mSearchButton;
private TextView mytext;
private LocationManager locationManager;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mSearchButton = (Button)this.findViewById(R.id.button);
mytext = (TextView) findViewById(R.id.dude);
locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
final Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
mSearchButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
String provider = locationManager.getBestProvider(criteria, true);
Location location = locationManager.getLastKnownLocation(provider);
}
});
}
//Start a location listener
LocationListener onLocationChange=new LocationListener() {
public void onLocationChanged(Location loc) {
//sets and displays the lat/long when a location is provided
String latlong = "Lat: " + loc.getLatitude() + " Long: " + loc.getLongitude();
mytext.setText(latlong);
}
public void onProviderDisabled(String provider) {
// required for interface, not used
}
public void onProviderEnabled(String provider) {
// required for interface, not used
}
public void onStatusChanged(String provider, int status,
Bundle extras) {
// required for interface, not used
}
};
//pauses listener while app is inactive
#Override
public void onPause() {
super.onPause();
locationManager.removeUpdates(onLocationChange);
}
//reactivates listener when app is resumed
#Override
public void onResume() {
super.onResume();
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,1000,100.0f,onLocationChange);
}
}
The time, specified in requestLocationUpdates is the shortest possible time interval for which a maximum of one update will occur. So you have registered for at most one update every second, but the actual update may take more that that to occur, and, furthermore, no guarantee is given for that.
From the documentation:
minTime - the minimum time interval for notifications, in milliseconds. This field is only used as a hint to conserve power, and actual time between location updates may be greater or lesser than this value.
Try to set that time interval to 0 and if there are no other problems (although it all seems fine to me), you should start getting the updates "as frequently as possible".

Android GPS location updates doesn't work

I am trying to get the GPS location of my HTC magic using the following code :
public class TestGPS extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final TextView tv1 = (TextView)findViewById(R.id.tv1);
final TextView tv2 = (TextView)findViewById(R.id.tv2);
LocationManager manager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
manager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, new LocationListener()
{
public void onStatusChanged(String provider, int status, Bundle extras)
{
}
public void onProviderEnabled(String provider)
{
}
public void onProviderDisabled(String provider)
{
}
public void onLocationChanged(Location location)
{
tv1.setText(String.valueOf(location.getLatitude()));
tv2.setText(String.valueOf(location.getLongitude()));
}
});
}
}
I waited for 5 minutes but none of the listener methods were called. The GPS icon shows up, stays there for some time but for some reason I don't get a fix, even outdoors in bright sunlight. I am testing on a HTC Magic with 1.6 SDK.
Can someone please tell me what's wrong with the code? Thanks.
The code looks fine. Make sure you have the ACCESS_FINE_LOCATION permission. Make sure your GPS is not broken -- in other words, does the built-in Maps application know where you are? Make sure your application works on the emulator with a fix you supply via the emulator.
Here is a sample project that uses GPS and that works fine on hardware.

Categories

Resources