5 public class NXTCommBluez implements NXTComm {
7 private static final String BDADDR_ANY = "00:00:00:00:00:00";
12 System.loadLibrary("jbluez");
15 public NXTInfo[] search(String name, int protocol) {
16 String[] btString = null;
19 btString = search(name);
20 } catch (BlueZException e) {
21 System.err.println(e.getMessage());
23 if (btString == null) return new NXTInfo[0];
25 NXTInfo[] nxts = new NXTInfo[btString.length];
26 for(int i=0;i<btString.length;i++) {
27 NXTInfo nxtInfo = new NXTInfo();
28 if (btString[i] == null) {
29 System.err.println("Null btString");
30 return new NXTInfo[0];
32 int sep = btString[i].indexOf("::");
33 //System.out.println("Setting address to " + btAddress);
34 nxtInfo.btDeviceAddress = btString[i].substring(sep+2);
35 nxtInfo.name = btString[i].substring(0, sep);
36 nxtInfo.protocol = NXTCommFactory.BLUETOOTH;
37 nxtInfo.btResourceString = btString[i];
45 public void close() throws IOException{
48 } catch (IOException ioe) {
49 System.err.println("Shutdown failed");
51 if (sk != -1) rcSocketClose(sk);
55 public boolean open(NXTInfo nxt) {
57 open(BDADDR_ANY, nxt.btDeviceAddress, 1);
59 } catch (BlueZException e) {
60 System.err.println("Error from open: " + e.getMessage());
66 public byte [] sendRequest(byte[] request, int replyLen) throws IOException {
69 byte[] lsb_msb = new byte[2];
70 lsb_msb[0] = (byte) request.length;
71 lsb_msb[1] = (byte) 0x00;
72 request = concat(lsb_msb, request);
74 rcSocketSend(sk, request);
76 if (replyLen == 0) return new byte[0];
79 data = rcSocketRecv(sk);
82 data = subArray(data, 2, data.length);
87 private void open(String l_bdaddr, String r_bdaddr, int channel) throws BlueZException {
91 //System.out.println("Creating socket");
92 sk = rcSocketCreate();
93 //System.out.println("Binding");
94 rcSocketBind(sk, l_bdaddr);
95 //System.out.println("Connecting");
96 rcSocketConnect(sk, r_bdaddr, channel);
104 } catch (IOException ioe) {}
111 private byte[] concat(byte[] data1, byte[] data2) {
112 int l1 = data1.length;
113 int l2 = data2.length;
115 byte[] data = new byte[l1 + l2];
116 System.arraycopy(data1, 0, data, 0, l1);
117 System.arraycopy(data2, 0, data, l1, l2);
123 private byte[] subArray(byte[] data, int start, int end) {
125 byte[] result = new byte[end - start];
126 System.arraycopy(data, start, result, 0, end - start);
131 public byte [] read () throws IOException {
132 // Currently all packets are 1-byte
133 byte [] packet = rcSocketRecv(sk);
134 byte [] data = new byte [packet.length/3];
135 for(int i=0;i<packet.length/3;i++) data[i] = packet[i*3+2];
139 public void write(byte[] data) throws IOException {
140 rcSocketSend(sk, data);
143 public OutputStream getOutputStream() {
144 return new NXTCommBTOutputStream(this);
147 public InputStream getInputStream() {
148 return new NXTCommBTInputStream(this);
151 native private String[] search(String name) throws BlueZException;
153 native private int rcSocketCreate() throws BlueZException;
155 native private void rcSocketBind(int sk, String bdaddr) throws BlueZException;
157 native private void rcSocketConnect(int sk, String bdaddr, int channel) throws BlueZException;
159 native public void rcSocketSend(int sk, byte[] data) throws IOException;
161 native public byte[] rcSocketRecv(int sk) throws IOException;
163 native private void rcSocketShutdown(int sk) throws IOException;
165 native private void rcSocketClose(int sk) throws IOException;