OSDN Git Service

lejos_NXJ_win32_0_5_0beta.zip
[nxt-jsp/lejos_nxj.git] / nxtOSEK / lejos_nxj / samples / BTReceive / BTReceive.java
1 import lejos.nxt.*;
2 import lejos.nxt.comm.*;
3 import java.io.*;
4
5 /**
6  * Receive data from another NXT, a PC, a phone, 
7  * or another bluetooth device.
8  * 
9  * Waits for a connection, receives an int and returns
10  * its negative as a reply, 100 times, and then closes
11  * the connection, and waits for a new one.
12  * 
13  * @author Lawrie Griffiths
14  *
15  */
16 public class BTReceive {
17
18         public static void main(String [] args)  throws Exception 
19         {
20                 String connected = "Connected";
21         String waiting = "Waiting...";
22         String closing = "Closing...";
23         
24                 while (true)
25                 {
26                         LCD.drawString(waiting,0,0);
27                         LCD.refresh();
28
29                 BTConnection btc = Bluetooth.waitForConnection();
30                 
31                         LCD.clear();
32                         LCD.drawString(connected,0,0);
33                         LCD.refresh();  
34
35                         DataInputStream dis = btc.openDataInputStream();
36                         DataOutputStream dos = btc.openDataOutputStream();
37                         
38                         for(int i=0;i<100;i++) {
39                                 int n = dis.readInt();
40                                 LCD.drawInt(n,7,0,1);
41                                 LCD.refresh();
42                                 dos.writeInt(-n);
43                                 dos.flush();
44                         }
45                         
46                         dis.close();
47                         dos.close();
48                         Thread.sleep(100); // wait for data to drain
49                         LCD.clear();
50                         LCD.drawString(closing,0,0);
51                         LCD.refresh();
52                         btc.close();
53                         LCD.clear();
54                 }
55         }
56 }
57