This React Native project fetches and displays the battery percentage of the user's device using a native module. The application leverages platform-specific code (Kotlin for Android) to retrieve the battery level, ensuring smooth integration with React Native. Note: iOS functionality has not been implemented or tested yet.
- Fetches the current battery percentage of the device.
- Displays the battery percentage in real-time.
- Refresh button to manually update the battery level.
- Android support only (iOS functionality not implemented).
- Node.js (v14 or above)
- React Native CLI or Expo
- Android Studio (for Android development)
-
Clone the repository:
git clone https://github.com/alizaali9/Battery-Percentage-With-Native-Module cd Battery-Percentage-With-Native-Module -
Install dependencies:
npm install # or yarn install -
Link the native module:
- For React Native 0.60+, auto-linking should take care of this.
- For older versions, link manually:
react-native link
-
Navigate to
android/app/src/main/java/com/<your_project>/. -
Create a file
BatteryModule.ktandBatteryPackage.kt:<---- BatteryModule.kt ----> package com.<your_project> import android.content.Intent import android.content.IntentFilter import android.os.BatteryManager import com.facebook.react.bridge.Promise import com.facebook.react.bridge.ReactApplicationContext import com.facebook.react.bridge.ReactContextBaseJavaModule import com.facebook.react.bridge.ReactMethod class BatteryModule(reactContext: ReactApplicationContext) : ReactContextBaseJavaModule(reactContext) { private val context: ReactApplicationContext = reactContext override fun getName(): String { return "BatteryModule" } @ReactMethod fun getBatteryLevel(promise: Promise) { try { val intentFilter = IntentFilter(Intent.ACTION_BATTERY_CHANGED) val batteryStatus = context.registerReceiver(null, intentFilter) val level = batteryStatus?.getIntExtra(BatteryManager.EXTRA_LEVEL, -1) ?: -1 val scale = batteryStatus?.getIntExtra(BatteryManager.EXTRA_SCALE, -1) ?: -1 val batteryPct = (level / scale.toFloat()) * 100 promise.resolve(batteryPct.toInt()) } catch (e: Exception) { promise.reject("ERROR", e.message) }} }
<---- BatteryPackage.kt ----> package com.<your_project> import com.facebook.react.ReactPackage import com.facebook.react.bridge.NativeModule import com.facebook.react.bridge.ReactApplicationContext import com.facebook.react.uimanager.ViewManager class BatteryPackage : ReactPackage { override fun createNativeModules(reactContext: ReactApplicationContext): List<NativeModule> { return listOf(BatteryModule(reactContext)) } override fun createViewManagers(reactContext: ReactApplicationContext): List<ViewManager<*, *>> { return emptyList() } }
-
Update
MainApplication.ktto include the module.
<---- MainApplication.kt ---->
package com.<your_project>
import android.app.Application
import com.facebook.react.PackageList
import com.facebook.react.ReactApplication
import com.facebook.react.ReactHost
import com.facebook.react.ReactNativeHost
import com.facebook.react.ReactPackage
import com.facebook.react.defaults.DefaultNewArchitectureEntryPoint.load
import com.facebook.react.defaults.DefaultReactHost.getDefaultReactHost
import com.facebook.react.defaults.DefaultReactNativeHost
import com.facebook.soloader.SoLoader
import com.test.BatteryPackage
class MainApplication : Application(), ReactApplication {
override val reactNativeHost: ReactNativeHost =
object : DefaultReactNativeHost(this) {
override fun getPackages(): List<ReactPackage> =
PackageList(this).packages.apply {
add(BatteryPackage())
// Packages that cannot be autolinked yet can be added manually here, for example:
// add(MyReactNativePackage())
}
override fun getJSMainModuleName(): String = "index"
override fun getUseDeveloperSupport(): Boolean = BuildConfig.DEBUG
override val isNewArchEnabled: Boolean = BuildConfig.IS_NEW_ARCHITECTURE_ENABLED
override val isHermesEnabled: Boolean = BuildConfig.IS_HERMES_ENABLED
}
override val reactHost: ReactHost
get() = getDefaultReactHost(applicationContext, reactNativeHost)
override fun onCreate() {
super.onCreate()
SoLoader.init(this, false)
if (BuildConfig.IS_NEW_ARCHITECTURE_ENABLED) {
// If you opted-in for the New Architecture, we load the native entry point for this app.
load()
}
}
}- Import the module in your React Native component:
import { NativeModules } from 'react-native'; const { BatteryModule } = NativeModules; const fetchBatteryLevel = async () => { try { const batteryLevel = await BatteryModule.getBatteryLevel(); console.log(`Battery Level: ${batteryLevel}%`); } catch (error) { console.error('Error fetching battery level:', error); } }; export default function App() { useEffect(() => { fetchBatteryLevel(); }, []); return ( <View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}> <Text>Battery Level:</Text> <Button title="Refresh" onPress={fetchBatteryLevel} /> </View> ); }
- Run the app on Android:
npx react-native run-android
Follow these steps to deploy your app on the Play Store:
- Build a release version of the app:
npx react-native run-android --variant=release
- Sign the APK and upload it to the Play Store.
- Implement and test iOS functionality.
- Add a widget for quick battery level monitoring.
- Include battery health and charging status.
- Optimize for better performance on low-end devices.
Feel free to reach out if you encounter any issues or have suggestions for improvement!