سؤال

From a native application how can we detect Google Glass verses a smart phone from code?

Moving correct answer to question:

boolean isRunningOnGlass() {
     return "Google".equalsIgnoreCase(Build.MANUFACTURER) && Build.MODEL.startsWith("Glass");
 }
هل كانت مفيدة؟

المحلول

Another way of doing this would be to use the Build API:

http://developer.android.com/reference/android/os/Build.html

نصائح أخرى

Using the GDK, you could use:

 boolean isRunningOnGlass() {
     return "Google".equalsIgnoreCase(Build.MANUFACTURER) && Build.MODEL.startsWith("Glass");
 }

(The model check may be good if a new model of Google Glas comes out.)

I suspect that there will be an official way of getting this, but perhaps you could use the browser user-agent:

1) On Android, you can get the user-agent programmatically via: how to get the default HTTP USER AGENT from the android device?

2) The user-agent can change, of course, but at of July 2013 it is/was:

Mozilla/5.0 (Linux; U; Android 4.0.4; en-us; Glass 1 Build/IMM76L; XE7) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30

You can figure out your android app is running on which OS Build, Product, Device etc. by using the android.os.Build class.

Eg: You can detect if your app is running on google glass(API 19) like this:

if(Build.VERSION.SDK_INT==Build.VERSION_CODES.KITKAT){
        Log.e("SDK_INT",""+Build.VERSION.SDK_INT);
        Log.e("MODEL",""+Build.MODEL);
        Log.e("DEVICE",""+Build.DEVICE);
        Log.e("TYPE",""+Build.TYPE);
        Log.e("HARDWARE",""+Build.HARDWARE);
        Log.e("BRAND",""+Build.BRAND);
        Log.e("DISPLAY",""+Build.DISPLAY);
        Log.e("MANUFACTURER",""+Build.MANUFACTURER);
        Log.e("PRODUCT",""+Build.PRODUCT);
        if (isGlass()){
            Log.e("isGlass","True");
        }
    } else {
    Log.e("Other",""+Build.VERSION.SDK_INT);
}
boolean isGlass(){return"Google".equalsIgnoreCase(Build.MANUFACTURER)&&Build.MODEL.startsWith("Glass");
}

Log Results

09-13 17:58:42.835 24240-24240/com.example.myxlab.beyondartest E/SDK_INT: 19
09-13 17:58:42.835 24240-24240/com.example.myxlab.beyondartest E/MODEL: Glass 1
09-13 17:58:42.835 24240-24240/com.example.myxlab.beyondartest E/DEVICE: glass-1

Similarly, for watches (API KITKAT_WATCH = 20).

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top