Tracking location on Android – using GPS to record data in Neurolab

In the Neurolab-Android app, we have a feature for recording data. It uses data incoming from the hardware device and stores it in a data table format with various parameters. Two of these parameters happened to be the latitude and longitude (location) of the user using the app. For that, we needed to implement a location tracking feature which can be used while recording the data in the data table. Let’s start off with adding the required permission uses in the Android Manifest file. <uses-permission android:name="android.permission.INTERNET"/> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/> These will be used to ask permissions from the user to access the devices' internet and location/GPS. Now, we will be making our Location Tracker class that will be used in tracking the location and getting the corresponding latitude and longitude.  Firstly, we are going to define some variables - an array of manifest permissions for enabling GPS, some constant values for requesting specific permissions, a provider for the GPS provider. private String[] mapPermissions = new String[]{ Manifest.permission.ACCESS_FINE_LOCATION }; public static final int GPS_PERMISSION = 103; private static final int UPDATE_INTERVAL_IN_MILLISECONDS = 400; private static final int MIN_DISTANCE_CHANGE_FOR_UPDATES = 1; private String provider = LocationManager.GPS_PROVIDER; We also need to initialize and have a location manager ready to request location updates from time to time. Defining a locationManager need to get the system service for location. We define it in the following way: LocationManager locationManager = (LocationManager)getContext().getSystemService(Context.LOCATION_SERVICE) Next, we set up a location listener which listens to location changes of the device. We define that in the following way: private LocationListener locationListener = new LocationListener() { @Override public void onLocationChanged(Location location) { bestLocation = location; } Now, that we have our variables, permissions, location manager and listener set up, we can start capturing the location. @SuppressLint("MissingPermission") public void startCaptureLocation() { if (PermissionUtils.checkRuntimePermissions(context, mapPermissions)) { locationManager.requestLocationUpdates(provider, UPDATE_INTERVAL_IN_MILLISECONDS, MIN_DISTANCE_CHANGE_FOR_UPDATES, locationListener); } else { PermissionUtils.requestRuntimePermissions(context, mapPermissions, GPS_PERMISSION); } } Firstly, we need to check for the runtime permissions required for our task. We achieve this with the function ‘checkRuntimePermissions’ which we have created in a utility class named ‘PermissionUtils’. The code of this utility class can be found here: PermissionUtils.java. It basically self checks individual permissions from the array passed in the arguments with the Android system. We then use the location manager instance to request current location updates using the constants and the location listener we defined earlier. So now, that we have started capturing the user device location, we can get the device Location object values (latitude and longitude).  @SuppressLint("MissingPermission") public Location getDeviceLocation() { if (bestLocation == null) { if (PermissionUtils.checkRuntimePermissions(context, mapPermissions)) { locationManager.requestLocationUpdates(provider, UPDATE_INTERVAL_IN_MILLISECONDS, MIN_DISTANCE_CHANGE_FOR_UPDATES, locationListener); return locationManager.getLastKnownLocation(provider); } else { return defaultLocation(); } } else { return bestLocation; } } This method requests the location and returns a Location object. If there is an internet connection problem or the device location is disabled from the device settings, it returns a default location object. Here in the Neurolab project, we had set the default location latitude and longitude to 0.0. Now we can…

Continue ReadingTracking location on Android – using GPS to record data in Neurolab

Getting user Location in SUSI Android App and using it for various SUSI Skills

Using user location in skills is a very common phenomenon among various personal assistant like Google Assistant, Siri, Cortana etc. SUSI is no different. SUSI has various skills which uses user’s current location to implement skills. Though skills like “restaurant nearby” or “hotels nearby” are still under process but skills like “Where am I” works perfectly indicating SUSI has all basic requirements to create more advance skills in near future. So let’s learn about how the SUSI Android App gets location of a user and sends it to SUSI Server where it is used to implement various location based skills. Sources to find user location in an Android app There are three sources from which android app gets users location : GPS Network Public IP Address All three of these have various advantages and disadvantages. The SUSI Android app uses cleverly each of them to always get user location so that it can be used anytime and anywhere. Some factors for comparison of these three sources : Factors GPS Network IP Address Source Satellites Wifi/Cell Tower Public IP address of user’s mobile Accuracy Most Accurate (20ft) Moderately Accurate (200ft) Least Accurate (5000+ ft) Requirements GPS in mobile Wifi or sim card Internet connection Time taken to give location Takes long time to get location Fastest way to get location Fast enough (depends on internet speed) Battery Consumption High Medium Low Permission Required User permission required User permission required No permission required Location Factor Works in outdoors. Does not work near tall buildings Works everywhere Works everywhere Implementation of location finding feature in SUSI Android App SUSI Android app very cleverly uses all the advantages of each location finding source to get most accurate location, consume less power and find location in any scenario. The /susi/chat.json endpoint of SUSI API requires following 7 parameters : Sno. Parameter Type Requirement 1 q String Compulsory 2 timezoneOffset int Optional 3 longitude double Optional 4 latitude double Optional 5 geosource String Optional 6 language Language  code Optional 7 access_token String Optional In this blog we will be talking about latitude , longitude and geosource. So, we need these three things to pass as parameters for location related skills. Let’s see how we do that. Finding location using IP Address: At the starting of app, user location is found by making an API call to ipinfo.io/json . This results in following JSON response having a field “loc” giving location of user (latitude and longitude. { "ip": "YOUR_IP_ADDRESS", "city": "YOUR_CITY", "region": "YOUR_REGION", "country": "YOUR_COUNTRY_CODE", "loc": "YOUR_LATITUDE,YOUR_LONGITUDE", "org": "YOUR_ISP" } By this way we got latitude, longitude and geosource will be “ip” . We find location using IP address only once the app is started because there is no need of finding it again and again as making network calls takes time and drains battery. So, now we have user’s location but this is not accurate. So, we will now proceed to check if we can find location using network is more accurate than location using IP…

Continue ReadingGetting user Location in SUSI Android App and using it for various SUSI Skills