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 input in my app. Instead, I would like to preemptively intercept the condition that would cause that message and use default values rather than ask for voice input. After searching for a while, the only thing I could find was a solution to the same question for Android in general:
private boolean isConnected() {
ConnectivityManager connectivityManager
= (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
return activeNetworkInfo != null && activeNetworkInfo.isConnected();
}
I tried using this for my Glassware but it doesn't seem to work (I turned off the wifi and data but isConnected() still returns true even though I get the "Can't reach Google right now" message). Does anyone know if the GDK has a way to do this? Or should something similar to the above method work?
EDIT: Here's my eventual solution, based partially on the answer by EntryLevelDev below.
I had to use a background thread to use HTTP GET requests to avoid getting a NetworkOnMainThreadException, so I decided to have it run every few seconds and update a local isConnected variable:
public static boolean isConnected = false;
public boolean isDeviceConnectedToInternet() {
return isConnected;
}
private class CheckConnectivityTask extends AsyncTask<Void, Boolean, Boolean> {
protected Boolean doInBackground(Void... voids) {
while(true) {
// Update isConnected variable.
publishProgress(isConnected());
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
/**
* Determines if the Glassware can access the internet.
* isNetworkAvailable() is used first because there is no point in executing an HTTP GET
* request if ConnectivityManager and NetworkInfo tell us that no network is available.
*/
private boolean isConnected(){
if (isNetworkAvailable()) {
HttpGet httpGet = new HttpGet("http://www.google.com");
HttpParams httpParameters = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParameters, 3000);
HttpConnectionParams.setSoTimeout(httpParameters, 5000);
DefaultHttpClient httpClient = new DefaultHttpClient(httpParameters);
try{
Log.d(LOG_TAG, "Checking network connection...");
httpClient.execute(httpGet);
Log.d(LOG_TAG, "Connection OK");
return true;
}
catch(ClientProtocolException e){
e.printStackTrace();
}
catch(IOException e){
e.printStackTrace();
}
Log.d(LOG_TAG, "Connection unavailable");
} else {
// No connection; for Glass this probably means Bluetooth is disconnected.
Log.d(LOG_TAG, "No network available!");
}
return false;
}
private boolean isNetworkAvailable() {
ConnectivityManager connectivityManager
= (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
Log.d(LOG_TAG, String.format("In isConnected(), activeNetworkInfo.toString(): %s",
activeNetworkInfo == null ? "null" : activeNetworkInfo.toString()));
return activeNetworkInfo != null && activeNetworkInfo.isConnected();
}
protected void onProgressUpdate(Boolean... isConnected) {
DecisionMakerService.isConnected = isConnected[0];
Log.d(LOG_TAG, "Checking connection: connected = " + isConnected[0]);
}
}
To start it, call new CheckConnectivityTask().execute();
(probably from onCreate()). I also had to add these to my Android.manifest:
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />