Back

CreateNxt

Hello World
Content:
- Methods

The NXT object is used to manage the connection between your Android phone and the Lego Mindstorms NXT brick.

nxt = app.CreateNxt() → app object: Nxt

Note: This function is deprecated. Please install and visit the NXT-Docs from the Plugins page

Example - Basic

function OnStart()
{
    nxt = app.CreateNxt();
    nxt.SetOnConnected( OnConnected );
    nxt.ShowDevices();

}

function OnConnected()
{
    nxt.Beep( 100, 500 );
}
    Copy     Copy All       Run      

Example - Auto Connect

function OnStart()
{
    nxt = app.CreateNxt();

    var deviceName = "myNXTbrick";
    var paired = nxt.IsPaired( deviceName );
    if (paired)
    {
        app.ShowProgress( "Connecting to " + deviceName + "..." );
            nxt.SetOnConnect( nxt_OnConnect );
            nxt.Connect( deviceName );
    }

}

function nxt_OnConnect( success, nxt )
{
    if( success ) {
        app.ShowPopup( "Connected to: " + nxt.GetBtName() + " : " + nxt.GetBtAddress() );
        nxt.Beep( 2000, 200 );
    }
    else app.Alert( "Failed to connect!" );
    app.HideProgress();
}
    Copy     Copy All       Run      

Example - Sensors

function OnStart()
{
    nxt = app.CreateNxt();

    lay = app.CreateLayout( "linear", "FillXY,VCenter" );

    var list = "Color,Distance,Light,Mail,Sound,Touch"
    spnType = app.CreateSpinner( list, .3 );
    spnType.SetOnChange( spnType_OnChange );
    lay.AddChild( spnType );

    spnInput = app.CreateSpinner( "1,2,3,4", .3 );
    lay.AddChild( spnInput );

    btn = app.CreateButton( "Measure", .3, .1 );
    btn.SetOnTouch( ReadSensor );
    lay.AddChild( btn );

    app.AddLayout( lay );
}

function spnType_OnChange()
{
    app.SimulateTouch( spnInput );
}

function ReadSensor()
{
    var res = "invalid", inp = Number(spnInput.GetText());

    switch( spnType.GetText() )
    {
        case "Color": res = nxt.ToColorName( nxt.ReadColorSensor( inp, "ColorDetect" )); break;
        case "Distance": res = nxt.ReadDistanceSensor( inp ) + " cm"; break;
        case "Light": res = nxt.ReadLightSensor( inp, false ); break;
        case "Mail": res = nxt.ReadMail( inp, "Text", false ); break;
        case "Sound": res = nxt.ReadSoundSensor( inp, "dB" ); break;
        case "Touch": res = nxt.ReadTouchSensor( inp ); break;
    }

    if(res) app.ShowPopup( res );
    else app.ShowPopup( "No Value" );
}
Copy All       Run      

Methods

The following methods are available on the Nxt object:

CheckConnection() → boolean
FileFindFirst( pattern ) → string
FileFindNext( handle ) → string
GetBtAddress() → string
GetBtName() → string
GetCurrentProgram() → string
GetRotationCount( motor ) → number: integer
GetType() → string: “Nxt”
IsBluetoothEnabled() → boolean
IsConnected() → boolean
IsBluetoothEnabled() → boolean
IsPaired( name ) → boolean
ReadColorSensor( input, mode ) → number: integer: 1..6 or 0..1023
ReadDistanceSensor( input ) → number: float
ReadLightSensor( input, active ) → number: integer: 0..100
ReadMail( mailbox, type, remove ) → string
ReadSoundSensor( input, mode ) → number: float
ReadTouchSensor( input ) → boolean
ToColorName( val ) → string: “black” or “blue” or “green” or “yellow” or “red” or “white”
boolean
string
number: hertz
number: integer
number: milliseconds
number: percent
number: percent: -100..100
number: float: 0 or rotations
number: integer: 1
number: integer: 1..4
number: integer: 1..10
number: integer: 1..6
string: path to file or folder ( “/absolute/...” or “relative/...” )
string: “A”, “B”, “C” or “ABC”
string: “ColorDetect” or “LightSense” or “RedSense” or “GreenSense” or “BlueSense”
string: “Text” or “Number” or “Logic”
string: DB or DbA
string:
  hexadecimal: “#rrggbb”, “#aarrggbb”
  colourName: “red”, “green”, ...:
““White”” or ““Red”” or ““Green”” or ““Blue”” or ““Off”
string
number: integer
function( connected )
function()
nxt.Beep
Play tone with given frequency for a given time in milliseconds.
nxt.Brake
Brakes motors by lowering the input power.
nxt.CheckConnection
Check whether phone/tablet is connected to the NXT.
nxt.Connect
Connects to the NXT device via Bluetooth.
nxt.Disconnect
Disconnect Phone/Tablet from NXT.
nxt.Drive
commands NXT to drive motors
nxt.FileFindFirst
Find the first program file of the NXT.
nxt.FileFindNext
Find next program file on the NXT.
nxt.GetBtAddress
Returns Bluetooth address of a connected NXT brick
nxt.GetBtName
Returns Bluetooth name of a connected NXT brick
nxt.GetCurrentProgram
Returns the name of the currently running program.
nxt.GetRotationCount
Returns the wheel encoder value of a motor.
nxt.GetType
Returns the control class name.
nxt.IsBluetoothEnabled
Checks if bluetooth is enabled on the device.
nxt.IsConnected
Checks if the device is connected to a NXT.
nxt.IsBluetoothEnabled
Checks if bluetooth is enabled on the device. Its the same as nxt.IsBluetoothEnabled.
nxt.IsMotorIdle
Checks if a motor is currently powered
nxt.IsPaired
Checks if a Bluetooth device with a given name is paired with our device.
nxt.PlaySoundFile
Play a sound file for a given amount of times.
nxt.ReadColorSensor
Read a color sensor value on the input where the sensor is plugged into.

In “ColorDetect” mode this function returns a value between 1 and 6 which represent the colors “black”, “blue”, “green”, “yellow”, “red”, or “white”.
You can use the ToColorName function to convert from these six values to a color name.
All other modes return a value between 0 and 1023.
nxt.ReadDistanceSensor
Reads the distance measured by the ultrasonic sensor in centimeters.
nxt.ReadLightSensor
Reads the intensity of the light currently being 'seen' by the NXT light sensor. If active is set true an additionall white LED will be turned on while sensing.
nxt.ReadMail
Reads a message from the NXT brick's mail box. This message can be written using a normal NXT-G program running on the brick. This allows you to read values from NXT-G programs with your phone or tablet.
nxt.ReadSoundSensor
Reads the sound pressure level of the surrounding.
nxt.ReadTouchSensor
Returns whether the touch sensor switch is currently pushed.
nxt.RequestEnable
Invokes system dialog box which could enable Bluetooth on tablet/phone when Bluetooth is disabled.
nxt.Reset
Reset wheel encoder offset to 0.
nxt.SendMail
Sends a message to the NXT brick's mail box.
nxt.SetInvert
Provides a convenient way to invert the direction of the motor commands.
nxt.SetLampColor
Changes the color sensor LED to one color of “White”, “Red”, “Green”, “Blue” or “Off”
nxt.SetOnConnect
Define a callback function which is called when the NXT has been connected via Bluetooth or if it failed to.
nxt.SetOnConnected
Define a callback function which is called when the NXT has been successfully connected via Bluetooth
nxt.ShowDevices
Shows the user a dialog box that contains a list of NXT bricks which are paired with the phone. The user can then select the brick to connect to via Bluetooth.
nxt.StartProgram
Starts an NXT-G program on the NXT brick if available.
nxt.Stop
Stop powering some motors.
nxt.StopProgram
Stops a the currently running NXT-G program.
nxt.ToColorName
Converts a color integer received from ReadColorSensor to a meaningful color name.