EasyCord LogoDocs
Plugin EcosystemHardware & Sensors

Battery

Monitor device battery level and charging state.

Battery Plugin

The Battery plugin lets you query the current battery status of the device, enabling your app to adjust its power usage or warn the user when the battery is critically low.

Installation

npm install @easycord/plugin-battery

API Reference

getBatteryInfo()

Returns the current battery level and whether the device is plugged in.

Returns

  • level (number): A float between 0.0 and 1.0 representing the battery percentage.
  • isCharging (boolean): True if the device is currently receiving power.

Example Usage

import { useState, useEffect } from 'react';
import { Battery, BatteryInfo } from '@easycord/plugin-battery';

export default function BatteryStatus() {
  const [info, setInfo] = useState<BatteryInfo | null>(null);

  useEffect(() => {
    Battery.getBatteryInfo().then(setInfo);
    
    // Optional: add listener for battery changes
    const listener = Battery.addListener('batteryChange', (newInfo) => {
      setInfo(newInfo);
    });

    return () => { listener.remove(); };
  }, []);

  if (!info) return <div>Loading battery info...</div>;

  return (
    <div className="p-4 border rounded shadow-sm w-64">
      <h3 className="font-bold text-lg mb-2">Battery Status</h3>
      <p>Level: {(info.level * 100).toFixed(0)}%</p>
      <p>Charging: {info.isCharging ? 'Yes ⚡' : 'No'}</p>
    </div>
  );
}