OSDN Git Service

f98a01947e423a4957f0a82b7cd26814e090d87e
[nxt-jsp/lejos_nxj.git] / nxtOSEK / lejos_nxj / samples / BTSend / BTSend.java
1 import lejos.pc.comm.*;
2 import java.io.*;
3
4 public class BTSend {
5         
6         public static void main(String[] args) {
7                 NXTComm nxtComm = NXTCommFactory.createNXTComm(NXTCommFactory.BLUETOOTH);
8                 
9                 /* Another way to connect, by discovery:
10
11                 NXTInfo[] nxtInfo = nxtComm.search(args[0], NXTCommFactory.BLUETOOTH);
12                 
13                 if (nxtInfo.length == 0) {
14                         System.out.println("No NXT Found");
15                         System.exit(1);
16                 }
17                 */
18                 
19                 // arg[0] = name, e.g NXT
20                 // arg[1] = address, with optional colons, e.g. 00:16:53:00:78:48
21         
22                 if (args.length != 2) {
23                         System.out.println("Usage: BTSend name address");
24                         System.exit(1);
25                 }
26                 
27                 NXTInfo[] nxtInfo = new NXTInfo[1];
28                         
29                 nxtInfo[0] = new NXTInfo(args[0],args[1]);
30                 
31                 System.out.println("Connecting to " + nxtInfo[0].btResourceString);
32
33                 boolean opened = nxtComm.open(nxtInfo[0]); 
34                 
35                 if (!opened) {
36                         System.out.println("Failed to open " + nxtInfo[0].name);
37                         System.exit(1);
38                 }
39                 
40                 System.out.println("Connected to " + nxtInfo[0].btResourceString);
41                 
42                 InputStream is = nxtComm.getInputStream();
43                 OutputStream os = nxtComm.getOutputStream();
44                 
45                 DataOutputStream dos = new DataOutputStream(os);
46                 DataInputStream dis = new DataInputStream(is);
47                                 
48                 for(int i=0;i<100;i++) {
49                         try {
50                                 dos.writeInt((i*30000));
51                                 dos.flush();                    
52                         } catch (IOException ioe) {
53                                 System.out.println("IO Exception writing bytes");
54                         }
55                         
56                         try {
57                                 System.out.println("Received " + dis.readInt());
58                         } catch (IOException ioe) {
59                                 System.out.println("IO Exception reading bytes");
60                         }
61                 }
62                 
63                 try {
64                         dis.close();
65                         //dos.close(); Why does this prevent re-connection?
66                         nxtComm.close();
67                 } catch (IOException ioe) {
68                         System.out.println("IOException closing connection");
69                 }
70         }
71 }