2

Hello it's possible to get location of Pico i.MX7 based on Android Things over Wi-Fi? When I run this code on phone everything works great, but when I run it on Pico, it seems that requestLocationUpdates doesn't update anything and the location is still null. I have permissions in Manifest too(both FINE and COARSE location). Thanks!

public class MainActivity extends Activity {

private LocationManager mLocationManager;
private String locationProvider = LocationManager.NETWORK_PROVIDER;

private TextView lat;
private TextView lon;

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

    lat = (TextView) findViewById(R.id.lat);
    lon = (TextView) findViewById(R.id.lon);

    if (ContextCompat.checkSelfPermission(this,
            Manifest.permission.ACCESS_FINE_LOCATION)
            != PackageManager.PERMISSION_GRANTED) {

        if (ActivityCompat.shouldShowRequestPermissionRationale(this,
                Manifest.permission.ACCESS_FINE_LOCATION)) {

        } else {

            ActivityCompat.requestPermissions(this,
                    new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
                    2);
        }
    }

    mLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    Location location = mLocationManager.getLastKnownLocation(locationProvider);
    mLocationManager.requestLocationUpdates(locationProvider, 10, 0, mLocationListener);
    // Toast.makeText(this, "Start", Toast.LENGTH_SHORT).show();
    showMyLocation(location);

    //Intent intent = new Intent(getApplicationContext(), SprinklerActivity.class);
    //startActivity(intent);
}

private void showMyLocation(Location l) {
    if (l == null) {
        Toast.makeText(this, "No location", Toast.LENGTH_SHORT).show();
        lat.setText("0");
        lon.setText("0");
    } else {
        Toast.makeText(this, "Latitude: " + l.getLatitude(), Toast.LENGTH_SHORT).show();
        Toast.makeText(this, "Longitude: " + l.getLongitude(), Toast.LENGTH_SHORT).show();
        lat.setText(l.getLatitude() + "");
        lon.setText(l.getLongitude() + "");
    }
}

private LocationListener mLocationListener = new LocationListener() {
    @Override
    public void onLocationChanged(Location location) {
        showMyLocation(location);
    }

    @Override
    public void onStatusChanged(String s, int i, Bundle bundle) {

    }

    @Override
    public void onProviderEnabled(String s) {

    }

    @Override
    public void onProviderDisabled(String s) {

    }
};
Onik
  • 19,396
  • 14
  • 68
  • 91
Yuro8
  • 41
  • 6
  • Do you have GPS turned on on the phone? – Onik Jan 01 '18 at 19:04
  • @Onik no, only wifi – Yuro8 Jan 01 '18 at 19:30
  • Is `Pico` and your phone are on the same WiFi network? – Onik Jan 01 '18 at 19:31
  • Yes they are. I even tried to connect Pico with cable but it still doesn't work – Yuro8 Jan 01 '18 at 19:36
  • I recieve first null in LastKnownLocation and new location updates are not recieved. Yes, I tried to reinstall it too – Yuro8 Jan 01 '18 at 19:57
  • Are you using the Google Play Services location provider or one built into the OS? The built-in version may only use GPS whereas Play Services will use several sensors – Nick Felker Jan 01 '18 at 20:06
  • @Nick Felker, he uses built-in API. Yuro8, try with [FusedLocationProviderClient](https://developers.google.com/android/reference/com/google/android/gms/location/FusedLocationProviderClient) – Onik Jan 01 '18 at 20:17
  • I tried it even with FusedLocaton but still the same problem :( – Yuro8 Jan 02 '18 at 21:58
  • Yes it's installed.I compile these two: compile 'com.google.android.gms:play-services-location:11.6.0' compile 'com.google.android.gms:play-services-nearby:11.6.0' – Yuro8 Jan 02 '18 at 22:42

1 Answers1

0

Seems there is no implementation for automatically update location in Android Things (even for LocationManager.NETWORK_PROVIDER), but anyway, if you have Internet access, you can use Google Maps Geolocation API requests like:

https://www.googleapis.com/geolocation/v1/geolocate?key=YOUR_API_KEY

with information about target WiFi access point in request, and if target WiFi access point is in Google database your got response JSON with Lat/Lon and accuracy. For more precisely results you should use GPS module, connected via, for example, UART. Here is the official description of using GPS modules on Android Things, and here - the source code of user mode driver. Moreover, if you use GPS module with GpsDriver you can send the location update to the framework (via .reportLocation() method) and your question code should works.

Andrii Omelchenko
  • 13,183
  • 12
  • 43
  • 79
  • I'm fine with precision about +-1km, even more, so wifi should be enough. It's pity that it doesn't update. This solution looks so complicated :( but I try to give it a shot. – Yuro8 Jan 02 '18 at 22:18
  • @Yuro8 It's not so complicated, as it seems at first look. There are many [examples](https://stackoverflow.com/a/29466338/6950238) and [tutorials](https://www.studytutorial.in/android-httpurlconnection-post-and-get-request-tutorial). – Andrii Omelchenko Jan 03 '18 at 08:46
  • Yeah, but now I'm reading that it's impossible to get the mac address of device from Android 6.0 and higher. Oh god, the android things is so ragged – Yuro8 Jan 03 '18 at 20:33
  • @Yuro8 Try to add fake address like "01:23:45:67:89:AB". Or at first get WiFi MAC address from UI (Settings->etc.) – Andrii Omelchenko Jan 03 '18 at 20:53
  • I think the request is good but it still return 400 - JSON not valid :( ` {"cellTowers": [{"cellId":"", "locationAreaCode":"", "mobileCountryCode":"", "mobileNetworkCode":""}], "wifiAccessPoints":[ { "macAddress":"01:23:45:67:89:AB"},{"macAddress":"11:23:45:67:89:AB"}]} ` – Yuro8 Jan 03 '18 at 21:53
  • @Yuro8 Send empty `cellTowers` array, or even not add that tag instead of send empty ("") fields. And there are strange symbols after 67 in second mac address. At first, try to send JSON from [example](https://developers.google.com/maps/documentation/geolocation/intro#request_body): `{ "considerIp": "false", "wifiAccessPoints": [ { "macAddress": "00:25:9c:cf:1c:ac", "signalStrength": -43, "signalToNoiseRatio": 0 }, { "macAddress": "00:25:9c:cf:1c:ad", "signalStrength": -55, "signalToNoiseRatio": 0 } ] }` – Andrii Omelchenko Jan 04 '18 at 09:02