开发者

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

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) {
    开发者_Go百科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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜