External Battery
  • 19 Dec 2023
  • 3 Minutes to read
  • Contributors
  • Dark
    Light

External Battery

  • Dark
    Light

Article Summary

Understanding the interactions with the external battery is essential for developers. The Vuzix M400 and M4000 require an external battery for normal operation. These devices support true hot-swapping because there is a small internal battery that allows the external battery to be completely removed for a short time without any negative impact to the user.

Any battery capable of supplying of 1.5 amps will be able to reliably power the M400 and M4000. When the external battery becomes fully depleted, a message will appear on the screen indicating that the operator has a limited time to swap the battery before the device powers-down. If the user replaces the external battery before this time, they can operate indefinitely.

The Vuzix 478T0A001 Power Bank not only provides adequate power, it also allows the M400 or M4000 to read its voltage, providing an easy way for users to know the time they have left before hot swapping is required.

In this guide you will learn how to register for the system broadcasts related to the battery and read the power level of the 478T0A001 Power Bank from your apps.


Creating the External Battery Status Receiver

Within your application, create a Java class that extends the BroadcastReceiver class.

package com.vuzix.sample.externalbattery;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class ExternalBatteryStatusReceiver extends BroadcastReceiver {
	@Override public void onReceive(Context context, Intent intent) {
	}
}

After, register your new ExternalBatteryStatusReceiver with your app's manifest file. You will need to create an intent-filter with an action named
android.intent.action.ACTION_POWER_CONNECTED
android.intent.action.ACTION_POWER_DISCONNECTED


If the user is using the Vuzix 478T0A001 Power Bank we can read additional information from the battery:
com.vuzix.action.EXTERNAL_BATTERY_CHANGED

<receiver android:name=".ExternalBatteryStatusReceiver"
	android:exported="true">
	<intent-filter>
		<action android:name="android.intent.action.ACTION_POWER_CONNECTED" />
		<action android:name="android.intent.action.ACTION_POWER_DISCONNECTED" />
		<!-- For reading extra data provided by the Vuzix 478T0A001 Power Bank -->
		<action android:name="com.vuzix.action.EXTERNAL_BATTERY_CHANGED" />
	</intent-filter>
</receiver>


Creating the External Battery Status Listener

We are going to create an interface named ExternalBatteryStatusListener that will describe a listener that will receive updates on when the status of the external battery changes.

Create an interface called ExternalBatteryStatusListener and declare two methods called onExternalBatteryStatusChanged and onVuzixExternalBatteryStatusChanged that takes contains the parameters as listed below.

public interface ExternalBatteryStatusListener {
	/** * Called when an external battery change broadcast is received *
	@param charging Boolean for if the device is currently charging *
	@param chargeMethod ChargeMethod for the current way the device is receiving power, None if *
	not charging*/
	void onExternalBatteryStatusChanged(boolean charging, ExternalBatteryStatusReceiver.ChargeMethod chargeMethod);
	/** * Called when an external battery change broadcast is received *
	@param connected The connection status of the external battery *
	@param capacity The capacity of the external battery *
	@param voltage The voltage of the external battery *
	@param current The current of the external battery */
	void onExternalBatteryStatusChanged(boolean connected, int capacity, int voltage, int current); }


Registering the External Battery Status Listener

We are now going to go back to the ExternalBatteryStatusReceiver class and implement the required methods for registering and unregistering an ExternalBatteryStatusListener. Add the follow variable and methods.

private ExternalBatteryStatusListener externalBatteryStatusListener;
public void registerExternalBatteryListener(ExternalBatteryStatusListener listener) {
	externalBatteryStatusListener = listener;
}
public void unregisterExternalBatteryListener() {
	if (externalBatteryStatusListener != null) {
		externalBatteryStatusListener = null;
	}
}

Now, in our onReceive method we are going to send our external battery status updates to the ExternalBatteryStatusListener. Below are the default values and intent extras referenced in the onRecieve mothod. Place these values in your ExternalBatteryReceiver class. We also have an enum named ChargedMethod that we will use to tell our listeners what type of charge method is currently charging the device. The options will be None, USB and AC. Wireless charging is a fourth possibility in the BatteryManager class, however, Vuzix Smart Glasses do not support wireless charging.

private final String EXTRA_VUZIX_EXTBAT_CONNECTED = "connected";
private final String EXTRA_VUZIX_EXTBAT_CAPACITY = "capacity";
private final String EXTRA_VUZIX_EXTBAT_VOLTAGE = "voltage";
private final String EXTRA_VUZIX_EXTBAT_CURRENT = "current";
enum ChargeMethod {None, USB, AC} // Vuzix Smart Glasses do not support wireless charging private static final int DEFAULT_CAPACITY = 0;
private static final int DEFAULT_VOLTAGE = 0;
private static final int DEFAULT_CURRENT = 0;


Receiving the onExternalBatteryStatusChanged Updates

On our MainActivity we're going to extend the ExternalBatteryStatusListener so we can receive onExternalBatteryStatusChanged updates. After implementing the required method we need to register an intent filter so we can listen for updates to the external battery. Ensure your MainActivity looks like the following code sample. You will want to register the intent filters with the following actions:

Intent.ACTION_BATTERY_CHANGED
com.vuzix.action.EXTERNAL_BATTERY_CHANGED

public class MainActivity extends AppCompatActivity implements ExternalBatteryStatusListener{
	ExternalBatteryStatusReceiver receiver;
	@Override protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		receiver = new ExternalBatteryStatusReceiver();
		IntentFilter intentFilter = new IntentFilter();
		intentFilter.addAction(Intent.ACTION_BATTERY_CHANGED);
		intentFilter.addAction("com.vuzix.action.EXTERNAL_BATTERY_CHANGED");
		registerReceiver(receiver, intentFilter);
		receiver.registerExternalBatteryListener(this);
	}
}

Don't forget to unregister your ExternalBatteryStatusReceiver and receiver in your onDestroy().

@Override protected void onDestroy() {
	receiver.unregisterExternalBatteryListener();
	unregisterReceiver(receiver);
	super.onDestroy();
}


Congratulations! You can now register and receive external battery status updates.

You will now be able to receive updates in your MainActivity class through the onExternalBatteryStatusChanged and onVuzixExternalBatteryStatusChanged methods.


Sample Project

A sample project for Android Studio can be downloaded here.


Was this article helpful?

What's Next