OSDN Git Service

lejos_NXJ_win32_0_5_0beta.zip
[nxt-jsp/lejos_nxj.git] / nxtOSEK / lejos_nxj / samples / BTSend / BTSend.java
1 import lejos.pc.comm.*;
2 import java.io.*;
3
4 /**
5  * This is a PC sample. It connects to the NXT, and then
6  * sends an integer and waits for a reply, 100 times.
7  * 
8  * Compile this program with javac (not nxjc), and run it 
9  * with java.
10  * 
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.
14  * 
15  * Run the program by:
16  * 
17  *   java BTSend <name> <address>
18  *   
19  * where <name> is the name of your NXT, and <address> is
20  * its Bluetooth address. 
21  * 
22  * For example:
23  * 
24  *   java BTSend NXT 00:16:53:00:78:48
25  *   
26  * You can find the address for your NXT by running nxjbrowse
27  *  - this lists the name and address of each NXT it finds.
28  * 
29  * See the comment in the code on how to do a Bluetooth 
30  * inquiry to find your NXT, instead of using the address
31  * parameter.
32  * 
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. 
36  * 
37  * @author Lawrie Griffiths
38  *
39  */
40 public class BTSend {
41         
42         public static void main(String[] args) {
43                 NXTComm nxtComm = NXTCommFactory.createNXTComm(NXTCommFactory.BLUETOOTH);
44                 
45                 /* Another way to connect, by discovery:
46
47                 NXTInfo[] nxtInfo = nxtComm.search(args[0], NXTCommFactory.BLUETOOTH);
48                 
49                 if (nxtInfo.length == 0) {
50                         System.out.println("No NXT Found");
51                         System.exit(1);
52                 }
53                 */
54                 
55                 // arg[0] = name, e.g NXT
56                 // arg[1] = address, with optional colons, e.g. 00:16:53:00:78:48
57         
58                 if (args.length != 2) {
59                         System.out.println("Usage: BTSend name address");
60                         System.exit(1);
61                 }
62                 
63                 NXTInfo[] nxtInfo = new NXTInfo[1];
64                         
65                 nxtInfo[0] = new NXTInfo(args[0],args[1]);
66                 
67                 System.out.println("Connecting to " + nxtInfo[0].btResourceString);
68
69                 boolean opened = false;
70                 
71                 try {
72                         opened = nxtComm.open(nxtInfo[0]); 
73                 } catch (NXTCommException e) {
74                         System.out.println("Exception from open");
75                 }
76                 
77                 if (!opened) {
78                         System.out.println("Failed to open " + nxtInfo[0].name);
79                         System.exit(1);
80                 }
81                 
82                 System.out.println("Connected to " + nxtInfo[0].btResourceString);
83                 
84                 InputStream is = nxtComm.getInputStream();
85                 OutputStream os = nxtComm.getOutputStream();
86                 
87                 DataOutputStream dos = new DataOutputStream(os);
88                 DataInputStream dis = new DataInputStream(is);
89                                 
90                 for(int i=0;i<100;i++) {
91                         try {
92                                 System.out.println("Sending " + (i*30000));
93                                 dos.writeInt((i*30000));
94                                 dos.flush();                    
95                                 
96                         } catch (IOException ioe) {
97                                 System.out.println("IO Exception writing bytes:");
98                                 System.out.println(ioe.getMessage());
99                                 break;
100                         }
101                         
102                         try {
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());
107                                 break;
108                         }
109                 }
110                 
111                 try {
112                         dis.close();
113                         dos.close();
114                         nxtComm.close();
115                 } catch (IOException ioe) {
116                         System.out.println("IOException closing connection:");
117                         System.out.println(ioe.getMessage());
118                 }
119         }
120 }