How to program in JAVA with Tasker

2,847 views
Skip to first unread message

czed

unread,
Feb 17, 2015, 10:19:17 AM2/17/15
to tas...@googlegroups.com
Hello,

Java programming function was added to Tasker and it's great.
My problem is that I do not understand how to program as "normal Java".
Something like :
int a=123;
return a;



Here's the code I'm interested in:

Near WiFi JAVA (188)
A1: Java Function [ Return:(WifiManager)
wiman Class Or Object:CONTEXT Function:getSystemService {Object}(String) Param:wifi Param: Param: Param: Param: Param: Param: Param: Param: Param: ]
A2: Java Function [ Return:%results Class Or Object:wiman Function:getScanResults {List} () Param: Param: Param: Param: Param: Param: Param: Param: Param: Param: ]
A3: Flash [ Text:%results1 Long:Off ]



This code should return information on nearby wifis, the problem is that it does not return the SSID.
Searching I found this Java code to see the SSID object

results.get (i) .SSID;


But I have no idea how to integrate because all the codes I've seen here have only one line function.


Thank you for your help.

technogeezer

unread,
Feb 17, 2015, 11:06:15 AM2/17/15
to tas...@googlegroups.com
Here's what I do (in psuedocode), and it works for me,

I do the first two steps above exactly the same.
3) variable set %cnt to %results(#)
4) for %cntr in [1:%cnt]
5) Popup results(%cntr)
6) end for

HTH

Pent

unread,
Feb 18, 2015, 3:30:37 AM2/18/15
to tas...@googlegroups.com

Near WiFi JAVA (188)
A1: Java Function [ Return:(WifiManager)
wiman Class Or Object:CONTEXT Function:getSystemService {Object}(String) Param:wifi Param: Param: Param: Param: Param: Param: Param: Param: Param: ]
A2: Java Function [ Return:%results Class Or Object:wiman Function:getScanResults {List} () Param: Param: Param: Param: Param: Param: Param: Param: Param: Param: ]

I have a feeling scan results may be null or zero length if there hasn't been a scan done recently.

To check that, you would need to assign the value to a Java object (e.g. results instead of %results)
and then check for null with:

If results eq null

Unfortunately, the current stable version of Tasker doesn't let you check for null. You can try the beta
posted above in the forum.

After the null check, you should check results.size() in a Java Code action to see if there are any results.

Pent

czed

unread,
Feb 19, 2015, 9:00:43 AM2/19/15
to tas...@googlegroups.com
Thank you for your answer, your code will maybe help but not today .

My problem is that the results variable doesn't show SSID, it show me something like that
,capabilities: [WPA-PSK-CCMP+TKIP][WPA2-PSK-CCMP+TKIP][ESS], level: -64, frequency: 2447, timestamp: 85733819773, distance: ?(cm), distanceSd: ?(cm)

Your code will show every results one by one but my problem will be the same (no SSID)

czed

unread,
Feb 19, 2015, 9:09:34 AM2/19/15
to tas...@googlegroups.com
I've never had any null results yet.

I will try may be the beta version, but I must before create a usable code

Do you know how can I get this value with Tasker  : results.get (i) .SSID; ?

Pent

unread,
Feb 19, 2015, 10:52:37 AM2/19/15
to tas...@googlegroups.com


My problem is that the results variable doesn't show SSID, it show me something like that
,capabilities: [WPA-PSK-CCMP+TKIP][WPA2-PSK-CCMP+TKIP][ESS], level: -64, frequency: 2447, timestamp: 85733819773, distance: ?(cm), distanceSd: ?(cm)

Ah, thought you weren't getting results.

To access the Java data structure, you need to store the result of the function in a Java object (no %) instead of a
Tasker variable. All Tasker variables are just strings.

So assign to 'results' instead of %results. Then you can loop through the list with a for action,
using values from results.size() and results.get( index ).

Something like:

Java Function, %listsize = results.size()
Variable Subtract, %listsize, 1
For %index 0:%listsize
  Java Function, result = results.get( %index )
  Java Function, %ssid = result.getSSID();
  Flash, Got SSID: %ssid
EndFor

Pent

Brad

unread,
Aug 29, 2015, 9:33:54 AM8/29/15
to Tasker
Hi,

Has anyone found a solution to this, on android 4.3 or 4.4, on android 5 and above getScanResults() returns the SSID and BSSID in the List or reuslts but not 4.3 or 4.4 ?

Brad

Brad

unread,
Aug 31, 2015, 9:25:16 AM8/31/15
to Tasker
Got It!

A1: Variable Set [ Name:%service To:wifi Do Maths:Off Append:Off ]
A2: Java Function [ Return:(WifiManager) wiman Class Or Object:CONTEXT Function:getSystemService {Object} (String) Param:%service Param: Param: Param: Param: Param: Param: ]
A3: Java Function [ Return: Class Or Object:(WifiManager) wiman Function:startScan {boolean} () Param: Param: Param: Param: Param: Param: Param: ]
A4: Java Function [ Return:list Class Or Object:wiman Function:getScanResults{List} () Param: Param: Param: Param: Param: Param: Param: ]
A5: Java Function [ Return:%size Class Or Object:list Function:size {int} () Param:0 Param: Param: Param: Param: Param: Param: ]
A6: Variable Subtract [ Name:%size Value:1 Wrap Around:0 ]
A7: For [ Variable:%index Items:0:%size ]
A8: Java Function [ Return:results Class Or Object:list Function:get {Object} (int) Param:%index Param: Param: Param: Param: Param: Param: ]
A9: Java Function [ Return:%ssid Class Or Object:results.SSID Function:toString{String} () Param:0 Param: Param: Param: Param: Param: Param: ]
A10: Java Function [ Return:%level Class Or Object:results.level Function:toString{String} () Param:0 Param: Param: Param: Param: Param: Param: ]
A11: Flash [ Text:%index %ssid %level Long:On ]
A12: End For
A13: Stop [ With Error:Off Task: ]

Hope this helps someone

Dale Smith

unread,
Sep 2, 2015, 12:36:48 AM9/2/15
to Tasker
Works great! I have a Samsung Galaxy S5 Android 5.0 stock phone. Tasker seems to have a problem with copy and pasting Java Function actions. I had Params that I couldn't see unless I exported to clipboard causing minor crashes. The UI for that action is a bit buggy. Build each action from scratch and you should be ok.

beshoo

unread,
May 25, 2016, 1:34:27 PM5/25/16
to Tasker
Dear Brad , i can not find a way to contact you but a reply on this. 
how can i run this java code in android Tasker:

ContentValues values = new ContentValues();

        values.put(Data.RAW_CONTACT_ID, 001);

        values.put(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE);

        values.put(Phone.NUMBER, "1-800-GOOG-411");

        values.put(Phone.TYPE, Phone.TYPE_CUSTOM);

        values.put(Phone.LABEL, "Nirav");

        Uri dataUri = getContentResolver().insert(android.provider.ContactsContract.Data.CONTENT_URI, values);

can you please help in this ?


Regards

Brad

unread,
May 26, 2016, 10:40:25 AM5/26/16
to Tasker
Hi,

Try this

A1: Java Function [ Return:values Class Or Object:ContentValues Function:new
{ContentValues} () Param:%service Param: Param: Param: Param: Param: Param: ]
A2: Java Function [ Return:%enabled Class Or Object:values Function:put
{} (String, Long) Param:Data.RAW_CONTACT_ID Param:001 Param: Param: Param: Param: Param: ]
A3: Java Function [ Return:%enabled Class Or Object:values Function:put
{} (String, String) Param:Data.MIMETYPE Param:Phone.CONTENT_ITEM_TYPE Param: Param: Param: Param: Param: ]
A4: Java Function [ Return:%enabled Class Or Object:values Function:put
{} (String, String) Param:Data.NUMBER Param:"1-800-GOOG-411" Param: Param: Param: Param: Param: ]
A5: Java Function [ Return:%enabled Class Or Object:values Function:put
{} (String, String) Param:Data.TYPE Param:Phone.TYPE_CUSTOM Param: Param: Param: Param: Param: ]
A6: Java Function [ Return:%enabled Class Or Object:values Function:put
{} (String, String) Param:Data.LABLE Param:"NIRAV" Param: Param: Param: Param: Param: ]
A7: Java Function [ Return:dataUri Class Or Object:CONTEXT Function:getContentResolver
{ContentResolver} () Param: Param: Param: Param: Param: Param: Param: ]
A8: Java Function [ Return:Uri Class Or Object:dataUri Function:insert
{Uri} (Uri, ContentValues) Param:android. provider. ContactsContract.Data.CONTENT_

URI Param:values Param: Param: Param: Param: Param: ]

Reply all
Reply to author
Forward
0 new messages