即使设备已配对,BluetoothServerSocket.accept() 也不会返回
我正在开发一个基于 BluetoothChat 示例 的 Android 蓝牙应用程序。我正在启动蓝牙服务器并侦听设备(不是手机)以通过不安全的 rfcomm 连接连接到我的应用程序。
private class AcceptThread extends Thread {
// The local server socket
private final BluetoothServerSocket mmServerSocket;
public AcceptThread(boolean secure) {
BluetoothServerSocket tmp = null;
// Create a new listening server socket
try {
tmp = mAdapter.listenUsingInsecureRfcommWithServiceRecord(mServiceName, MY_UUID_INSECURE);
} catch (Exception e) {
Log.e(TAG, ".AcceptThread # listen() failed", e);
}
mmServerSocket = tmp;
}
public void run() {
BluetoothSocket socket = null;
// Listen to the server socket if we're not connected
while (mState != STATE_CONNECTED) {
try {
// This is a blocking call and will only return on a
// successful connection or an exception
Log.d(TAG, ".AcceptThread.run # ...accepting server socket conn");
socket = mmServerSocket.accept(); //FIXME: it blocks here
Log.d(TAG, ".AcceptThread.run # server socket connection accepted");
} catch (Exception e) {
MMLog.e(TAG, ".run # accept() failed: "+e);
connectionFailed();
break;
}
// If a connection was accepted
if (socket != null) {
synchronized (BluetoothService.this) {
switch (mState) {
case STATE_LISTEN:
case STATE_CONNECTING:
// starting the thread where i will receive input
// streams from the other device
connected(socket, socket.getRemoteDevice());
break;
case STATE_NONE:
case STATE_CONNECTED:
// Either not ready or already connected. Terminate new socket.
try {
socket.close();
} catch (IOException e) {
Log.e(TAG, "Could not close unwanted socket", e);
}
break;
}
}
}
}
}
public void cancel() {
try {
if(mmServerSocket != null) {
mmServerSocket.close();
}
} catch (IOException e) {
Log.e(TAG, ".cancel # Could not close server socket: ", e);
}
}
}
我使用的是 HTC Desire S,安卓 2.3.5。设备已配对,但我没有收到数据,因为连接在“.accept()”方法中被阻止。它只是继续等待。
socket = mmServerSocket.accept();
//...and waiting
- 如果设备已配对,为什么它仍然等待?
- 我怎样才能建立连接,因为我也尝试过反射,但仍然没有结果
- HTC的蓝牙堆栈有问题吗?有人使用另一部 Android 手机建立了连接吗?
I am developing an Android bluetooth application based on the BluetoothChat exemple. i am starting a bluetooth server and listening for a device(not a phone) to connect to mine app on an insecure rfcomm connection.
private class AcceptThread extends Thread {
// The local server socket
private final BluetoothServerSocket mmServerSocket;
public AcceptThread(boolean secure) {
BluetoothServerSocket tmp = null;
// Create a new listening server socket
try {
tmp = mAdapter.listenUsingInsecureRfcommWithServiceRecord(mServiceName, MY_UUID_INSECURE);
} catch (Exception e) {
Log.e(TAG, ".AcceptThread # listen() failed", e);
}
mmServerSocket = tmp;
}
public void run() {
BluetoothSocket socket = null;
// Listen to the server socket if we're not connected
while (mState != STATE_CONNECTED) {
try {
// This is a blocking call and will only return on a
// successful connection or an exception
Log.d(TAG, ".AcceptThread.run # ...accepting server socket conn");
socket = mmServerSocket.accept(); //FIXME: it blocks here
Log.d(TAG, ".AcceptThread.run # server socket connection accepted");
} catch (Exception e) {
MMLog.e(TAG, ".run # accept() failed: "+e);
connectionFailed();
break;
}
// If a connection was accepted
if (socket != null) {
synchronized (BluetoothService.this) {
switch (mState) {
case STATE_LISTEN:
case STATE_CONNECTING:
// starting the thread where i will receive input
// streams from the other device
connected(socket, socket.getRemoteDevice());
break;
case STATE_NONE:
case STATE_CONNECTED:
// Either not ready or already connected. Terminate new socket.
try {
socket.close();
} catch (IOException e) {
Log.e(TAG, "Could not close unwanted socket", e);
}
break;
}
}
}
}
}
public void cancel() {
try {
if(mmServerSocket != null) {
mmServerSocket.close();
}
} catch (IOException e) {
Log.e(TAG, ".cancel # Could not close server socket: ", e);
}
}
}
I am using a HTC Desire S, android 2.3.5. The device gets paired, but i don't receive data, because the connection gets blocked in the '.accept()' method. It just keeps on waiting.
socket = mmServerSocket.accept();
//...and waiting
- Why does it still wait, if the device is paired?
- How can i establish the connection, because i also tried reflection, and still no result
- Is there a problem with HTC's Bluetooth stack? Has anyone established the connection maybe using another android phone?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为您的代码有问题,不应该是
socket = tmp.accept();
这是我必须建立套接字服务器连接的内容:I think there is something wrong with your code, shouldn't it be
socket = tmp.accept();
here is what I have to make a socket server connection:有可能,这与您的其他设备有关(这发生在我身上)。您的 Android 设备已经完成了列出传入连接的工作。您的特殊设备无法正确启动与 Android 手机的连接的原因有很多:
我想您最好的机会是向特殊设备的制造商/销售商询问详细规格和/或用于配置/测试它的软件/驱动程序。
Chances are, this has to do with your other device (this is happening to me). Your Android already does its job which is listing for incoming connections. There are many reason why your special device won't initiate connections properly to your Android phone:
I suppose your best chance is to query the manufacturer/ seller of the special device for detailed specifications and/or software/ driver to configure/ test it.