0

Bluetooth PAN ネットワーク経由で接続されている 3 つのデバイスがあります。

  • デバイス 1 : サーバー - 私の場合、サーバーは EV3 LEGO ブロック == ロボットです!
  • デバイス 2 : リモート - 2 番目のデバイス (Android アプリ) は、デバイス 1 のアクションを制御するために使用する必要があります。
  • デバイス 3 : フロントエンド - 3 番目のデバイスは、選択したアクションを表示する必要があります (Android)

可能な通信方式はBluetoothと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();
    }

リレースレッド THREAD

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;
            }
        }
    }
}

現在、デバイス 3 として TCP クライアントを使用していますが、デバイス 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);
     }
  }
}

そして、Main を保持するための RelayThread のコンストラクター

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 に答える