diff --git a/android/src/main/java/com/remobile/batteryStatus/BatteryListener.java b/android/src/main/java/com/remobile/batteryStatus/BatteryListener.java index 7b86b60..d69b5e3 100755 --- a/android/src/main/java/com/remobile/batteryStatus/BatteryListener.java +++ b/android/src/main/java/com/remobile/batteryStatus/BatteryListener.java @@ -127,17 +127,19 @@ private void removeBatteryListener() { * @return a JSONObject containing the battery status information */ private JSONObject getBatteryInfo(Intent batteryIntent) { + // Are we charging / charged? int status = batteryIntent.getIntExtra(BatteryManager.EXTRA_STATUS, -1); boolean isCharging = status == BatteryManager.BATTERY_STATUS_CHARGING || status == BatteryManager.BATTERY_STATUS_FULL; JSONObject obj = new JSONObject(); try { - obj.put("level", batteryIntent.getIntExtra(android.os.BatteryManager.EXTRA_LEVEL, 0)); - obj.put("isCharging", isCharging); + obj.put ("isCharging", isCharging); + obj.put ("level", batteryIntent.getIntExtra(android.os.BatteryManager.EXTRA_LEVEL, 0)); // a new JSONObject() } catch (JSONException e) { LOG.e(LOG_TAG, e.getMessage(), e); } + System.out.println(obj); return obj; } @@ -149,9 +151,11 @@ private JSONObject getBatteryInfo(Intent batteryIntent) { */ private void updateBatteryInfo(Intent batteryIntent) { JSONObject info = this.getBatteryInfo(batteryIntent); - LOG.v(LOG_TAG, info.toString()); + WritableMap params = Arguments.createMap(); try { - this.sendJSEvent("BATTERY_STATUS_EVENT", JsonConvert.jsonToReact(info)); + params.putString("level", info.getString("level")); + params.putBoolean("isCharging", info.getBoolean("isCharging")); + this.sendJSEvent("BATTERY_STATUS_EVENT", params); } catch (JSONException e) { LOG.e(LOG_TAG, e.getMessage(), e); } diff --git a/libs/battery.js b/libs/battery.js index f421868..776369d 100644 --- a/libs/battery.js +++ b/libs/battery.js @@ -33,12 +33,12 @@ let subscription = null; exports.register = function (options) { let _level = null; - let _isPlugged = null; + let _isCharging = null; const { onBatteryStatus, onBatteryLow, onBatteryCritical } = options; if (onBatteryStatus || onBatteryLow || onBatteryCritical) { - subscription = EventEmitter.addListener('BATTERY_STATUS_EVENT', (info) => { + subscription = EventEmitter.addListener('BATTERY_STATUS_EVENT', info => { if (info) { - if (_level !== info.level || _isPlugged !== info.isPlugged) { + if (_level !== info.level || _isCharging !== info.isCharging) { if (info.level === null && _level !== null) { return; // special case where callback is called because we stopped listening to the native side. } @@ -46,7 +46,7 @@ exports.register = function (options) { // Something changed. Fire batterystatus event onBatteryStatus && onBatteryStatus(info); - if (!info.isPlugged) { // do not fire low/critical if we are charging. issue: CB-4520 + if (!info.isCharging) { // do not fire low/critical if we are charging. issue: CB-4520 // note the following are NOT exact checks, as we want to catch a transition from // above the threshold to below. issue: CB-4519 if (_level > STATUS_CRITICAL && info.level <= STATUS_CRITICAL) { @@ -58,7 +58,7 @@ exports.register = function (options) { } } _level = info.level; - _isPlugged = info.isPlugged; + _isCharging = info.isCharging; } } });