0

我有 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 发送命令时它不显示任何文本。我怎么能实现我的项目 - 我做错了什么?

4

1 回答 1

0

这是给你的服务器的。创建所有连接的列表

List<RelayThread> clients = new ArrayList<RelayThread>();
while (true) {
        try {
            socket = serverSocket.accept();
        } catch (IOException e) {
            System.out.println("I/O error: " + e);
        }
        // new thread for a client
        RelayThread relay = new RelayThread(socket,this);
        relay.start();
        clients.add(relay); 
   }

以及向其他客户端发送消息的方法

public void sendCommand(String command, RelayThread source){
  for (int i=0;i<clients.size();i++){
     if (clients.get(i) != source) {
        clients.get(i).sendCommand(command);
     }
  }
}

并且,RelayThread 的构造函数保持 Main

Main main;
public RelayThread (Socket clientSocket,Main main) {
    this.socket = clientSocket;
    this.main = main;
}

并且,RelayThread 中的发件人消息

public void sendCommand(String command){
    out.writeBytes((command+"\r\n").getBytes()); // I suggest you add a parser charachter, like \r\n. then client can understand message ends
}
于 2015-11-16T21:23:55.893 回答