Skip to content

Instantly share code, notes, and snippets.

@uratmangun
Created December 4, 2024 13:01
Show Gist options
  • Save uratmangun/bc0388f9847f44a79d6f73a44373b3a6 to your computer and use it in GitHub Desktop.
Save uratmangun/bc0388f9847f44a79d6f73a44373b3a6 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
def get_battery_info():
try:
# Battery percentage
with open('/sys/class/power_supply/battery/capacity', 'r') as f:
percentage = f.read().strip()
# Charging status
with open('/sys/class/power_supply/battery/status', 'r') as f:
status = f.read().strip()
# Temperature (in tenths of a degree Celsius)
with open('/sys/class/power_supply/battery/temp', 'r') as f:
temp = float(f.read().strip()) / 10
# Current (negative when discharging, positive when charging)
with open('/sys/class/power_supply/battery/current_now', 'r') as f:
current = int(f.read().strip()) / 1000 # Convert to mA
print(f"Battery Status:")
print(f"Percentage: {percentage}%")
print(f"Status: {status}")
print(f"Temperature: {temp}°C")
print(f"Current: {abs(current):.2f} mA ({'charging' if current > 0 else 'discharging'})")
except FileNotFoundError:
print("Error: Cannot access battery information.")
print("Make sure you're running this script on Android with Termux.")
except Exception as e:
print(f"Error: {str(e)}")
if __name__ == "__main__":
get_battery_info()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment