Java me 为什么我的J2ME蓝牙代码在传入RFCOMM连接后挂起?
我只想向第一个传入的RFCOMM蓝牙连接发送“hello”,然后退出。我的代码适用于诺基亚N73,但不适用于更现代的设备,如诺基亚E52。在诺基亚E52中,应用程序将在以下时间后挂起:Java me 为什么我的J2ME蓝牙代码在传入RFCOMM连接后挂起?,java-me,bluetooth,jsr82,Java Me,Bluetooth,Jsr82,我只想向第一个传入的RFCOMM蓝牙连接发送“hello”,然后退出。我的代码适用于诺基亚N73,但不适用于更现代的设备,如诺基亚E52。在诺基亚E52中,应用程序将在以下时间后挂起: streamConnectionNotifier.acceptAndOpen(); 代码如下: 所有代码都在我的线程的run()方法中: public class BTTest extends Thread { public void run() { ... } } 我首先将蓝牙设备设
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());
}
try {
OutputStream out = streamConnection.openOutputStream();
String s = "hello";
out.write(s.getBytes());
out.flush();
streamConnection.close();
} catch (IOException exception) {
System.out.println(exception.toString());
}
一旦获得StreamConnection对象,我发送“hello”,然后关闭连接:
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());
}
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.AcceptionOpen()之后挂起
有人有主意吗?如果需要更多信息,请说明。而不是使用SPP UUID(
0x1101
),您应该指定自己的128位UUID(使用uuidgen
或类似工具)。在应用程序挂起的手机上,可能正在运行另一个SPP服务。我将尝试您的建议,然后告诉您结果,谢谢!嗨,普华永道,不幸的是,那个项目被取消了,我从来没有机会测试它(