WiimoteConnector.java~
1.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package org.mote.wiimote.whiteboard;
import org.mote.wiimote.whiteboard.preferences.WWPreferences;
import wiiremotej.WiiRemote;
import wiiremotej.WiiRemoteJ;
public class WiimoteConnector {
private WiimoteDataHandler dh;
public WiimoteConnector(WiimoteDataHandler dh) {
this.dh = dh;
}
public void connect() {
if (!WWPreferences.WIIMOTE_BT_ADDRESSES.isEmpty()) {
WiimoteWhiteboard.getLogger().info(String.format("Directly connecting to bluetooth address(es) %s.", WWPreferences.WIIMOTE_BT_ADDRESSES));
for (int i = 0; i < Math.min(WWPreferences.WIIMOTE_BT_ADDRESSES.size(), WWPreferences.WIIMOTES); i++) {
connect(WWPreferences.WIIMOTE_BT_ADDRESSES.get(i));
}
} else {
WiiRemoteJ.findRemotes(dh, WWPreferences.WIIMOTES);
}
}
private void connect(final String address) {
new Thread(new Runnable() {
private boolean done = false;
public void run() {
while (!done) {
try {
WiiRemote r = WiiRemoteJ.connectToRemote(address);
if (r != null && r.isConnected()) {
done = true;
dh.addRemote(r);
}
} catch (Exception e) {
// if (e.getCause().getMessage().startsWith("Failed to open baseband connection")) {
// // apparently simple timeout error
// } else {
// // "regular" connection error
// }
try {
Thread.sleep(1000);
} catch (InterruptedException e1) {
e1.printStackTrace();
}
}
}
}
}).start();
}
}