New to Droidscript, got some issues and needs help

865 views
Skip to first unread message

Daniel Jones

unread,
Dec 29, 2015, 8:21:05 AM12/29/15
to DroidScript
Hi,

guys.

i just installed droidscript two days ago, and trying to figure out how to make my first app via droidscript.

however, right now i got some issues which i very much would like to ask for help.

1. WIFI connect to PC feature does not work

i have tried a couple times so far, it does not seem to work. my laptop and my Nexus 6 both are connected to the same wireless router.

i already enable the adb wireless option on droidscript app, however, when i tried to input 127.0.0.1:8088 to the browser in PC, it come up with cannot establish the connection error page.

BTW, adb drive and required USB drive for nexus6 are already installed in my laptop.

please help me out.

2. i have been working on build my first app via droidscript, and now i got an issue could not figure out how to fix.

1)---- when i click to run, it always returns with error, saying "a is not defined". but i think i already define a with var, what is wrong?

2)----- the exit button, i have no idea how to do to make a exit action, like if i press exit button, then the app exits itself. how?

please check my script below and help me with some answers

thanks in advance.



function OnStart() {
    lay = app.CreateLayout("Linear", "Horizontal,FillXY");

    btnA = app.CreateButton("press me to start", 0.2, 0.1);
    lay.AddChild(btnA);

    btnB = app.CreateButton("press me to quit", 0.2, 0.1);
    lay.AddChild(btnB);

    btnA.SetOnTouch(btn_OnTouch);
    btnB.SetOnTouch(btnb_OnTouch);

    loc = app.CreateLocator("Network");
    loc.SetOnChange(loc_Onchange);
    loc.SetRate(20);
    loc.Start();

    app.AddLayout(lay);
}



function loc_Onchange(data) {
      var a = data.provider + ":Lat" + data.latitude + ",Lng" + data.longitude;
 
}

function btn_OnTouch() {
    app.ShowPopup("please wait, locating......");
    if (a == undefined) {
        setTimeout("loc_Onchange()",3000);
    } else {
         app.Alert(a, "location result");
    }

}

function btnb_OnTouch() {
    app.ShowPopup("exit");
}

Chris

unread,
Dec 29, 2015, 9:10:04 AM12/29/15
to DroidScript
I would HIGHLY recommend you read ebooks or google javascript for beginners.

The scope of a variable is only within a function if declared as

function whatever() {
var a = 'hello'
}

Once that function passes, a is not available elsewhere. IF you want a to be global, declare 'var a' outside of a function (normally at the very top if your script) or don't use 'var' at all, it will be created as a global.

if (a == undefined) will always throw an error if a is undefined. To check for a 'undefined' var, use:
if (typeof a == 'undefined') -----

DroidScript is javascript. Again, I recommend you do some beginners tutorials on that first. :)

Netpower8

unread,
Dec 29, 2015, 9:31:59 AM12/29/15
to DroidScript
hi heres the updated code with comments... var "a" need to be a global var to be able to be seen by other functions

/*
//Called when application is started.
function OnStart()
{
    //Create a layout with objects vertically centered.
    lay = app.CreateLayout( "linear", "VCenter,FillXY" );   

    //Create a text label and add it to layout.
    txt = app.CreateText( "Hello" );
    txt.SetTextSize( 32 );
    lay.AddChild( txt );
   
    //Add layout to app.   
    app.AddLayout( lay );
}

*/


function OnStart() {
    lay = app.CreateLayout("Linear", "Horizontal,FillXY");

    btnA = app.CreateButton("press me to start", 0.2, 0.1);
    lay.AddChild(btnA);

    btnB = app.CreateButton("press me to quit", 0.2, 0.1);
    lay.AddChild(btnB);

    btnA.SetOnTouch(btn_OnTouch);
    btnB.SetOnTouch(btnb_OnTouch);

    loc = app.CreateLocator("Network");
    loc.SetOnChange(loc_Onchange);
    loc.SetRate(20);
    loc.Start();

    app.AddLayout(lay);
}


// var a inside this function is a local variable.... you have to declare a global var
// to be able to let other functions "SEE" the var
// the "myglobalcar" is a global variable and can be seen by other functions
var myglobalvar = "please wait, locating......";
var a="";

function loc_Onchange(data) {
      var a = data.provider + ":Lat" + data.latitude + ",Lng" + data.longitude;
     
      // uncomment below and comment the above line
      // a = data.provider + ":Lat" + data.latitude + ",Lng" + data.longitude;
 
}

function btn_OnTouch() {
    app.ShowPopup(myglobalvar);
    if (a == undefined) {                       // <<<=== the script error is correct
        setTimeout("loc_Onchange()",3000);      // var a is not defined.

    } else {
         app.Alert(a, "location result");
    }

}

function btnb_OnTouch() {
    app.ShowPopup("exit");  // this only effectively let the user know exit was selected
    app.Exit();             // <<<<==== add this line. to exit
}

Steve Garman

unread,
Dec 29, 2015, 9:33:39 AM12/29/15
to DroidScript
As far as the WiFi IDE is concerned, the ADB stuff is for connection by USB cable.

As your devices are connected by WiFi, you should turn off the ADB option in DroidScript.

Then when you start the server, it will display an ip address to type into your PC browser.

Steve Garman

unread,
Dec 29, 2015, 9:41:14 AM12/29/15
to DroidScript
Looking at Nelson's code, I see he has helpfully explained about declaring a global variable a but has then continued to use a local variable in the function.

You need to change


var a = data.provider + ":Lat" + data.latitude + ",Lng" + data.longitude;

to

Steve Garman

unread,
Dec 29, 2015, 9:43:53 AM12/29/15
to DroidScript
Sorry Nelson,
I see you put a comment in explaining that he needs to do that.

When I run code in my head, I skip comments without looking at them.

Daniel Jones

unread,
Dec 29, 2015, 9:54:38 AM12/29/15
to DroidScript
Hi,

Steve, thanks for your comments.

maybe i did not explain well, i meant i was trying to use wireless ADB feature provided by Droidscript app, i did not use USB cable to connect my phone to laptop. i just meant the adb via USB cable is working well but the wireless ADB is not. so i am not able to edit or run script on my laptop by wireless ADB, which Droidscript said it is available feature for users.

Netpower8

unread,
Dec 29, 2015, 9:56:33 AM12/29/15
to DroidScript
its okay steve... we all make mistakes... like the last time i made a mistake my mind keep ignoring the push command i made on the 3rd line... was really tired and somehow my mind keep on ignoring it... and was really frustrated why the code did not work properly... until you pointed me i made my error happen... grrrrr

Daniel Jones

unread,
Dec 29, 2015, 10:00:50 AM12/29/15
to DroidScript
Hi,

that is odd, i just tried all your guys comments above to fix my script.

however, it still says "a is not defined".

althought i already removed the var in front of a, also try to use either    if (a == undefined) {     or    if (typeof a == undefined) {    

both ways are the same error,

can you guys help me fix it?

Steve Garman

unread,
Dec 29, 2015, 10:05:19 AM12/29/15
to DroidScript
Daniel,
I think I understood what you were saying butthe WiFi IDE does not normally use ADB, the ADB option is mostly a workaround for people without a wireless router.

If you turn off the "use ADB" option in the DroidScript settings box, then restart the WiFi server in DroidScript, a box will pop up with an address provided by your router.

This will probably be 192.161...
It will definitely not be 127.0.0.1

Each Android device will expose a different ip address so you can use different browser tabs on your PC for different phones/tablets.

Steve Garman

unread,
Dec 29, 2015, 10:16:33 AM12/29/15
to DroidScript
if (a == undefined)

if (typeof a == undefined)

Neither of those is right, you need.

if (typeof a == "undefined")

Also, there is no point in running loc_OnChange from a setTimeout

That function will be called by the locator when it has some data for you.

Daniel Jones

unread,
Dec 29, 2015, 10:17:39 AM12/29/15
to DroidScript
Thanks, Steve

it is very helpful, now, wireless ADB feaure is working like a charm.

my bad, i did not notice there is a "ADB option" in settings and forgot to turn it off.

:)

Ron Michel

unread,
Jan 19, 2017, 12:46:55 PM1/19/17
to DroidScript
a... could be undefined because gps location takes a long time to gather, what I do is sample a and if aold is != a then I will toggle gps as loaded and proceed with my routines. when working with gps make sure location is on.

Symbroson Development

unread,
Jan 19, 2017, 12:52:15 PM1/19/17
to DroidScript
If you're new to DS you could read this topic https://groups.google.com/forum/m/#!topic/androidscript/mjpck-lX7Lw
Also I suggest you to loog at the 'Whack a Droid' pdf file

Tech Ansari

unread,
May 25, 2017, 8:05:18 AM5/25/17
to DroidScript
Same with me i am also trying to connect droid script in windows pc but it shows no result found please help me out.
Reply all
Reply to author
Forward
0 new messages