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

后端 未结 2 818
一向
一向 2021-01-20 07:22

Is there a way to detect if Google Glass is connected to the internet at runtime? For instance, I often get the message \"Can\'t reach Google right now\" when using voice in

2条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-20 08:07

    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:

    
    

    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.

提交回复
热议问题