react-native-device-battery-info
v0.1.1
Published
A lightweight and easy-to-use React Native module to access the device's battery information. Retrieve current battery percentage, charging state, and subscribe to live updates as the battery level or charging status changes. Works seamlessly on iOS and A
Maintainers
Readme
react-native-device-battery-info
A lightweight and easy-to-use React Native module to access the device's battery information. Retrieve current battery percentage, charging state, and subscribe to live updates as the battery level or charging status changes. Works seamlessly on iOS and Android.
Features
- 🔋 Get current battery level (percentage)
- ⚡ Check charging status
- 📱 Real-time battery status updates
- ⚙️ Native implementation for optimal performance
- 📦 Lightweight with zero dependencies
- 🔄 Automatic cleanup of event listeners
Installation
npm install react-native-device-battery-infoor if you use yarn:
yarn add react-native-device-battery-infoiOS Setup
This package requires CocoaPods to be installed in your React Native project. After installing the package, navigate to the iOS directory and install the pod:
cd ios && pod installMake sure to run pod install whenever you install or update the package.
Android Setup
No additional setup required for Android. The module works out of the box.
Usage
Get Current Battery Status
import { getBatteryStatus } from 'react-native-device-battery-info';
try {
const batteryInfo = await getBatteryStatus();
console.log('Battery Level:', batteryInfo.level); // 0-100
console.log('Is Charging:', batteryInfo.isCharging); // true/false
} catch (error) {
console.error('Failed to get battery status:', error);
}Listen to Battery Status Changes
import { addBatteryStatusListener } from 'react-native-device-battery-info';
// Add listener
const subscription = addBatteryStatusListener((batteryInfo) => {
console.log('Battery status changed:', batteryInfo);
// batteryInfo: { level: number, isCharging: boolean }
});
// Remove listener when component unmounts
subscription.remove();Example with React Hooks
import React, { useEffect, useState } from 'react';
import { getBatteryStatus, addBatteryStatusListener } from 'react-native-device-battery-info';
function BatteryStatus() {
const [batteryLevel, setBatteryLevel] = useState(0);
const [isCharging, setIsCharging] = useState(false);
useEffect(() => {
// Get initial battery status
getBatteryStatus().then((status) => {
setBatteryLevel(status.level);
setIsCharging(status.isCharging);
});
// Subscribe to battery status changes
const subscription = addBatteryStatusListener((status) => {
setBatteryLevel(status.level);
setIsCharging(status.isCharging);
});
// Cleanup subscription
return () => subscription.remove();
}, []);
return (
<View>
<Text>Battery Level: {batteryLevel}%</Text>
<Text>Charging: {isCharging ? 'Yes' : 'No'}</Text>
</View>
);
}API Reference
getBatteryStatus(): Promise
Returns a promise that resolves to an object containing the current battery status:
level: number (0-100) - Current battery level percentageisCharging: boolean - Whether the device is currently charging
addBatteryStatusListener(callback: (status: BatteryStatus) => void): Subscription
Registers a listener for battery status changes:
callback: Function called when battery status changes- Returns a subscription object with a
remove()method
Types
interface BatteryStatus {
level: number; // 0-100
isCharging: boolean; // true/false
}
interface Subscription {
remove: () => void;
}Contributing
See the contributing guide to learn how to contribute to the repository and the development workflow.
License
MIT
Made with create-react-native-library
