可将GPS玻璃通过Android SDK来访问?可将、玻璃、来访问、GPS

2023-09-05 08:46:38 作者:熟悉街道

我意识到GDK还没有被释放,但是我已经开始与投入的APK的玻璃作为谷歌鼓励(的 https://developers.google.com/glass/gdk )。我写了慢跑一个非常简单的应用程序,计算从LocationManager距离和速度。有谁知道这是否应该或不应该与当前工作玻璃本身(即无需配对手机)?从在玻璃拆解阅读了它看起来像它有一个内置的GPS芯片。我在从玻璃越来越GPS虽然问题,我得到的感觉是当前受限制。

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.

推荐答案

是的,但诀窍是,你需要使用requestLocationUpdates()而不是getLastKnownLocation()。这是绊倒了我,因为我最终得到空,如果我只是做了getLastKnownLocation()的一部分。

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:

如果您使用的是Android的工作室和我一样,并运行到未引用的符号的错误(比如我),都懒得去解决它以不同的方式(我想你看到的趋势...)时间,提前确定几件事情:

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;

更​​新的onkeydown()方法

Android studioSDK下载与配置

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;
...

这只是从文档的LocationListener的片段中加入:的http:// developer.android.com/guide/topics/location/strategies.html~~V ,并加入了一些标准。你可以硬code的供应商,但我想用的任何玻璃汇报为最佳,因此这将(希望)适应不同的场景(未证实的是,所以它只是一个希望在片刻)。

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

添加一个简单的makeUseOfLocation()方法,讲细节:

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.

添加适当的权限,您的清单

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" />

构建和运行...我尝试这个带或不带玻璃(XE9更新)配对到我手机上,一切似乎像预期的那样。

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