我有 3 台设备通过蓝牙 PAN 网络连接。
- 设备 1:服务器 - 在我的情况下,服务器是 EV3 LEGO Brick == ROBOT!
- 设备 2:远程 - 应使用第二个设备(Android APP)来控制设备 1 的操作
- 设备 3:前端 - 第三个设备应显示所选操作 (Android)
可能的通信方式是蓝牙和 JAVA 中的套接字连接。我已经可以从 DEVICE 2 控制 DEVICE 1 - 但命令不会中继到 DEVICE 3。这是我用于服务器的代码:
主要的
try {
serverSocket = new ServerSocket( 1111 );
} catch (IOException e) {
e.printStackTrace();
}
while (true) {
try {
socket = serverSocket.accept();
} catch (IOException e) {
System.out.println("I/O error: " + e);
}
// new thread for a client
new RelayThread(socket).start();
}
RelayThread 线程
public class RelayThread extends Thread {
protected Socket socket;
BufferedReader bufferedReader;
public RelayThread (Socket clientSocket) {
this.socket = clientSocket;
}
public void run() {
Singleton motors = Singleton.getInstance();
InputStream inp = null;
BufferedReader brinp = null;
DataOutputStream out = null;
try {
inp = socket.getInputStream();
InputStreamReader isr = new InputStreamReader(inp, "UTF-8");
bufferedReader = new BufferedReader(isr);
out = new DataOutputStream(socket.getOutputStream());
} catch (IOException e) {
return;
}
while (true) {
try {
String command= bufferedReader.readLine();
if ((command== null) || command.equalsIgnoreCase("QUIT")) {
socket.close();
return;
}
else {
// do ROBOT actions
/*
* SERVER ACTIONS
*/
// notify the other client of the delivered LINE
out.writeBytes(command);
out.flush();
}
} catch (IOException e) {
e.printStackTrace();
return;
}
}
}
}
我现在使用 TCP-Client 作为我的 DEVICE 3 - 但是当我通过 DEVICE 2 发送命令时它不显示任何文本。我怎么能实现我的项目 - 我做错了什么?