How to check if Google Glass is connected to internet using GDK

若如初见. 提交于 2019-12-01 21:29:38
pt2121

If Glass connects to a phone with Bluetooth, your method returns true even when your phone has no WiFi and data connection.

I guess it's a correct behavior. getActiveNetworkInfo is more about a connection via available interfaces. It's not really about connection to the internet. It's like connecting to a router doesn't mean you connect to the internet.

NOTE (from the doc):

getActiveNetworkInfo returns

"a NetworkInfo object for the current default network or null if no network default network is currently active"

To check the internet connection, you might try ping Google instead though I think there might be a better way to check.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    new Thread(new Runnable() {

        @Override
        public void run() {
            Log.v(MainActivity.class.getSimpleName(), "isGoogleReachable : "
                    + isGoogleReachable());
        }

    }).start();;

}
private boolean isGoogleReachable() {
    try {
        if (InetAddress.getByName("www.google.com").isReachable(5000)) {
            return true;
        } else {
            return false;
        }
    } catch (UnknownHostException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        return false;
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        return false;
    }
}

Add this permission:

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

EDIT: Or you could try this:

public static void isNetworkAvailable(Context context){
    HttpGet httpGet = new HttpGet("http://www.google.com");
    HttpParams httpParameters = new BasicHttpParams();
    // Set the timeout in milliseconds until a connection is established.
    // The default value is zero, that means the timeout is not used.
    int timeoutConnection = 3000;
    HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);
    // Set the default socket timeout (SO_TIMEOUT)
    // in milliseconds which is the timeout for waiting for data.
    int timeoutSocket = 5000;
    HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);

    DefaultHttpClient httpClient = new DefaultHttpClient(httpParameters);
    try{
        Log.d(TAG, "Checking network connection...");
        httpClient.execute(httpGet);
        Log.d(TAG, "Connection OK");
        return;
    }
    catch(ClientProtocolException e){
        e.printStackTrace();
    }
    catch(IOException e){
        e.printStackTrace();
    }

    Log.d(TAG, "Connection unavailable");
}

Also see:

Detect if Android device has Internet connection

"should something similar to the above method work?"

Yes, it works fine if Bluetooth is also off.

When you have wifi and data turned off, what is the network type name that activeNetworkInfo.getTypeName() returns?

This might be a bug — can you dump as much info as possible out of the NetworkInfo object (especially the type name, DetailedState enumeration, and so forth) and file it in our issue tracker?

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!