It is currently Sun Jun 24, 2018 10:45 pm

All times are UTC - 8 hours [ DST ]




 Page 1 of 4 [ 53 posts ]  Go to page 1, 2, 3, 4  Next
Author Message
 Post subject: Phone functions?
PostPosted: Sat Mar 16, 2013 10:42 pm 

Joined: Wed Aug 24, 2011 12:10 am
Posts: 31
Hi,
I was wondering if there is a way that we could use Andre to perform actions like turning on or off wifi, the security lock, etc. through the use of buttons that use scripts. It would be handy to be able to turn on wifi and join my home network with the press of a button from within my remote configuration or disable the lock when Andre is active and detecting that I am on my network.
I find that my phone struggles to run Tasker, Andre and my media player at the same time, so if I could do it all with Andre, that would be a plus.
Thanks!


 Profile  
 
 Post subject: Re: Phone functions?
PostPosted: Mon Mar 18, 2013 11:47 am 
Site Admin

Joined: Wed Aug 17, 2011 4:55 pm
Posts: 39
There's no native, easy way to do this right now, but you have options. I may eventually add simple device control (Wifi, screen brightness, volume levels) as Parameters (and therefore direct script calls as well) but for now you have two options.

The first is Tasker, which of course gives you the power of controlling pretty much everything on the device. You'd have to set up your desired Task in Tasker, and trigger it via Android Intent. This isn't easy to do right now, since you have to understand Tasker's intent format and build it up as an Andre Parameter, which it's not obvious. I'm considering making this a lot easier by creating a Tasker-specific Parameter/method so you can just fill in the fields.

The second way is to leverage Java directly through script. The JavaScript engine I'm using actually blends both native Java and JavaScript together. You can do almost *anything* an app can do in native Java this way. However this stuff is pretty advanced, and successfully using it can be tricky; not only do you have to be comfortable with Java, but there are some quirks introduced by being blended with JavaScript. Here's an example to turn off WiFi, which you can drop straight into an Andre script and it should work:

Andre.getSystemService( android.content.Context.WIFI_SERVICE ).setWifiEnabled( false );



"Andre" is a pre-defined variable reference to the Android Activity itself, which is used to get system services like WiFi or Audio. Also note the interpreter doesn't have explicit imports for all packages, so it's a god idea to give the full path when referencing classes and their values (e.g. android.content.Context.WIFI_SERVICE instead of just trying Context.WIFI_SERVICE).

For more complex use of this, you often end up having to tinker with syntax and formatting because of Java/JavaScript collision, but in essence you can accomplish anything if you know the Android APIs.


 Profile  
 
 Post subject: Re: Phone functions?
PostPosted: Mon Mar 18, 2013 4:34 pm 

Joined: Wed Aug 24, 2011 12:10 am
Posts: 31
I'm pretty clueless at most programming languages (the only one I have a very basic knowledge in is Lua), but thanks for the snippet. I'll see what I can do with it.
One day I'd like to eliminate Tasker, if possible.


 Profile  
 
 Post subject: Re: Phone functions?
PostPosted: Tue Apr 16, 2013 7:19 am 

Joined: Wed Aug 24, 2011 12:10 am
Posts: 31
Okay, so this works great after pasting this in a script that I created through the settings-> tools menu (I think Tools should be under the "more" menu, though -- more intuitive, I'd say).
I have a few questions, if you don't mind helping me out again.

1. Can this script be modified to toggle the wifi and return a variable which would then be used to change the appearance of the button to reflect the wifi status? Can you spoon-feed me the instructions on how to do so? ;)

2. Is the script stored with my configuration file for easy backup?

3. If I upload a button to the gallery, will the associated script be included?

Thanks!


 Profile  
 
 Post subject: Re: Phone functions?
PostPosted: Fri Apr 19, 2013 1:07 pm 
Site Admin

Joined: Wed Aug 17, 2011 4:55 pm
Posts: 39
#1: Sure this should be pretty easy, here's how:

Create a two-state (on/off) button; you'll find one already configured in the Entity Gallery.

Create a script called 'Toggle Wifi' with the following code:

if ( Andre.getSystemService( android.content.Context.WIFI_SERVICE ).isWifiEnabled() ) {
   Andre.getSystemService( android.content.Context.WIFI_SERVICE ).setWifiEnabled( false );
   andreCommands.changeState( null, andreButton, "off" );
} else {
   Andre.getSystemService( android.content.Context.WIFI_SERVICE ).setWifiEnabled( true );
   andreCommands.changeState( null, andreButton, "on" );
}


Make the two-state button call this script from both states (e.g. create a Call Script parameter in the 'default' state that calls the above script ID). It'll determine current wifi state, toggle it to the opposite state and change the button appropriately.

The only issue is that the button won't reflect wifi state if it has changed outside of Andre. You can do a tiny bit more work to fix this. Assuming your WiFi toggle button has an ID of "ToggleWifiBtn", create a second script called 'Sync Wifi state' with the following code:

if ( Andre.getSystemService( android.content.Context.WIFI_SERVICE ).isWifiEnabled() ) {
   andreCommands.changeState( "_all_", "ToggleWifiBtn', "on" );
} else {
   andreCommands.changeState( "_all_", "ToggleWifiBtn', "off" );
}


Create an invisible button anywhere that calls this script. In "Remote Settings", trigger this button under "Resume Button Trigger". This will make sure every time you turn on the device (or return to Andre from another app) it will test the WiFi state and set all buttons called 'ToggleWifiBtn' correctly. If you have multiple copies of this button (e.g. on several different pages) you could also call the second script right after the first which will sync all buttons together whenever one of the buttons is pressed.

#2: Yes, every single aspect of your configuration gets exported to XML, including all extras (javascript scripts & TCP scripts, sound file mappings, etc.).

#3: When you store an Entity into the local Gallery, it just saves the Entity itself. However, when you publish an Entity from the local Gallery to the Community Gallery (for everyone else), you choose scripts and other stuff to bundle with it so it's self contained.

Let me know how it goes. If you don't get around to it, I may try myself to create a nice example of this button and script and publish it to the Community Gallery for everyone to use.


 Profile  
 
 Post subject: Re: Phone functions?
PostPosted: Tue Apr 23, 2013 6:52 am 

Joined: Wed Aug 24, 2011 12:10 am
Posts: 31
Thanks for all of your help!
I've followed your instructions, but I'm getting an error from the second script on line 2 that says that I'm missing a ")" around the arguments.

While we're at it, can we change parameters of that button to reflect connectivity? Ie.- change the tint of the button or the image if connected to a certain network? The reason I ask its because I currently have no WAN access at my place and I have buttons that I use with my Vera2 over my LAN. When I arrive home, I sometimes like to connect from outside and use Andre to trigger a task on my Elk alarm panel through the Vera to momentarily activate a relay that buzzes the front door open. I would like to hit a button to activate wifi and then see when I've connected via that button so that I know when I am able to use the door button.

Thanks again.


 Profile  
 
 Post subject: Re: Phone functions?
PostPosted: Tue Apr 23, 2013 12:19 pm 

Joined: Wed Aug 24, 2011 12:10 am
Posts: 31
Ok, I figured out the problem.
The single ' marks in the second script after ToggleWifiBtn should be " instead.

"ToggleWifiBtn' ---> "ToggleWifiBtn"


 Profile  
 
 Post subject: Re: Phone functions?
PostPosted: Thu Apr 25, 2013 12:36 pm 
Site Admin

Joined: Wed Aug 17, 2011 4:55 pm
Posts: 39
Quixote wrote:
While we're at it, can we change parameters of that button to reflect connectivity? Ie.- change the tint of the button or the image if connected to a certain network?


There is no current built-in trigger or variable that would let you do this easily right now. You could use a script to check the state similar to the above code, but you'd have to repeatedly test it, since the WiFi connection state could change even while you're actively using Andre (so you can't just test it once on resume). It shouldn't be too hard to add a system variable that gets set with the ssid whenever wifi state changes, I'll add it to the to-do list. Then you can edit the variable to trigger a button whenever the variable gets updated. So you'd create a hidden button which changes the state of the WiFi button or modifies a property like tint color, etc.

I will create an example when I add the variable to the code and release it.


 Profile  
 
 Post subject: Re: Phone functions?
PostPosted: Sun Jun 10, 2018 5:50 am 

Joined: Fri Jun 08, 2018 6:08 am
Posts: 81404
wh210wh210wh210wh210wh210


 Profile  
 
 Post subject: Re: Phone functions?
PostPosted: Sun Jun 10, 2018 5:50 am 

Joined: Fri Jun 08, 2018 6:08 am
Posts: 81404
wh210wh210wh210wh210wh210


 Profile  
 
 Post subject: Re: Phone functions?
PostPosted: Sun Jun 10, 2018 5:50 am 

Joined: Fri Jun 08, 2018 6:08 am
Posts: 81404
wh210wh210wh210wh210wh210


 Profile  
 
 Post subject: Re: Phone functions?
PostPosted: Sun Jun 10, 2018 5:51 am 

Joined: Fri Jun 08, 2018 6:08 am
Posts: 81404
wh210wh210wh210wh210wh210


 Profile  
 
 Post subject: Re: Phone functions?
PostPosted: Sun Jun 10, 2018 5:51 am 

Joined: Fri Jun 08, 2018 6:08 am
Posts: 81404
wh210wh210wh210wh210wh210


 Profile  
 
 Post subject: Re: Phone functions?
PostPosted: Sun Jun 10, 2018 5:51 am 

Joined: Fri Jun 08, 2018 6:08 am
Posts: 81404
wh210wh210wh210wh210wh210


 Profile  
 
 Post subject: Re: Phone functions?
PostPosted: Sun Jun 10, 2018 5:52 am 

Joined: Fri Jun 08, 2018 6:08 am
Posts: 81404
wh210wh210wh210wh210wh210


 Profile  
 
Display posts from previous:  Sort by  
 Page 1 of 4 [ 53 posts ]  Go to page 1, 2, 3, 4  Next

All times are UTC - 8 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  

cron