Java me 基于j2me的蓝牙通信服务器客户端

Java me 基于j2me的蓝牙通信服务器客户端,java-me,bluetooth,midp,jsr82,Java Me,Bluetooth,Midp,Jsr82,如何使用同一个流多次从服务器到客户端或从客户端到服务器进行读/写 我正在做一个基于回合的蓝牙游戏。有没有关于如何在j2me中实现这一点的想法 我正在使用RfCOM协议 客户端代码是 public void serviceSearchCompleted(int transID, int respCode) { try { StreamConnection SC = (StreamConnection) Connector.open(connectionURL);

如何使用同一个流多次从服务器到客户端或从客户端到服务器进行读/写

我正在做一个基于回合的蓝牙游戏。有没有关于如何在j2me中实现这一点的想法

我正在使用RfCOM协议

客户端代码是

public void serviceSearchCompleted(int transID, int respCode) {
    try {
        StreamConnection SC = (StreamConnection) Connector.open(connectionURL);
        input = SC.openDataInputStream();
        output = SC.openDataOutputStream();
    } catch (IOException ex) {
        ex.printStackTrace();
    }
    while (true) {
        f.setCommandListener(new CommandListener() {

            public void commandAction(Command c, Displayable d) {
                if (c.getLabel().toString().equalsIgnoreCase("send")) {
                    try {
                        output.writeUTF("Hey server");
                        output.flush();
                        String msg = input.readUTF();
                        System.out.println(msg);
                    } catch (IOException ex) {
                        ex.printStackTrace();
                        System.out.println("am here now " + ex);
                    }
                }
            }
        });
        synchronized (lock) {
            lock.notify();
        }

    }
}
服务器代码:

while (true) {
                StreamConnection sc = scn.acceptAndOpen();

                RemoteDevice rd = RemoteDevice.getRemoteDevice(sc);
                DataInputStream input = sc.openDataInputStream();
                DataOutputStream output = sc.openDataOutputStream();
                String inMsg = input.readUTF();
                System.out.println(inMsg + " recived at " + new Date().toString());

                output.writeUTF("Hey client Sent at " + new Date().toString());
                output.flush();
            }
流只工作一次,然后再次单击“发送”时不会发生任何事情

处理连接初始化4 处理连接打开4 处理连接发送4 处理连接接收4 嘿,客户于2012年7月22日太阳19:47:15 GMT+02:00发送 处理连接发送4 处理连接接收4
L2CAPConnectionNotifier.acceptAndOpen
将阻止循环并等待新连接。 将代码从while主体移动到新线程

while (true) {
    StreamConnection sc = scn.acceptAndOpen();
    final RemoteDevice rd = RemoteDevice.getRemoteDevice(sc);
    new Thread() {
        public void run() {
            treatConnection(rd);
        }
    }.start();
}

private void treatConnection(RemoteDevice rd) {
    DataInputStream input = sc.openDataInputStream();
    DataOutputStream output = sc.openDataOutputStream();
    String inMsg = input.readUTF();

    while (inMsg != null) { // not sure about this stop condition...
        System.out.println(inMsg + " recived at " + new Date().toString());
        output.writeUTF("Hey client Sent at " + new Date().toString());
        output.flush();

        inMsg = input.readUTF();
    }
}

在第一次成功接收后,它仍然会被阻止