【问题标题】:bluetooth communication server client stuck j2me蓝牙通信服务器客户端卡住j2me
【发布时间】:2012-07-21 02:28:18
【问题描述】:

如何使用同一个 Stream 从服务器到客户端或从客户端到服务器多次读取/写入?

我正在通过蓝牙制作回合制游戏。关于如何在 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();
            }

流只工作一次,然后当我再次点击发送时没有任何反应

Processing CONN_INIT 4
Processing CONN_OPEN 4
Processing CONN_SEND 4
Processing CONN_RECEIVE 4
Hey client Sent at Sun Jul 22 19:47:15 GMT+02:00 2012
Processing CONN_SEND 4
Processing CONN_RECEIVE 4

【问题讨论】:

    标签: java-me bluetooth midp jsr82


    【解决方案1】:

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

    【讨论】:

    • 第一次成功接收后还是一样被阻塞
    猜你喜欢
    • 2021-12-20
    • 1970-01-01
    • 2011-03-13
    • 2018-01-01
    • 2011-08-08
    • 2013-06-21
    • 2021-08-31
    • 2015-06-14
    • 1970-01-01
    相关资源
    最近更新 更多