Using JavaScript and Agent API to check if isCharging=TRUE and load Script

DM
Daniel Maniera
OPAL Associates GmbH (Germany)

Hi everyone.

I'm looking for a solution to kill an application when a device is charging.

I found an documentation about using the Agent API in Lockdown ( https://www.soti.net/mc/help/v2024.0/en/console/configurations/profiles/configurations/categories/restrictions/tutorial_JS_lockdown_screen.html?hl=using%2Candroid%2Cagent%2Cjavascript%2Cfrom%2Clockdown%2Cscreen) but i'm not an developer and i do not get it to work.

Someone has an idea?

2 years ago
SOTI MobiControl
ANSWERS
MD
Matt Dermody Diamond Contributor
2 years ago

ChatGPT is very helpful for learning javascript ;) 

RS
Rafael Schäfer Platinum Contributor
2 years ago
DM
Daniel Maniera
2 years ago

Hi Rafael,

i want to insert it into the lockdown. The Script is working fine when i send it to the device.

I build a workaround with Task Scheduler which sends the script:

if (mobicontrol.battery.isCharging) {
    mobicontrol.app.stop('APP_IDENTIFIER');
}

every 15minutes. Which results in exact what i am looking for. At the moment this is just for several devices. But imagine having such kind of Task every few minutes for hundrets of devices. This will result in a lot of load on the server. So i am looking for a solution to trigger a cmd which includes:

#!/usr/bin/env js
if (mobicontrol.battery.isCharging) {
    mobicontrol.app.stop('APP_IDENTIFIER');
}

directly on the device by using the Agent API because i know that it's not possible to trigger a local file from a website directly for security reason.

ZC
Zafer Cigdem Platinum Contributor
2 years ago

Hi Daniel,

I just saw your message after sharing my answer, your above JS code works either using inside of the .cmd/.bat, so do you need any help related to this topic :)

Thanks

AW
Adam Williams Silver Contributor
2 years ago

Hi Daniel,

Others may correct me, however, I dont believe your Task Scheduler option will place additional load on the server. The Task Scheduler payload will be delivered to the devices once and then that timer will act on the device rather than poling the server to re-deliver the script.

ZC
Zafer Cigdem Platinum Contributor
2 years ago

Yeah I agree with Adam, it's what I know as well. 

ZC
Zafer Cigdem Platinum Contributor
2 years ago

Hi Daniel,

To give you some insight you can use below JS and trigger JavaScript script when the device is charging.

Please remember to put extra space after the if and below lines, you can see at below Screenshot as well.


charging = mobicontrol.battery.isCharging
if (charging = 'true') {
mobicontrol.message.createInfoDialog("Battery charging is:"+ mobicontrol.battery.isCharging).show();
//Write here any JS script to execute
//After your test you can comment out Line-3
}

I hope it helps. Thank you

Zafer

S
SGMOD@SOTI
a year ago

Hi Daniel,

Thank you for posting on SOTI Pulse. Thanks Matt, Zafer, Adam and Rafael for responding to the post, your expertise and willingness to help are greatly appreciated!

Have you had an opportunity to test the suggested solutions and has it successfully addressed your query? 

If you have any additional questions or concerns, please don't hesitate to reach out. We're dedicated to providing assistance and support.