Last active
May 7, 2021 12:59
-
-
Save NilsonLima/5d319f3b6b14e4b68b1924e8461efff2 to your computer and use it in GitHub Desktop.
BatteryTester (getting battery health using native modules and DeviceInfo)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* BatteryManager.js */ | |
import { NativeModules } from 'react-native'; | |
const { BatteryManagerModule } = NativeModules; | |
const BatteryManager = { | |
getHealth: BatteryManagerModule.getHealth, | |
} | |
BatteryManager.Constants = BatteryManagerModule.getConstants(); | |
export default BatteryManager; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* BatteryManagerModule.java */ | |
package digital.pier; | |
import com.facebook.react.bridge.NativeModule; | |
import com.facebook.react.bridge.ReactApplicationContext; | |
import com.facebook.react.bridge.ReactContext; | |
import com.facebook.react.bridge.ReactContextBaseJavaModule; | |
import com.facebook.react.bridge.ReactMethod; | |
import java.util.Map; | |
import java.util.HashMap; | |
import android.os.BatteryManager; | |
public class BatteryManagerModule extends ReactContextBaseJavaModule { | |
BatteryManagerModule(ReactApplicationContext context) { | |
super(context); | |
} | |
@Override | |
public String getName() { | |
return "BatteryManagerModule"; | |
} | |
@Override | |
public Map<String, Object> getConstants() { | |
final Map<String, Object> constants = new HashMap<>(); | |
constants.put("BATTERY_HEALTH_DEAD", BatteryManager.BATTERY_HEALTH_DEAD); | |
constants.put("BATTERY_HEALTH_GOOD", BatteryManager.BATTERY_HEALTH_GOOD); | |
constants.put("BATTERY_HEALTH_OVERHEAT", BatteryManager.BATTERY_HEALTH_OVERHEAT); | |
constants.put("BATTERY_HEALTH_OVER_VOLTAGE", BatteryManager.BATTERY_HEALTH_OVER_VOLTAGE); | |
constants.put("BATTERY_HEALTH_UNKNOWN", BatteryManager.BATTERY_HEALTH_UNKNOWN); | |
constants.put("BATTERY_HEALTH_UNSPECIFIED_FAILURE", BatteryManager.BATTERY_HEALTH_UNSPECIFIED_FAILURE); | |
return constants; | |
} | |
@ReactMethod(isBlockingSynchronousMethod = true) | |
public void getHealth() { | |
IntentFilter filter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED); | |
Intent status = context.registerReceiver(null, filter); | |
return batteryStatus.getIntExtra(BatteryManager.EXTRA_HEALTH, 0); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* BatteryTester.android.js */ | |
import BatteryManager from 'api/android/BatteryManager'; | |
import useAsyncCallback from 'hooks/useAsyncCallback'; | |
function BatteryTester() { | |
const { data, loading } = useAsyncCallback(BatteryManager.getHealth, { immediate: true }); | |
if (loading) return /* loading view */; | |
switch (data) { | |
case BatteryManager.Constants.BATTERY_HEALTH_GOOD: | |
return /* test succeded */; | |
default: | |
return /* test failed */; | |
} | |
} | |
export default BatteryTester; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* BatteryTester.default.js */ | |
import { usePowerState } from 'react-native-device-info'; | |
const POWER_STATES = [ | |
CHARGING: 'charging', | |
FULL: 'full', | |
UNPLUGGED: 'unplugged', | |
UNKNOWN: 'unknown', | |
]; | |
function BatteryTester() { | |
const state = usePowerState(); | |
switch (state) { | |
case POWER_STATES.CHARGING: | |
return /* test succeeded */; | |
case POWER_STATES.UNPLUGGED: | |
case POWER_STATES.FULL: | |
return /* test still on holding */; | |
default: | |
return /* should fail test */; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment