为什么我的J2ME蓝牙code传入连接RFCOMM后挂起? [英] Why does my J2ME Bluetooth code hangs after an incoming RFCOMM connection?

查看:286
本文介绍了为什么我的J2ME蓝牙code传入连接RFCOMM后挂起?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所有我想要做的就是发送你好第一传入RFCOMM蓝牙连接,然后退出。我的code适用于诺基亚N73,但并不像诺基亚E52更现代化的设备。在诺基亚E52,应用程序挂起刚过:

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

下面是code:

所有code是我的线程的run()方法中:

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

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

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

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

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

有一次,我得到的StreamConnection对象,我送你好,然后关闭连接:

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

在诺基亚N73,我得到连接接收。在日志和我收到你好在客户端。不过,诺基亚E52,诺基亚5230,诺基亚E71,应用刚过挂起streamConnectionNotifier.acceptAndOpen()。

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.

推荐答案

除了使用SPP UUID( 0x1101 ),你应该指定自己的128-bit的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.

这篇关于为什么我的J2ME蓝牙code传入连接RFCOMM后挂起?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆