Can GPS for Glass be accessed through the Android

2019-03-31 05:53发布

问题:

I realize the GDK hasn't been released yet but I've started experimenting with putting APKs on Glass as google encourages (https://developers.google.com/glass/gdk). I wrote a very simple app for jogging that calculates distance and pace from the LocationManager. Does anyone know if this should or should not currently work natively with Glass (i.e. without having to pair with a phone)? From reading up on a Glass teardown it looks like it has a built in GPS chip. I'm having problems getting GPS from Glass though and I'm get the feeling it is currently restricted.

回答1:

Yes, but the trick is you need to use requestLocationUpdates() instead of getLastKnownLocation(). That's the part that tripped me up, as I ended up getting nulls if I just did getLastKnownLocation().

So using the Compass example as a basis (to avoid other potential issues and effort), if you have that running correctly here's a very basic hack to extend it and add location updates:

  1. If you're using Android Studio, like me, and running into "unreferenced symbol" errors (like me) and are too lazy to solve it a different way (I think you see the trend...), define a few things ahead of time:

    private LocationManager mLocationManager;
    private LocationListener mLocationListener;
    private Criteria criteria;
    
  2. Update the onKeyDown() method

    ...
    String directionName = mSpokenDirections[getDirectionIndex(heading)];
    String headingText = String.format(speechFormat, (int) heading, directionName);
    // **** Location hack starts here... ****
    criteria = new Criteria();
    criteria.setAccuracy(Criteria.ACCURACY_FINE);
    provider = mLocationManager.getBestProvider(criteria, true);
    boolean isEnabled = mLocationManager.isProviderEnabled(provider);
    if (isEnabled) {
        headingText += " and GPS is enabled on provider " + provider;
        // 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.
                makeUseOfNewLocation(location);
            }
    
            public void onStatusChanged(String provider, int status, Bundle extras) {}
    
            public void onProviderEnabled(String provider) {}
    
            public void onProviderDisabled(String provider) {}
        };
    
        // Register the listener with the Location Manager to receive location updates
        mLocationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 10000, 0, locationListener);
    } else {
        headingText = ", but GPS not enabled on provider " + provider;
    }
    // **** Location hack ends here ****
    mSpeech.speak(headingText, TextToSpeech.QUEUE_FLUSH, null);
    return true;
    ...
    

    Here it's just adding in the LocationListener snippet from the docs: http://developer.android.com/guide/topics/location/strategies.html, and adding some criteria. You could hardcode in a provider, but I wanted to use whatever Glass reported back as "best", so it would (hopefully) adapt to different scenarios (haven't verified that yet, so it's just a hope at the moment).

  3. Add in a simple makeUseOfLocation() method that speaks the details:

    public void makeUseOfNewLocation(Location location) {
      if (location != null) {
        String lat = "Latitude: " + location.getLatitude();
        String lng = ", Longitude: " + location.getLongitude();
        String summary = lat + lng;
        mSpeech.speak(summary, TextToSpeech.QUEUE_FLUSH, null);
     }
     return;
    }
    

    This way you can wander around and hear where it thinks you are. I wandered around the building a bit just for the heck of it.

  4. Add the appropriate permissions to your manifest

    
    
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    

Build and run... I tried this both with and without Glass (XE9 update) paired to my phone and everything seemed to behave as expected.