蓝牙创建套接字但在 connect() 处阻塞 ANDROID
我正在使用 Google 蓝牙聊天示例中的代码来设置蓝牙监听服务器。我希望应用程序始终侦听传入连接。当应用程序看到连接请求时,它将接受连接,读取远程设备将发送的字符串,然后通过发送文件进行响应。
问题是该应用程序从不接受连接。它在 socket = mmServerSocket.accept();
处阻塞,并且永远不会继续前进。请参阅以下行后的代码 if(D) System.out.println("Waiting to connect************");
为了测试它,我启动了活动它启动线程,然后尝试连接我的笔记本电脑并发送文件。当我这样做时,整个 Android 蓝牙管理器会看到该文件并有效地绕过我的 Android 设备下载它。这是测试它的唯一方法吗?我无法弄清楚这是测试问题还是编码问题。
private class AcceptThread extends Thread {
// The local server socket
private final BluetoothServerSocket mmServerSocket;
public AcceptThread() {
BluetoothServerSocket tmp = null;
// Create a new listening server socket
try {
tmp = mAdapter.listenUsingRfcommWithServiceRecord(NAME, MY_UUID);
} catch (IOException e) {
Log.e(TAG, "listen() failed", e);
}
mmServerSocket = tmp;
}
public void run() {
if (D) Log.d(TAG, "BEGIN mAcceptThread" + this);
setName("AcceptThread");
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
if(D) System.out.println("Waiting to connect************");
socket = mmServerSocket.accept();
if(D) System.out.println("We have accepted connection and are connected***************");
} catch (IOException e) {
Log.e(TAG, "accept() failed", e);
break;
}
// If a connection was accepted
if (socket != null) {
synchronized (BluetoothServer.this) {
switch (mState) {
case STATE_LISTEN:
case STATE_CONNECTING:
// Situation normal. Start the connected thread.
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;
}
}
}
}
if (D) Log.i(TAG, "END mAcceptThread");
}
I am using code from the Google Bluetooth Chat Example to set up a bluetooth listening server. I want the app to listen always for an incoming connection. When the app sees a connection request it will accept the connection, read in the string that the remote device will send, and then respond by sending a file.
The problem is that the app never accepts the connection. It blocks at socket = mmServerSocket.accept();
and never moves on. See the code below after the line if(D) System.out.println("Waiting to connect************");
To test it I start the activity that starts the thread and then attempt to connect with my laptop and send a file. When I do this the overall Android bluetooth manager sees the file and downloads it effectively bypassing my Android device. Is this the only way to test it? I cant figure out if its a testing or coding problem.
private class AcceptThread extends Thread {
// The local server socket
private final BluetoothServerSocket mmServerSocket;
public AcceptThread() {
BluetoothServerSocket tmp = null;
// Create a new listening server socket
try {
tmp = mAdapter.listenUsingRfcommWithServiceRecord(NAME, MY_UUID);
} catch (IOException e) {
Log.e(TAG, "listen() failed", e);
}
mmServerSocket = tmp;
}
public void run() {
if (D) Log.d(TAG, "BEGIN mAcceptThread" + this);
setName("AcceptThread");
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
if(D) System.out.println("Waiting to connect************");
socket = mmServerSocket.accept();
if(D) System.out.println("We have accepted connection and are connected***************");
} catch (IOException e) {
Log.e(TAG, "accept() failed", e);
break;
}
// If a connection was accepted
if (socket != null) {
synchronized (BluetoothServer.this) {
switch (mState) {
case STATE_LISTEN:
case STATE_CONNECTING:
// Situation normal. Start the connected thread.
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;
}
}
}
}
if (D) Log.i(TAG, "END mAcceptThread");
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
此问题的解决方案位于您的
androidmanifest.xml
文件中;手机没有授予您的程序权限
我实际上添加了一些代码,但我猜它已被编辑掉
The solution to this problem lies in your
androidmanifest.xml
file;The phone isn't giving your program permissions
I have actually added some code but I guess it got edited out
检查您的客户端是否正在寻找与 MY_UUID 相同的 UUID。您可能想要将 UUID 更改为 RFCOMM 的通用 UUID。
Check if your client side is looking for same UUID as MY_UUID. You may want to change the UUID to generic UUID for RFCOMM.
我也有同样的问题。我正在使用蓝牙聊天示例,该示例从输出 ADC 数据的 arduino BT 读取数据。一切都工作正常,直到我更新了 SDK。直到我尝试使用 DataInputStream 包装的 InpuStream 读取数据,程序才会退出,这样我就可以使用 readline() 函数。我有正确的 UUID。我多次尝试
更改读取功能:
I'm having the same problem. I'm using the Bluetooth Chat example that reads data from an arduino BT that is outputing ADC data. Everything was working fine until I updated my SDK. The program doesn't exit until I try to read in data using InpuStream wrapped with DataInputStream so I can use the readline() function. I have the right UUID. I've tried multiple time
my altered read function: