[TIMOB-15534] iOS: Battery Platform event listener doesn't work after first call
GitHub Issue | n/a |
---|---|
Type | Bug |
Priority | Medium |
Status | Closed |
Resolution | Fixed |
Resolution Date | 2015-01-14T19:22:15.000+0000 |
Affected Version/s | Release 3.1.3 |
Fix Version/s | Release 4.0.0 |
Components | iOS |
Labels | SupportTeam |
Reporter | Martin Williamson |
Assignee | Vishal Duggal |
Created | 2013-10-18T09:32:01.000+0000 |
Updated | 2015-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;
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)) ?
Same problem is batteryState - it will only return the true value from the first launch.
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.
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!
Test Case
Pull pending master - https://github.com/appcelerator/titanium_mobile/pull/6568
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.