@dderevjanik/termux-api
Version:
This library allows you to interact with your Android device from Node.js using termux-api
60 lines (52 loc) • 1.22 kB
text/typescript
import { exec } from "child_process";
export interface BatteryStatus {
/**
* The health of the battery.
* @example "GOOD"
*/
health: string;
/**
* The percentage of battery remaining.
* @example 73
*/
percentage: number;
/**
* The plugged status of the battery.
* @example "UNPLUGGED"
*/
plugged: string;
/**
* The charging status of the battery.
* @example "DISCHARGING"
*/
status: string;
/**
* The temperature of the battery in degrees Celsius.
* @example 21.0
*/
temperature: number;
/**
* The current battery current in microamperes.
* @example 691537
*/
current: number;
}
/**
* Get the status of the device battery
*/
export async function batteryStatus(): Promise<BatteryStatus> {
return new Promise<BatteryStatus>((resolve, reject) => {
const command = `termux-battery-status`;
exec(command, (error, stdout, stderr) => {
if (error) {
return reject(`Error: ${error.message}`);
}
if (stderr) {
return reject(`Error: ${stderr}`);
}
const output = stdout.trim();
const battery: BatteryStatus = JSON.parse(output);
return resolve(battery);
});
});
}