Android Things:通过Wi-Fi定位

时间:2018-01-01 18:34:03

标签: location wifi android-things

您好可以通过Wi-Fi获取基于Android物品的Pico i.MX7的位置吗?当我在手机上运行这个代码时,一切都很好,但是当我在Pico上运行它时,似乎requestLocationUpdates没有更新任何东西,并且该位置仍为空。我也有Manifest的权限(FINE和COARSE位置)。谢谢!

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) {

    }
};

1 个答案:

答案 0 :(得分:0)

似乎没有自动更新Android Things中的位置的实现(即使是LocationManager.NETWORK_PROVIDER),但无论如何,如果您可以访问Internet,则可以使用Google Maps Geolocation API请求,例如:

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

在请求中提供有关目标WiFi access point的信息,如果目标WiFi接入点位于Google数据库中,则您的{/ 3}}具有Lat / Lon和准确性。为了获得更精确的结果,您应该使用response JSON,例如通过UART连接。 GPS module是在Android Things上使用GPS模块的官方说明,Here - 用户模式驱动程序的源代码。此外,如果您使用带有here的GPS模块,您可以将位置更新发送到框架(通过GpsDriver方法),您的问题代码也可以使用。