Game apps for smartphones and tablets

Research project PHBern  
HomeStart online editorAndroid TurtlegraphicsPrintJava-Online

Bluetooth connection between NXT and smartphone


In this first example, a Bluetooth connection is set up between the smartphone and the NXT robot. The program execution on NXT can be started and ended with the smartphone.

Before the first use, both devices have to be paired.
Switch on NXT and select on smartphone: Settings -Wireless and networks - Bluetooth settings

 

 

After selecting the NXT, you will be asked to enter a code Code. Enter 1234.

 

Show example

Edit example in the online editor

App installieren auf Smartphone oder Tablet

Program code:
// NxtBasic.java

package app.nxtbasic;

import ch.aplu.android.*;
import ch.aplu.android.nxt.*;

public class NxtBasic extends GameGrid
{
  public void main()
  {
    GGConsole = GGConsole.init();
    c.println("NxtBasic starting...");
    TurtleRobot robot = new TurtleRobot(c.getActivity());
    robot.connect();
    if (!robot.isConnected())
    {
      c.println("Connection to robot failed.");
      return;
    }
    c.println("Working now...");
    for (int = 0; i < 4; i++)
    {
      robot.forward(100);
      robot.left(90);
    }
    robot.exit();
    c.println("Connection closed.");
  }
}

 

   
Explanations to the program code:
GGConsole.init() Initializes a console window on the smartphone
new TurtleRobot(c.getActivity())  
if (robot.connect() != NxtRobot.ConnectState.CONNECTED)
      return;
If the Bluetooth connection cannot be set up, the following lines are not executed
robot.exit() The Bluetooth connection to the NXT is closed