1 import lejos.pc.comm.*;
5 * This is a PC sample. It connects to the NXT, and then
6 * sends an integer and waits for a reply, 100 times.
8 * Compile this program with javac (not nxjc), and run it
11 * You need pccomm.jar on the CLASSPATH. On Windows you
12 * will also need bluecove.jar on the CLASSPATH. On Linux,
13 * you will need libjbluez.so on the Java library path.
17 * java BTSend <name> <address>
19 * where <name> is the name of your NXT, and <address> is
20 * its Bluetooth address.
24 * java BTSend NXT 00:16:53:00:78:48
26 * You can find the address for your NXT by running nxjbrowse
27 * - this lists the name and address of each NXT it finds.
29 * See the comment in the code on how to do a Bluetooth
30 * inquiry to find your NXT, instead of using the address
33 * Your NXT should be running a sample such as BTReceive or
34 * SignalTest. Run the NXT program first until it is
35 * waiting for a connection, and then run the PC program.
37 * @author Lawrie Griffiths
42 public static void main(String[] args) {
43 NXTComm nxtComm = NXTCommFactory.createNXTComm(NXTCommFactory.BLUETOOTH);
45 /* Another way to connect, by discovery:
47 NXTInfo[] nxtInfo = nxtComm.search(args[0], NXTCommFactory.BLUETOOTH);
49 if (nxtInfo.length == 0) {
50 System.out.println("No NXT Found");
55 // arg[0] = name, e.g NXT
56 // arg[1] = address, with optional colons, e.g. 00:16:53:00:78:48
58 if (args.length != 2) {
59 System.out.println("Usage: BTSend name address");
63 NXTInfo[] nxtInfo = new NXTInfo[1];
65 nxtInfo[0] = new NXTInfo(args[0],args[1]);
67 System.out.println("Connecting to " + nxtInfo[0].btResourceString);
69 boolean opened = false;
72 opened = nxtComm.open(nxtInfo[0]);
73 } catch (NXTCommException e) {
74 System.out.println("Exception from open");
78 System.out.println("Failed to open " + nxtInfo[0].name);
82 System.out.println("Connected to " + nxtInfo[0].btResourceString);
84 InputStream is = nxtComm.getInputStream();
85 OutputStream os = nxtComm.getOutputStream();
87 DataOutputStream dos = new DataOutputStream(os);
88 DataInputStream dis = new DataInputStream(is);
90 for(int i=0;i<100;i++) {
92 System.out.println("Sending " + (i*30000));
93 dos.writeInt((i*30000));
96 } catch (IOException ioe) {
97 System.out.println("IO Exception writing bytes:");
98 System.out.println(ioe.getMessage());
103 System.out.println("Received " + dis.readInt());
104 } catch (IOException ioe) {
105 System.out.println("IO Exception reading bytes:");
106 System.out.println(ioe.getMessage());
115 } catch (IOException ioe) {
116 System.out.println("IOException closing connection:");
117 System.out.println(ioe.getMessage());