Titanium JIRA Archive
Titanium SDK/CLI (TIMOB)

[TIMOB-15534] iOS: Battery Platform event listener doesn't work after first call

GitHub Issuen/a
TypeBug
PriorityMedium
StatusClosed
ResolutionFixed
Resolution Date2015-01-14T19:22:15.000+0000
Affected Version/sRelease 3.1.3
Fix Version/sRelease 4.0.0
ComponentsiOS
LabelsSupportTeam
ReporterMartin Williamson
AssigneeVishal Duggal
Created2013-10-18T09:32:01.000+0000
Updated2015-03-10T22:30:06.000+0000

Description

Adding the event listener for battery change is never called in ios 7 on iphone 4s and 5. Ti.Platform.getBatteryLevel() reports current battery level from first launch of the app but never gets updated. Functions as expected (gives 1% changes) on android
 
var Battery = function UpdateBatteryLevel() {
   Titanium.Platform.batteryMonitoring = true;

        Titanium.Platform.addEventListener('battery', UpdateBattery);
        Ti.App.addEventListener('UpdateBattery', RefreshBattery);
    

};

function UpdateBattery(e) {
    var battLevel = e.level;
    if (Ti.Platform.osname == 'iphone') {
        if (battLevel > -1) {
            battLevel = battLevel * 100;
        }
    }

    Ti.App.Properties.setDouble('batterylevel', battLevel);
    Ti.App.fireEvent('BatteryUpdated');
    Ti.API.info(new Date() + ' - Battery Level Updated to ' + Ti.App.Properties.getDouble('batterylevel'));
}

function RefreshBattery() {
    var battLevel = Ti.Platform.getBatteryLevel();
    if (Ti.Platform.osname == 'iphone') {
        if (battLevel > -1) {
            battLevel = battLevel * 100;
        }
    }
    Ti.App.Properties.setDouble('batterylevel', battLevel);
    Ti.API.info(new Date() + ' - Battery Level = ' + Ti.App.Properties.getDouble('batterylevel'));
}



module.exports = Battery; 

Comments

  1. gautier pialat 2013-12-17

    Hi, Any workaround possible? Impossible to get the battery status updated ? Many tks. Can you please up/define a priority (We are building an app for divers with photo and video. Dont have the battery level on the diver dasboard on app is a blocking point (due to photo and video use a lot of battery and everything is manage by an overlay in fullScreen)) ?
  2. Tim Fisher 2014-03-27

    Same problem is batteryState - it will only return the true value from the first launch.
       
       setInterval(function(){
       	Titanium.Platform.batteryMonitoring = true;
       	Titanium.App.idleTimerDisabled = (Ti.Platform.batteryState == Ti.Platform.BATTERY_STATE_UNPLUGGED)?false:true;
       	alert(Ti.Platform.batteryState);
       }, 1000 * 5);
       
       
  3. David Smith 2014-04-08

    I'm interested in a fix for this bug as well. In the meantime, here is what I did to work around the issue: // Work-around for TIMOB-15534, toggle batteryMonitoring off then on to get the current batteryState. Ti.Platform.batteryMonitoring = false; Ti.Platform.batteryMonitoring = true; Now Ti.Platform.batteryState seems to be updated. The Ti.Platform battery event only fires once. To work around this issue, I simply created a setInterval timer and checked Ti.Platform.batteryState every 5 seconds or so, toggling batteryMonitoring off then on each time as shown above before reading Ti.Platform.batteryState.
  4. Ian Wu 2014-12-09

    Great tip - thanks David... Any ideas when this will get fixed? It's 3.4.1GA already and we're now in iOS8.1.1 and still not working... Thanks is advanced for getting this fixed soon!
  5. Vishal Duggal 2015-01-14

    Test Case
       var win = Ti.UI.createWindow({backgroundColor:'white'});
       
       var label = Ti.UI.createLabel({
       	top:50,
       	text:'Battery State - Unknkown\nBattery Level - Unknkown'
       })
       
       win.add(label);
       
       var label2 = Ti.UI.createLabel({
       	bottom:50,
       	text:'Battery State - Unknkown\nBattery Level - Unknkown'
       })
       
       win.add(label2);
       
       function UpdateBattery(e) {
       	label.text = 'Battery State - '+e.state+'\nBattery Level - '+e.level;
       }
       
       function UpdateBattery2() {
       	label2.text = 'Battery State - '+Ti.Platform.batteryState+'\nBattery Level - '+Ti.Platform.batteryLevel;
       }
       
       //Enable battery monitoring and add listener
       Ti.Platform.batteryMonitoring = true;
       Ti.Platform.addEventListener('battery',UpdateBattery);
       
       //Check every 3 seconds via properties
       setInterval(UpdateBattery2,3000);
       
       win.open();
       
  6. Vishal Duggal 2015-01-14

    Pull pending master - https://github.com/appcelerator/titanium_mobile/pull/6568
  7. Eric Wieber 2015-03-10

    Verified fixed using: Titanium SDK 4.0.0.v20150306095010 Studio 4.0.0.201502171827 CLI 3.4.2 Xcode 6.2 Was able to see battery events fired multiple times.

JSON Source