bsorrentino
4/12/2017 - 12:44 PM

Battery Mode using Cordova Broadcaster Plugin

Battery Mode using Cordova Broadcaster Plugin

;(function() {

console.log( "INIT TEST!!");

function onReady() {

  window.broadcaster.addEventListener( "powerStateDidChange", function( e ) {
    console.log( "powerStateDidChange", e );
  });

  cordova.plugins.broadcasterTest.startMonitoringPowerState(
    function(initState) {

      console.log( "START MONITOR POWER STATE!", JSON.stringify(initState) );
    },
    function(e) {
      console.log( "ERROR STARTING MONITOR POWER STATE ", e);

    }
  )
}

document.addEventListener('deviceready', onReady, false);
})();
/********* broadcasterTest.m Cordova Plugin Implementation *******/

#import <Cordova/CDV.h>

@interface broadcasterTest : CDVPlugin {
  // Member variables go here.
}

- (void)startMonitoringPowerState:(CDVInvokedUrlCommand*)command;
@end

@implementation broadcasterTest

-(NSDictionary *)isLowPowerModeEnabled {
    
    BOOL state = ([[NSProcessInfo processInfo] isLowPowerModeEnabled]) ;
    
    return @{ @"lowPower":[NSNumber numberWithBool:state] };
}

- (void)startMonitoringPowerState:(CDVInvokedUrlCommand*)command
{
    CDVPluginResult* pluginResult = nil;

    
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector: @selector(onPowerStateDidChange:)
                                                 name: NSProcessInfoPowerStateDidChangeNotification
                                               object: nil];

    pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:[self isLowPowerModeEnabled]];

    [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
    
}

- (void)onPowerStateDidChange:(NSNotification *)notification {
    
    [[NSNotificationCenter defaultCenter] postNotificationName:@"powerStateDidChange"
                                                        object:nil
                                                      userInfo:[self isLowPowerModeEnabled]];
}



@end
var exec = require('cordova/exec');

exports.startMonitoringPowerState = function(success, error) {
    exec(success, error, "broadcasterTest", "startMonitoringPowerState", []);
};