Battery Status

The Battery Status component provides access to the Battery Status API, enabling your Progressive Web App to monitor the device's battery level, charging status, and remaining time. This allows you to create power-aware applications that can adapt their behavior based on the device's battery condition.

This component is particularly useful for:

  • Power-saving modes that reduce functionality when battery is low

  • Alerting users before battery-intensive operations

  • Adjusting video/image quality based on battery level

  • Deferring background tasks when battery is low

  • Displaying battery status in system monitoring dashboards

  • Warning users about low battery during important tasks

  • Disabling power-intensive features when battery is critical

  • Optimizing app performance based on charging status

Browser Support

The Battery Status API has limited browser support and is primarily available on Chromium-based browsers. It requires HTTPS for security reasons.

Supported Browsers:

  • Chrome/Edge (Desktop and Android): Full support

  • Opera: Full support

  • Firefox: Removed in version 52 (privacy concerns)

  • Safari: Not supported

circle-exclamation

Usage

Basic Battery Display

Visual Battery Indicator

Power-Saving Mode

Low Battery Warning

Adaptive Video Quality

Battery Monitor Dashboard

Integration with Symfony Live Components

For applications using Symfony UX Live Components, here's an advanced integration example:

Then use the component in your templates:

Parameters

None

Actions

None - This component automatically monitors battery status and dispatches events when it changes.

Targets

None

Events

pwa--battery:updated

Dispatched when the battery status changes or when the controller is initially connected.

Payload:

  • charging (boolean): Whether the battery is currently charging

  • level (number): Battery level from 0 to 1 (multiply by 100 for percentage)

  • chargingTime (number|null): Seconds until battery is fully charged (Infinity if not charging or unknown)

  • dischargingTime (number|null): Seconds until battery is fully discharged (Infinity if charging or unknown)

Example:

pwa--battery:unsupported

Dispatched when the Battery Status API is not supported by the browser.

No payload

Example:

Best Practices

  1. Always provide fallbacks: Not all browsers support the Battery API

  2. Respect user privacy: Don't share battery information with third parties

  3. Use sparingly: Battery information is primarily for power optimization

  4. Test on devices: Battery behavior varies significantly between devices

  5. Handle edge cases: Battery values can be null or Infinity

  6. Don't be intrusive: Subtle power-saving adjustments are better than aggressive changes

  7. Inform users: Tell users when you're adapting behavior based on battery

  8. Combine with other signals: Use battery status alongside other performance indicators

  9. Monitor updates: Battery status can change frequently - be prepared

  10. Graceful degradation: Core functionality should work regardless of battery level

Common Use Cases

Power-Saving Mode Activation

Background Task Scheduling

Critical Operation Warning

Complete Example: Adaptive Application

Troubleshooting

API not supported

Issue: Battery Status API not available

Solution: Always handle the unsupported event and provide fallback behavior:

Values are null or undefined

Issue: Battery properties return null

Cause: Some properties may not be available on all devices

Solution: Always check for null/undefined:

Inaccurate remaining time

Issue: Charging/discharging time estimates are inaccurate

Cause: Time estimates are calculated by the OS and can be unreliable

Solution: Treat time values as estimates, not precise measurements

HTTPS requirement

Issue: Battery API not working on HTTP

Solution: The Battery Status API requires a secure context (HTTPS or localhost)

Browser Compatibility

Browser
Support

Chrome/Edge (Desktop)

βœ“ Full support

Chrome/Edge (Android)

βœ“ Full support

Firefox

βœ— Removed (v52+)

Safari

βœ— Not supported

Opera

βœ“ Full support

triangle-exclamation

Last updated

Was this helpful?