为什么我的 J2ME 蓝牙代码在传入 RFCOMM 连接后挂起?

发布于 2024-09-19 10:54:35 字数 2065 浏览 5 评论 0原文

我想做的就是向第一个传入的 RFCOMM 蓝牙连接发送“hello”,然后退出。我的代码适用于诺基亚 N73,但不适用于诺基亚 E52 等更现代的设备。在诺基亚 E52 中,应用程序在以下之后挂起:

streamConnectionNotifier.acceptAndOpen();

这是代码:

所有代码都在我的线程的 run() 方法内:

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

我首先将我的蓝牙设备设置为可发现:

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

然后,我准备我的服务器套接字:

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

然后,我等待传入的 RFCOMM 蓝牙连接:

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

一旦获得 StreamConnection 对象,我发送“hello”,然后关闭连接:

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

在诺基亚 N73 中,我收到“连接已收到”。在日志上,我在客户端收到“hello”。但在诺基亚 E52、诺基亚 5230、诺基亚 E71 上,应用程序在执行streamConnectionNotifier.acceptAndOpen() 后立即挂起。

有人有想法吗?如果您需要更多信息,请注明。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

属性 2024-09-26 10:54:35

您不应使用 SPP UUID (0x1101),而应指定自己的 128 位 UUID(使用 uuidgen 或类似工具)。在应用程序挂起的手机上,可能正在运行另一个 SPP 服务。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文