Why does my J2ME Bluetooth code hangs after an incoming RFCOMM connection?

纵然是瞬间 提交于 2019-12-24 13:44:17

问题


All I want to do is send "hello" to the first incoming RFCOMM Bluetooth connection and then exit. My code works on Nokia N73 but not on more modern devices like Nokia E52. In Nokia E52, the application hangs just after:

streamConnectionNotifier.acceptAndOpen();

Here is the code:

All the code is inside the run() method of my Thread:

public class BTTest extends Thread {
    public void run() {
    ...
    }
}

I first set my bluetooth device discoverable:

try {
    LocalDevice localDevice = LocalDevice.getLocalDevice();
    localDevice.setDiscoverable(DiscoveryAgent.GIAC);
} catch (BluetoothStateException exception) {
    System.out.println(exception.toString());
}

Then, I prepare my server socket:

StreamConnectionNotifier streamConnectionNotifier = null;
try {
    String url = "btspp://localhost:" + new UUID(0x1101).toString() + ";name=SampleServer";
    streamConnectionNotifier = (StreamConnectionNotifier)Connector.open(url);
    if (streamConnectionNotifier == null) {
        System.out.println("Error: streamConnectionNotifier is null");
        return;
    }
} catch (IOException exception) {
    System.out.println(exception.toString());
}

Then, I wait for an incoming RFCOMM Bluetooth connection:

StreamConnection streamConnection = null;
try {
    System.out.println("Waiting for incoming connection...");
    streamConnection = streamConnectionNotifier.acceptAndOpen();
    if (streamConnection == null) {
        System.out.println("Error: streamConnection is null");
    } else {
        System.out.println("Connection received.");
    }
} catch (IOException exception) {
    System.out.println(exception.toString());
}

Once, I get the StreamConnection object, I send "hello" and then close the connection:

try {
    OutputStream out = streamConnection.openOutputStream();
    String s = "hello";
    out.write(s.getBytes());
    out.flush();
    streamConnection.close();
} catch (IOException exception) {
    System.out.println(exception.toString());
}

In Nokia N73, I get "Connection received." on the logs and I receive "hello" on the client side. But on Nokia E52, Nokia 5230, Nokia E71, the application hangs just after streamConnectionNotifier.acceptAndOpen().

Does anyone have an idea? If you need more information please state.


回答1:


Instead of using the SPP UUID (0x1101), you should specify your own 128-bit UUID (using uuidgen or a similar tool). On the phones where the application hangs, there is probably another SPP service running.



来源:https://stackoverflow.com/questions/3701551/why-does-my-j2me-bluetooth-code-hangs-after-an-incoming-rfcomm-connection

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!