2

I'm developing something that needs to upload a whole load of images, so I think it's better to do it when Glass is both connected to wifi and a battery charger. Also, if it is in idle mode it'll be the best.

Where can I put my image upload code so that this requirement is fulfilled?

mimming
  • 13,974
  • 3
  • 45
  • 74
albusshin
  • 3,930
  • 3
  • 29
  • 57
  • You may need to use an indirect way: 1) monitor battery to see if charge is increasing (idle alarm ?); 2) transfer file and check time / battery impact to decide whether to continue. Sounds like an interesting little service to create that many folks could use. – ErstwhileIII Mar 06 '14 at 15:20
  • Yeah, that's what I thought. I'll check into details of checking the state of Glass. – albusshin Mar 06 '14 at 16:57
  • 1
    all device related stuff on glass is not any different from plain android device. http://developer.android.com/training/monitoring-device-state/battery-monitoring.html and stackoverflow question on wifi listener http://stackoverflow.com/questions/9434235/android-i-want-to-set-listener-to-listen-on-wireless-state-can-anyone-help-me-w – Pavlonator Mar 07 '14 at 03:38

1 Answers1

0

As mentioned in a comment, it should be the same as other Android devices.

You can try this:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Card card = new Card(this);
    card.setText("wifi " + MainActivity.isWiFiConnected(this) + " charger " + MainActivity.isChargerConnected(this));        
    setContentView(card.toView());
    Log.v("TEST", "wifi " + MainActivity.isWiFiConnected(this));
    Log.v("TEST", "charger " + MainActivity.isChargerConnected(this));
    if(MainActivity.isWiFiConnected(this) && MainActivity.isChargerConnected(this)) { 
       // do whatever...
    }
}

public static boolean isWiFiConnected(Context context) {
    ConnectivityManager conMan = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo.State wifi = conMan.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState();
    if (wifi == NetworkInfo.State.CONNECTED || wifi == NetworkInfo.State.CONNECTING)
        return true;
    return false;
}

public static boolean isChargerConnected(Context context) {
    Intent intent = context.registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
    int plugged = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
    return plugged == BatteryManager.BATTERY_PLUGGED_AC || plugged == BatteryManager.BATTERY_PLUGGED_USB;
}

Add

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

in the manifest.

Also, to check that Glass really connects to the internet, please see How to check if Google Glass is connected to internet using GDK.

Community
  • 1
  • 1
pt2121
  • 11,720
  • 8
  • 52
  • 69