0

I'm not sure whether this is the correct place to ask this question, perhaps there is an appropriate Stackexchange site for this, but here is my question anyway.

I recently developed a GPS tracking App for my Android Smart Phone so that my wife could track myself and our 10 year old son on a cycling/camping trip. The app recorded our GPS location (or Network location depending on availability) and uploaded the latitude and longitude coordinates to my server which were stored in a database. My wife was then able to view these coordinates on a web page using Google Maps Api.

The app worked great except that it relied on the battery life of my smart phone. I had to rely on a USB battery charger to keep my phone charged on the trip. Is there an Android based device that I could install my Tracking APK onto that has GPS and Mobile Networking so that you may add a sim. The device would need to be quite small and compact, have a good battery life and would not really need a screen so does not need to necessarily be a phone or tablet.

Edit

Thanks to @apmartin1991 for his answer. I haven't used DDMS to check battery performance, but my phone reports that the GPS usage is low. I will take a look at DDMS though. My code is listed below. Basically it pings GPS every 10 minutes and network 12 minutes. If it finds that the last known time when it found GPS is newer than when it found network location it will use GPS otherwise it will find network. When a location is retrieved it is posted to my server via an asynchronous task and stored in a db. As well as streamlining code, I would still like to find a dedicated device that will not only give enhanced battery life, but also separate my phone from the tracker. I know there are commercial products out there. I have used such devices at work such as the Garmin Pet Tracker, but these tend to be around £165 plus change.

Code:

Timer timer;
LocationManager locationManager;
// Ping GPS every 10 minutes
private static final int GPS_INTERVAL = 1000 * 60 * 10;
// Ping network every 12 minutes
private static final int NET_INTERVAL = 1000 * 60 * 12;
private ProgressDialog dialog;

TextView latText, lngText, locText;

String lat = "";
String lng = "";
String loc_service = "";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    latText = (TextView)findViewById(R.id.latText);
    lngText = (TextView)findViewById(R.id.lngText);
    locText = (TextView)findViewById(R.id.locText);

    locationManager = (LocationManager)this.getSystemService(Context.LOCATION_SERVICE);

    // Register the listener with the Location Manager to receive location updates
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, GPS_INTERVAL, 0, locationListener);
    locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, NET_INTERVAL, 0, locationListener);

    timer = new Timer();
    timer.schedule(new GetGPSStatus(), GPS_INTERVAL);
}

// Define a listener that responds to location updates
LocationListener locationListener = new LocationListener() {
    public void onLocationChanged(Location location) {
        // Called when a new location is found by the network location provider.
        timer.cancel(); 

        Log.d("Coords",lat+" "+lng+" "+loc_service);

        latText.setText(lat);
        lngText.setText(lng);
        locText.setText(loc_service);

        if(lat != "" && lat != "") {
            String data[] = {lat, lng, loc_service};    
            new SaveCoordsToDb(AmLocation.this).execute(data);
        }

        timer = new Timer();
        timer.schedule(new GetGPSStatus(), 1000);
    }

    public void onStatusChanged(String provider, int status, Bundle extras) {}

    public void onProviderEnabled(String provider) {}

    public void onProviderDisabled(String provider) {}
};

class GetGPSStatus extends TimerTask {
    @Override
    public void run() {

        Location gps_loc = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
        Location net_loc = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

        // If the last GPS location is newer than the network time, use the gps
        // otherwise use the network location
        if(gps_loc.getTime() > net_loc.getTime()) {
            lat = String.valueOf(gps_loc.getLatitude());
            lng = String.valueOf(gps_loc.getLongitude());
            loc_service = LocationManager.GPS_PROVIDER;
        }
        else {
            lat = String.valueOf(net_loc.getLatitude());
            lng = String.valueOf(net_loc.getLongitude());
            loc_service = LocationManager.NETWORK_PROVIDER;
        }


    }
}

// Save GPS data to the remote server db
public class SaveCoordsToDb extends AsyncTask<String, Void, String> {

    InputStream is = null;
    String result = "";
    private ProgressDialog dialog;
    private Context context;

    public SaveCoordsToDb(Context baseContext) {
        context = baseContext;
    }

    @Override
    protected void onPreExecute(){ 
       super.onPreExecute();
       dialog = new ProgressDialog(context);
       try {

           dialog.setMessage("Updating GPS Location ...");
           dialog.setCanceledOnTouchOutside(false);
           dialog.setCancelable(false);
           dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
           dialog.setButton(DialogInterface.BUTTON_NEGATIVE, "Cancel", new DialogInterface.OnClickListener() {
               @Override
               public void onClick(DialogInterface dialog, int which) {
                   dialog.dismiss();
               }
           });
           dialog.show();
       }
       catch(Exception e) {
           Log.e("Dialog",e.toString());
       }

    }

    @Override
    protected String doInBackground(String... urls) {


        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();  
        nameValuePairs.add(new BasicNameValuePair("lng", urls[0]));
        nameValuePairs.add(new BasicNameValuePair("lat", urls[1]));
        nameValuePairs.add(new BasicNameValuePair("source", urls[2]));

        HttpClient httpclient = new DefaultHttpClient();
        HttpPost post = new HttpPost("http://myserveraddy.co.uk/index/save");
        try {
            post.setEntity(new UrlEncodedFormEntity(nameValuePairs));  
            HttpResponse response = httpclient.execute(post);
            HttpEntity entity = response.getEntity();
            is = entity.getContent();
            Log.d("POST","200");
        } 
        catch (Exception e) {
            Log.e("POST", e.toString());
        }
        return null;
    }   

    @Override
    protected void onPostExecute(String result) {
        dialog.dismiss();
        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8);
            StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
            is.close();
            result = sb.toString();`

        } catch (Exception e) {
            Log.e("log_tag", "Error converting result " + e.toString());
        }
    }
}
amburnside
  • 1,943
  • 5
  • 24
  • 43
  • look at this might help you http://www.pocketfinder.com/gps-locator/personal-gps-locator/ – LMK Aug 13 '14 at 13:45

2 Answers2

0

The battery life won't change depending on your device, GPS among all devices generally uses the same amount of power, I don't think the phone is the cause of the power drain its the constant pinging to the GPS and to your server.

Without viewing the code it's hard to tell what could be using a large amount of battery, have you used DDMS to look for any code which is using a lot of battery / CPU time?

I had a similar problem where the battery life of a phone would last under thirty minutes to an hour, I also thought it was due to GPS, I later found it was an issue within code, both for the GPS and for the socket code. After altering some code around I made the battery last in excess of six to ten hours depending on usage.

It might be useful for you to post some code too, your GPS tasks / how you write to the database, anything like that. However DDMS would be your best option as you can see what is using the most CPU time / system resources, this could then highlight any issues you have within the code before you start to investigate a different device which may not give you much extra battery life.

apmartin1991
  • 3,064
  • 1
  • 23
  • 44
0

Raspberry Pi is a cheap and good alternative for the same. http://www.milos.ivanovic.co.nz/blog/252

For more details visit their site: http://www.raspberrypi.org/

thepace
  • 2,221
  • 1
  • 13
  • 21
  • A good solution but not very compact, plus I would need to keep the rpi plugged into the USB charger and how would I install my already existing APK?? – amburnside Oct 07 '14 at 06:59