I have developed an Android application that connects via Bluetooth to a gamepad controller and shows button presses, joy stick movements, etc made on the handheld controller on the Android device.
Application and source code are on Github here: https://github.com/portsample/gamecontroller
An elusive goal of this endeavor has been having battery state of the gamepad controller captured by the application and shown on the Android device. This may or may not be possible using the Google Android API for SDK 33 or more recent. Any suggestions or advice would be highly appreciated.
May 3, 2024 Update: This is close,
private int getBatteryLevel() { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { int[] deviceIds = InputDevice.getDeviceIds(); for (int deviceId : deviceIds) { InputDevice inputDevice = InputDevice.getDevice(deviceId); if (inputDevice != null && (inputDevice.getSources() & InputDevice.SOURCE_GAMEPAD) == InputDevice.SOURCE_GAMEPAD) { //float batteryLevel = inputDevice.getBatteryCapacity(); float batteryLevel = inputDevice.getBatteryState(); //float batteryLevel = 27; return (int) (batteryLevel * 100); // Convert battery level to percentage } } } return -1; // Battery level not available } But still no cigar. Error message is,
error: incompatible types: BatteryState cannot be converted to float float batteryLevel = inputDevice.getBatteryState(); How to do this with BatteryState() object and GetCapacity() function?
// batteryLevel = inputDevice.getBatteryState();can't work. Note: The remaining battery capacity is only part of the BatteryState object.