Remote access to a NXT

Lawrie Griffiths

Remote access to a NXT


It is possible to access a remote NXT from an EV3 over Bluetooth serial comms. The same mechanism works from a PC running Linux, but not currently from Microsoft Windows or other operating systems.

To connect to a NXT using LCP to access the hardware, you can do:

RemoteNXT nxt = new RemoteNXT("NXT2", Bluetooth.getNXTCommConnector());
System.out.println(nxt.getName());
nxt.getAudio().playTone(1000, 1000);

For a stream connection to a NXT, for example, if the NXT is running the NXTReceive sample over Bluetooth with a packet connection:

final String NXT = "NXT2";
NXTCommConnector connector = Bluetooth.getNXTCommConnector();   
System.out.println("Connecting to " + NXT);
NXTConnection connection = connector.connect(NXT, NXTConnection.PACKET);
if (connection == null) {
    System.err.println("Failed to connect");
    return;
}
System.out.println("Connected");

DataInputStream input = connection.openDataInputStream();
DataOutputStream output = connection.openDataOutputStream();

System.out.println("Sending data");

for(int i=0;i<100;i++) {
    output.writeInt(i);
    output.flush();
    System.out.println("Read: " + input.readInt());
}

System.out.println("All data sent");

output.close();
input.close();
connection.close(); 

System.out.println("Connection closed");

Related

EV3 Wiki: Home