Connect on app launch?

96 views
Skip to first unread message

patel4prez

unread,
Mar 10, 2015, 1:18:24 PM3/10/15
to androi...@googlegroups.com
Hello all, 

Was just introduced to DroidScript this morning and had a quick question.

Is there any way to automatically have the app connect to an NXT Brick when it is launched?

I have a system in place right now (my blinds get raised and lowered using a bluetooth-connected NXT Brick) that uses Tasker to input taps and navigate my current app (consists of 3 buttons), but it takes a while and isn't the "right way" to do it.

Here is the code I have so far:


//Called when application is started.
function OnStart()
{
    //Create our screen layout.
    CreateLayout();
    //Create NXT controller object.
    nxt = app.CreateNxt();   
}
//Create the screen layout.
function CreateLayout()
{    
    //Create the layout (with repeating pattern background).
    lay = app.CreateLayout( "Linear", "VCenter,FillXY" );
    lay.SetBackground( "/Sys/Img/Tile.png", "repeat" );
    //Create 'Connect' button.
    btn = app.CreateButton( "Connect", 0.3,0.1, "Lego" );
    btn.SetMargins( 0,0,0,0.1 );
    btn.SetOnTouch( btn_OnTouch );  
    lay.AddChild( btn );
    //Create 'Blinds - Up' Button
    btnUp = app.CreateButton( "Up", 0.3, 0.1, "Lego" );    
    btnUp.SetOnTouch( btnUp_OnTouch );     
    lay.AddChild( btnUp );
    //Create 'Blinds - Down' Button
    btnDown = app.CreateButton( "Down", 0.3, 0.1, "Lego" );    
    btnDown.SetOnTouch( btnDown_OnTouch );     
    lay.AddChild( btnDown );
    //Add layout to the app.
    app.AddLayout( lay );
}
//Called when user presses connect.
function btn_OnTouch()
{        
    //Show list of NXT devices.
    nxt.ShowDevices();
}
//Called when 'Blinds - Up' button is pressed.
function btnUp_OnTouch( e )
{                         
    nxt.StartProgram( "Blinds - Up.rxe" );
}
//Called when 'Blinds - Down' button is pressed.
function btnDown_OnTouch( f )
{                           
    nxt.StartProgram( "Blinds - Down.rxe" );
}



Any help is greatly appreciated. 

Thank you! 

Steve Garman

unread,
Mar 10, 2015, 5:22:27 PM3/10/15
to androi...@googlegroups.com
While you are waiting for someone to come along who knows the Next brick, have you had a chance to look at the recently added nxt stuff on the wiki?

http://wiki.droidscript.me.uk/doku.php?id=built_in:nxt

patel4prez

unread,
Mar 10, 2015, 7:59:18 PM3/10/15
to androi...@googlegroups.com
Hey Steve,

I checked out the Wiki (looks to be the similar to the in-app documentation) and the only methods I see regarding connectivity are used to check to see if there is a brick connected. It doesn't allow (as far as I can tell) for any sort of connection initiation.

Thanks for the tip though!

Dave Smart

unread,
Mar 11, 2015, 4:59:10 AM3/11/15
to androi...@googlegroups.com
Hi Guys,

You can connect to the brick at startup like this:-

(Sorry these methods are not documented yet)


//Called when application is started.
function OnStart()
{
//Create our screen layout.
CreateLayout();

//Create NXT controller object.
nxt = app.CreateNxt();   
//Connect to NXT by name.
var name = "NXT_BOB";
app.ShowProgress( "Connecting to "+name+"..." );
        nxt.SetOnConnect( nxt_OnConnect );
        nxt.Connect( name );
}

//Handle successful brick connection.
function nxt_OnConnect( success, nxt )
{
    if( success ) nxt.Beep( 2000, 200 );
    else app.Alert( "Failed to connect!" );
    app.HideProgress();
}

Steve Garman

unread,
Mar 11, 2015, 6:27:12 AM3/11/15
to androi...@googlegroups.com
Dave, is this the same name that is returned by nxt.GetBtName() when you connect to the brick manually?
http://wiki.droidscript.me.uk/doku.php?id=built_in:nxt_getbtaddress

I expect app.SaveText and app.LoadText will come in handy here.
http://wiki.droidscript.me.uk/doku.php?id=built_in:save_text

patel4prez

unread,
Mar 11, 2015, 8:06:01 AM3/11/15
to androi...@googlegroups.com
Just came to say that this worked beautifully! 

I modified the code a little bit to run a program upon successful connection, and now when I open the app from Tasker, I can get it to run the program without any interaction!

Thank you!

If anybody's wondering, the code I used to complete this task is as follows:

//Called when application is started.
function OnStart()
{

    //Create NXT controller object.
    nxt = app.CreateNxt();   

    //Connect to NXT by name.
    var name = "Blinds";
    app.ShowProgress( "Connecting to "+name+"..." );
        nxt.SetOnConnected( runProgram );
        nxt.Connect( name );

//Function to run program
function runProgram()

Dave Smart

unread,
Mar 11, 2015, 9:13:33 AM3/11/15
to androi...@googlegroups.com
I would love to see a youtube video of your blinds in action :)

madlyR

unread,
Mar 11, 2015, 1:43:04 PM3/11/15
to androi...@googlegroups.com
Hi Dave,

first I have to say I love DroidScript! 

I try to update wiki for NXT last days and most of my work is just reverse engineering based on modified Introspector app and guesses.

Thanks for tip on callback nxt_OnConnected - I see there are two parameters. I updated SetOnConnect
Probably SetOnConnected callback has nxt object parameter too. Yes, or no?

Please see my last Wiki updates, my English is horrible and it's better to check and correct my language errors and my guesses about some undocumented functions.

Radek 

patel4prez

unread,
Mar 11, 2015, 1:46:39 PM3/11/15
to androi...@googlegroups.com
I'll try and put something together this weekend :-)
Reply all
Reply to author
Forward
0 new messages