Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Adobe AIR Development

Reply
Developer
Posts: 124
Registered: ‎01-22-2011
My Device: Blackberry Q10
My Carrier: Sprint

Battery Level/Status in Simulator

I'm implementing a battery monitor in my app to inform the user of low battery and to save state in the event of low battery.  The code I'm using looks like:

 

		private function monitorBatteryLevel():void{
			device.batteryMonitoringEnabled = true;
			app.addEventListener(DeviceBatteryEvent.LEVEL_CHANGE, batteryLevelChange);
			app.addEventListener(DeviceBatteryEvent.STATE_CHANGE, batteryStateChange);
			trace ("Battery State: " + device.batteryState);
			trace ("Battery Level: " + device.batteryLevel);
		}
		
		private function batteryStateChange(event:DeviceBatteryEvent):void{
			switch (device.batteryState){
				case DeviceBatteryState.CHARGING:
                                            trace ("Battery State: Charging");
					break;
				case DeviceBatteryState.FULL:
                                            trace ("Battery State: Full");
					break;
				case DeviceBatteryState.UNKNOWN:
                                            trace ("Battery State: Unknown");
					break;
				case DeviceBatteryState.UNPLUGGED:
                                            trace ("Battery State: Unplugged");
					break;
			}
		}
		
		private function batteryLevelChange(event:DeviceBatteryEvent):void{
			trace ("Battery level: " + device.batteryLevel);
		}

 

Running this on my simulator, Battery State always returns a value of 2 and Battery Level always returns 0.

 

Is this what others are seeing?  If so, are people submitting apps with battery monitoring without being able to test the events?

Developer
Posts: 2,462
Registered: ‎11-04-2010
My Device: Bold 9700

Re: Battery Level/Status in Simulator

hey wingflap,

 

im gonna take a shot in the dark here and say that the battery readings you are getting are not going to be accurate at all. since this isnt the actual device nor is there a way to adjust what the battery is via the simulator for testing, i dont think we are able to test out the battery stuff via the API.

J. Rab (Blog) (Twitter)
--
1. If you liked my post or found it useful please click on the thumbs up and provide a Like!
2. If my post solved your problem please click on the Accept as Solution button. Much appreciated!

Approved Apps: OnTrack | ssShots | Hangman
Developer
Posts: 6,473
Registered: ‎12-08-2010
My Device: PlayBook, Z10
My Carrier: none

Re: Battery Level/Status in Simulator

You could always set up a temporary test fixture, with a "mock" Device object, and simulate the appropriate values and events.  There's no reason not to test something like this, though it takes a small bit of work to do it.

 

You could also use conditional compilation to enable it so you don't need to take it out in your release version.


Peter Hansen -- (BB10 and dev-related blog posts at http://peterhansen.ca.)
Author of White Noise and Battery Guru for BB10 and for PlayBook | Get more from your battery!
Developer
Posts: 124
Registered: ‎01-22-2011
My Device: Blackberry Q10
My Carrier: Sprint

Re: Battery Level/Status in Simulator

I had been assuming that since I'm on a laptop, the simulator would be able to react to battery events.  Are people handling saving state just based on a deactivation event?  Will my app be not approved if it does not monitor battery state/level?  Does QNX fire an app deactivation just prior to battery drain that I can rely on for saving state?

Developer
Posts: 6,473
Registered: ‎12-08-2010
My Device: PlayBook, Z10
My Carrier: none

Re: Battery Level/Status in Simulator

Nothing much has been documented about what exactly would happen when the system runs out of battery.  It would be reasonable to expect the active app to get a deactivation event, but they haven't said if that will happen yet.

 

I did use only the deactivation event, and ignored the battery level... as long as they aren't providing complete docs and testing support in the simulator, I'm going to wait until I have an actual PlayBook before doing anything that fancy.


Peter Hansen -- (BB10 and dev-related blog posts at http://peterhansen.ca.)
Author of White Noise and Battery Guru for BB10 and for PlayBook | Get more from your battery!
Developer
Posts: 1,003
Registered: ‎01-16-2011
My Device: PlayBook (sim)
My Carrier: Rogers

Re: Battery Level/Status in Simulator

You definitely won't get rejected because of it.

I'm hoping to add all that in once get an actual device.
- If you like my response/post, or it helped you find an answer you were looking for, please provide a Kudo - white star to the bottom right of this post. -
- Please use the search bar at the top, or check out the PlayBook FAQ's for help getting started -
- Hockey DrillBook -
Contributor
Posts: 22
Registered: ‎02-04-2011
My Device: PlayBook
My Carrier: o2

Re: Battery Level/Status in Simulator

[ Edited ]

I created small solution which allows to test/simulate the Battery Management.

 

Check out www.patrick-heinzelmann.de/labs/playbookdevicesimulation

------------------------------------------------------------------------------------------------------------
Check out PlayBook Device Simulation and Last.fm ON AIR for the PlayBook
Developer
Posts: 124
Registered: ‎01-22-2011
My Device: Blackberry Q10
My Carrier: Sprint

Re: Battery Level/Status in Simulator

I'm going to just stick with the deactivation event.  I just wanted to make sure that people were not being rejected for not including funcionality that can't be tested (although I went way out on a limb with reacting to and resizing based on orientation change). 

 

@patrick,

 

Last night, I stumbled across a post where you introduced your simulator.  It looks awesome and I'll be giving it a try soon.  Thanks for the incredible contribution!

 

@the-rest-of-you,

Thanks for the quick and meaningful responses.  It's greatly appreciated.

Developer
Posts: 6,473
Registered: ‎12-08-2010
My Device: PlayBook, Z10
My Carrier: none

Re: Battery Level/Status in Simulator

Just to err on the side of caution here, can someone who's app has been approved and who did NOT do anything with battery monitoring just mention it?  My app has not been approved yet, so what I did or didn't do in this area is meaningless as far as that's concerned...


Peter Hansen -- (BB10 and dev-related blog posts at http://peterhansen.ca.)
Author of White Noise and Battery Guru for BB10 and for PlayBook | Get more from your battery!
Developer
Posts: 2,462
Registered: ‎11-04-2010
My Device: Bold 9700

Re: Battery Level/Status in Simulator

i just want to add that i did not utilize the deactivate, activate, or battery levels. my program didnt require it because the data is saved after every action that requires it. there is not overhead for this and is done in the background. so i didnt see a need for it. i dont think they'd reject an app because it doesnt include stuff like that.

J. Rab (Blog) (Twitter)
--
1. If you liked my post or found it useful please click on the thumbs up and provide a Like!
2. If my post solved your problem please click on the Accept as Solution button. Much appreciated!

Approved Apps: OnTrack | ssShots | Hangman