Detecting the device being plugged in

Apparently the ACTION_BATTERY_CHANGED is a “sticky broadcast” which means you can register for it and receive it any time after it has been broadcast. To get the plugged state you can do something like: public void onCreate() { BroadcastReceiver receiver = new BroadcastReceiver() { public void onReceive(Context context, Intent intent) { int plugged = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, … Read more

Battery-safe coding

I’ll assume you mean your application. In my experience, the major consumers of energy are, where #1 is most important: CPU usage 4G WiFi Bluetooth Memory Whether 4G or WiFi is worse depends upon your usage, e.g. whether you are talking with a poor signal over your cellular network or are streaming video over your … Read more

Android : Is there anyway to get battery capacity of a device in mah?

Got it! Couldn’t find anything straight in SDK but can be done using reflection. Here is the working code :- public void getBatteryCapacity() { Object mPowerProfile_ = null; final String POWER_PROFILE_CLASS = “com.android.internal.os.PowerProfile”; try { mPowerProfile_ = Class.forName(POWER_PROFILE_CLASS) .getConstructor(Context.class).newInstance(this); } catch (Exception e) { e.printStackTrace(); } try { double batteryCapacity = (Double) Class .forName(POWER_PROFILE_CLASS) .getMethod(“getAveragePower”, … Read more

Get battery level before broadcast receiver responds for Intent.ACTION_BATTERY_CHANGED

This is how to get the battery level without registering a receiver: Intent batteryIntent = context.getApplicationContext().registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED)); int rawlevel = batteryIntent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1); double scale = batteryIntent.getIntExtra(BatteryManager.EXTRA_SCALE, -1); double level = -1; if (rawlevel >= 0 && scale > 0) { level = rawlevel / scale; } It can use a null BroadcastReceiver because of … Read more

How to get the battery level after connect to the BLE device?

I have solved this problem. public class BluetoothLeService extends Service { private static final UUID Battery_Service_UUID = UUID.fromString(“0000180F-0000-1000-8000-00805f9b34fb”); private static final UUID Battery_Level_UUID = UUID.fromString(“00002a19-0000-1000-8000-00805f9b34fb”); public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) { if(status == BluetoothGatt.GATT_SUCCESS) { broadcastUpdate(ACTION_DATA_AVAILABLE, characteristic); } } private void broadcastUpdate(final String action, final BluetoothGattCharacteristic characteristic) { final Intent intent = … Read more

Calculating battery life in iOS

The API allows you to register to receive notifications for changes to the battery level. It only reports a change at 5% increments up or down, but you can use a timer and measure the time between two changes (or initial battery level and first change). Here’s how you register for the notifications: // Use … Read more

Get battery level and state in Android

Here is a code sample that explains how to get battery information. To sum it up, a broadcast receiver for the ACTION_BATTERY_CHANGED intent is set up dynamically, because it can not be received through components declared in manifests, only by explicitly registering for it with Context.registerReceiver(). public class Main extends Activity { private TextView batteryTxt; … Read more