Can I access GPS for glass via the Android SDK?

I understand that GDK has not yet been released, but I started experimenting with putting the APK on Glass, as google encourages ( https://developers.google.com/glass/gdk ). I wrote a very simple running app that calculates distance and speed from the LocationManager. Does anyone know if this should or should not currently work with Glass (i.e., without the need to pair with the phone)? From reading on the glass edge it seems that he has a built-in GPS chip. I'm having problems getting GPS from glass, although I feel that it is currently limited.

+3
source share
1 answer

Yes, but the trick is that you need to use requestLocationUpdates () instead of getLastKnownLocation (). This is the part that baffled me since I hit zero if I just got getLastKnownLocation ().

Thus, using the Compass example as a basis (to avoid other potential problems and efforts), if you have what works correctly here, a very simple hack to expand it and add location updates:

  • If you are using Android Studio, for example, me, and you are triggering "unreferenced symbol" errors (for example, me) and are too lazy to solve it differently (I think you see the trend ...), define a few things ahead of time:

    private LocationManager mLocationManager; private LocationListener mLocationListener; private Criteria criteria; 
  • Update 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, he simply adds a LocationListener fragment from the docs: http://developer.android.com/guide/topics/location/strategies.html and adds some criteria. You could have hard-coded the provider, but I wanted to use what Glass reported as the β€œbest”, so it (hopefully) would adapt to different scenarios (until it was confirmed that at the moment it was just hope).

  • Add a makeUseOfLocation () method to a simple method that says 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; } 

    That way you can roam and hear where you think. I wandered around the building just a little for that.

  • Add appropriate permissions for 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 it with and without Glass (XE9 update) paired with my phone, and everything seemed to behave as expected.

+1
source

Source: https://habr.com/ru/post/1498070/


All Articles