OSDN Git Service

lejos_NXJ_win32_0_5_0beta.zip
[nxt-jsp/lejos_nxj.git] / nxtOSEK / lejos_nxj / samples / BTConnectTest / BTConnectTest.java
1 import java.io.IOException;\r
2 \r
3 import lejos.nxt.*;\r
4 import lejos.nxt.comm.*;\r
5 import java.io.*;\r
6 \r
7 /**\r
8  * \r
9  * Test of NXT to NXT Bluetooth comms.\r
10  * \r
11  * Connects to another NXT, sends 100 ints, and receives the \r
12  * replies. Then closes the connection and shuts down.\r
13  * \r
14  * Works with the BTReceive sample running on the slave NXT.\r
15  * \r
16  * Change the name string to the name of your slave NXT, and make sure\r
17  * it is in the known devices list of the master NXT. To do this, turn\r
18  * on the slave NXT and make sure Bluetooth is on and the device\r
19  * is visible. Use the Bluetooth menu on the slave for this. Then,\r
20  * on the master, select the Bluetooth menu and then select Search.\r
21  * The name of the slave NXT should appear. Select Add to add\r
22  * it to the known devices of the master. You can check this has\r
23  * been done by selecting Devices from the Bluetooth menu on the\r
24  * master.\r
25  * \r
26  * @author Lawrie Griffiths\r
27  *\r
28  */\r
29 public class BTConnectTest {\r
30         public static void main(String[] args) throws Exception {\r
31                 String name = "NXT";\r
32                 \r
33                 LCD.drawString("Connecting...", 0, 0);\r
34                 LCD.refresh();\r
35                 \r
36                 BTRemoteDevice btrd = Bluetooth.getKnownDevice(name);\r
37 \r
38                 if (btrd == null) {\r
39                         LCD.clear();\r
40                         LCD.drawString("No such device", 0, 0);\r
41                         LCD.refresh();\r
42                         Thread.sleep(2000);\r
43                         System.exit(1);\r
44                 }\r
45                 \r
46                 BTConnection btc = Bluetooth.connect(btrd);\r
47                 \r
48                 if (btc == null) {\r
49                         LCD.clear();\r
50                         LCD.drawString("Connect fail", 0, 0);\r
51                         LCD.refresh();\r
52                         Thread.sleep(2000);\r
53                         System.exit(1);\r
54                 }\r
55                 \r
56                 LCD.clear();\r
57                 LCD.drawString("Connected", 0, 0);\r
58                 LCD.refresh();\r
59                 \r
60                 DataInputStream dis = btc.openDataInputStream();\r
61                 DataOutputStream dos = btc.openDataOutputStream();\r
62                                 \r
63                 for(int i=0;i<100;i++) {\r
64                         try {\r
65                                 LCD.drawInt(i*30000, 8, 0, 2);\r
66                                 LCD.refresh();\r
67                                 dos.writeInt(i*30000);\r
68                                 dos.flush();                    \r
69                         } catch (IOException ioe) {\r
70                                 LCD.drawString("Write Exception", 0, 0);\r
71                                 LCD.refresh();\r
72                         }\r
73                         \r
74                         try {\r
75                                 LCD.drawInt(dis.readInt(),8, 0,3);\r
76                                 LCD.refresh();\r
77                         } catch (IOException ioe) {\r
78                                 LCD.drawString("Read Exception ", 0, 0);\r
79                                 LCD.refresh();\r
80                         }\r
81                 }\r
82                 \r
83                 try {\r
84                         LCD.drawString("Closing...    ", 0, 0);\r
85                         LCD.refresh();\r
86                         dis.close();\r
87                         dos.close();\r
88                         btc.close();\r
89                 } catch (IOException ioe) {\r
90                         LCD.drawString("Close Exception", 0, 0);\r
91                         LCD.refresh();\r
92                 }\r
93                 \r
94                 LCD.clear();\r
95                 LCD.drawString("Finished",3, 4);\r
96                 LCD.refresh();\r
97                 Thread.sleep(2000);\r
98         }\r
99 }\r