Current battery status - Ionic Native

Hi,

I am working on an app where I’d like to be able to query the device for the current battery level. The issue I’m finding is that the battery status plugin only supports subscriptions to certain events, but I can’t seem to find a way to immediately query for the current charge level without waiting for the onChange event.

From my research it appears that Ionic v1 with ngCordova has this type of support. Is there a way to achieve this functionality with Ionic Native in Ionic v2+?

Thanks.

Please provide some links to the plugins that do or do not support what you want.

Sure, it’s the Battery Status plugin used in Ionic Native:


Based on the following article, it appears that it was possible to get the current status in Ionic v1 without waiting for the onChange event to fire: https://www.thepolyglotdeveloper.com/2015/07/monitor-device-battery-status-using-ionic-framework/

Thanks.

And what exactly of the blogpost do you think does not work any more?

I’m not seeing that at all. It looks exactly like the current state of affairs, where you receive notifications when the status changes by 1%. Can you point out the part of that article where there is a synchronous query?

1 Like

I’d prefer to use Ionic v2 or later since I haven’t worked with v1 before. If v1 is still supported, I don’t mind using it for this project though.

In the video walkthrough, the author mentions that it should immediately report the status, and also every time the status is updated (reference: https://youtu.be/nElfreBtoJQ?t=592).

I’m not really familiar with Ionic/Angular v1, but I don’t recognize any synchronous queries, just the following snippet that (from my understanding) gets the battery status on ionicPlatform.ready:

$ionicPlatform.ready(function() {
        $rootScope.$on("$cordovaBatteryStatus:status", function(event, args) {
            if(args.isPlugged) {
                alert("Charging -> " + args.level + "%");
            } else {
                alert("Battery -> " + args.level + "%");
            }
        });
    });

Not the way I read that. That looks to me like it is registering a callback that will be called every time the status changes by 1% or more. It’s the exact same plugin under the hood; I don’t think you will see any fundamental difference using any version of Ionic here. Maybe batteryStatus does fire immediately on app startup (but a quick look at the plugin source does not inspire hope on that front).

I see, thank you very much for the info. I guess I’ll try and find an alternative solution for my use case.

Have you tried just coding it up optimistically and seeing when the status observable actually does fire? At least on Android, it appears that the nature of the battery status change event may be structured in such a way that it will fire immediately upon plugin initialization without the plugin having to do anything.

I have not tried it on an actual device, no, just in a browser via ionic serve —lab. Come to think of it, I don’t think that was a good test though since I’m testing a Native plugin and from what I recall the functionality only works on actual hardware.

My target platform for this app is iOS, but hopefully the same behavior you’re seeing on Android translates to iOS as well. I’ll go ahead and try running the code on an iOS device. Thanks for the suggestion.

Just to chime in and clarify real quick, there are a couple Ionic Native plugins that work in the browser, but the majority of them do not as you recall.

The battery-level plugin needs a method to get the current battery level / status once at call. Maybe with a Promise. Currently you have to unsubscibe to the e.g. onChange observer and subscribe again to get the current level. Otherwise it fires only on value change. This is a nice feature. But sometimes you want the current value.
Ok, you can hold the value in your app in a service wrapper or on another global place, whatever. But I wondering, what’s wrong on a method to get the battery-status once? … Why not? You can! :slight_smile:

Seems like quite a fundamental requirement to me. I also need to be able to (on iOS SPECIFICALLY) report the battery status at the point of starting to use an app. Unfortunately, I cannot wait for the battery status to change, as the state of the battery on the devices being used is so good that waiting for a change can take ages.