Capturing Position Data with PSLab Android App
PSLab Android app by FOSSASIA can be used to visualize different waveforms, signal levels and patterns. Many of them involve logging data from different instruments. These data sets can be unique and the user might want them to be specific to a location or a time. To facilitate this feature, PSLab Android app offers a feature to save user’s current location along with the data points.
This implementation can be done in two ways. One is to use Google Maps APIs and the other one is to use LocationManager classes provided by Android itself. The first one is more on to proprietary libraries and it will give errors when used in an open source publishing platform like Fdroid as they require all the libraries used in an app to be open. So we have to go with the latter, using LocationManager classes.
As first step, we will have to request permission from the user to allow the app access his current location. This can be easily done by adding a permission tag in the Manifest.xml file.
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
Since we are not using Google Maps APIs, capturing the current location will take a little while and that can be considered as the only downfall of using this method. We have to constantly check for a location change to capture the data related to current location. This can be easily done by attaching a LocationListener as it will do the very thing for us.
private LocationListener locationListener = new LocationListener() { @Override public void onLocationChanged(Location location) { locationAvailable = true; } @Override public void onStatusChanged(String s, int i, Bundle bundle) {/**/} @Override public void onProviderEnabled(String s) {/**/} @Override public void onProviderDisabled(String s) { // TODO: Handle GPS turned on/off situations } };
In case if the user has turned off GPS in his device, this method wouldn’t work. We will have to request him to turn the feature on using a simple dialog box or a bottom sheet dialog.
We can also customize how frequent the locationlistener should check for a location using another class named LocationManager. This class can be instantiated as follows:
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Then we can easily set the time interval using requestLocationUpdates method. Here I have requested location updates in every one second. That is a quite reasonable rate.
locationManager.requestLocationUpdates(provider, 1000, 1, locationListener);
Once we have set all this up, we can capture the current location assuming that the user has turned on the GPS option from his device settings and the LocationManager class has a new location as we checked earlier.
if (locationAvailable) { Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); }
Each location will contain details related to its position such as latitudes and longitudes. We can log these data using the CSVLogger class implementation in PSLab Android app and let user have this option while doing his experiments.
Reference:
- An open source implementation : https://github.com/borneq/HereGPSLocation/blob/master/app/src/main/java/com/borneq/heregpslocation/MainActivity.java
Google Maps: https://developers.google.com/maps/documentation/android-sdk/intro