1 package nl
.ru
.des
.sensors
;
3 import java
.io
.DataInputStream
;
4 import java
.io
.DataOutputStream
;
5 import java
.io
.IOException
;
6 import java
.util
.LinkedList
;
7 import java
.util
.Queue
;
9 import lejos
.hardware
.Button
;
10 import lejos
.remote
.nxt
.BTConnector
;
11 import lejos
.remote
.nxt
.NXTConnection
;
13 public class BTController
{
14 public static Queue
<String
> buf
;
15 private static DataInputStream dataIn
;
16 private static DataOutputStream dataOut
;
17 private static NXTConnection conn
;
18 private static BTConnector btconnector
;
20 public static void startMaster(final String slave
, final MessageHandler sh
) {
21 System
.out
.println("Start BT... Press any key to commence");
22 Button
.waitForAnyEvent();
23 btconnector
= new BTConnector();
25 System
.out
.println("Connecting to " + slave
);
26 conn
= btconnector
.connect(slave
, NXTConnection
.RAW
);
28 dataOut
= conn
.openDataOutputStream();
29 dataIn
= conn
.openDataInputStream();
33 StringBuilder sb
= new StringBuilder();
36 int c
= dataIn
.readUnsignedByte();
38 sh
.handleMessage(sb
.toString());
39 sb
= new StringBuilder();
41 sb
.appendCodePoint(c
);
43 } catch (IOException e
) {
51 public static void startSlave() {
52 System
.out
.println("Start BT... Press any key to commence");
53 Button
.waitForAnyEvent();
54 btconnector
= new BTConnector();
56 System
.out
.println("Waiting for the master...");
57 conn
= btconnector
.waitForConnection(60000, NXTConnection
.RAW
);
59 dataIn
= conn
.openDataInputStream();
60 dataOut
= conn
.openDataOutputStream();
61 buf
= new LinkedList
<String
>();
63 @Override public void run(){
67 dataOut
.write(buf
.poll().getBytes());
69 } catch (Exception e
) {