Android BluetoothServerSocket未从Accept方法返回
BluetoothServerSocketInstance.accept()不返回任何内容 下一步是不执行 在同一个位置上螺纹块 我见过许多类似的问题,但不幸的是,没有一个对我有帮助 我正在创建这样的接受线程Android BluetoothServerSocket未从Accept方法返回,android,multithreading,bluetooth,block,bluetooth-socket,Android,Multithreading,Bluetooth,Block,Bluetooth Socket,BluetoothServerSocketInstance.accept()不返回任何内容 下一步是不执行 在同一个位置上螺纹块 我见过许多类似的问题,但不幸的是,没有一个对我有帮助 我正在创建这样的接受线程 private class AcceptThread extends Thread { // The local server socket private String mSocketType; boolean isRunning = true; pri
private class AcceptThread extends Thread {
// The local server socket
private String mSocketType;
boolean isRunning = true;
private BluetoothServerSocket mmServerSocket;
public AcceptThread(boolean isAndroid) {
BluetoothServerSocket tmp = null;
// Create a new listening server socket
try {
if (isAndroid)
tmp = mAdapter.listenUsingRfcommWithServiceRecord(
NAME_SECURE, UUID_ANDROID_DEVICE);
else
tmp = mAdapter.listenUsingRfcommWithServiceRecord(
NAME_SECURE, UUID_OTHER_DEVICE);
} catch (IOException e) {
}
mmServerSocket = tmp;
}
public void run() {
setName("AcceptThread" + mSocketType);
BluetoothSocket socket = null;
// Listen to the server socket if we're not connected
while (mState != BluetoothState.STATE_CONNECTED && isRunning) {
try {
// This is a blocking call and will only return on a
// successful connection or an exception
if (mmServerSocket != null)
socket = mmServerSocket.accept();
} catch (IOException e) {
break;
}
// If a connection was accepted
if (socket != null) {
synchronized (BluetoothService.this) {
switch (mState) {
case BluetoothState.STATE_LISTEN:
case BluetoothState.STATE_CONNECTING:
// Situation normal. Start the connected thread.
connected(socket, socket.getRemoteDevice(),
mSocketType);
break;
case BluetoothState.STATE_NONE:
case BluetoothState.STATE_CONNECTED:
// Either not ready or already connected. Terminate
// new socket.
try {
socket.close();
} catch (IOException e) {
}
break;
}
}
}
}
}
public void cancel() {
try {
mmServerSocket.close();
mmServerSocket = null;
} catch (IOException e) {
}
}
public void kill() {
isRunning = false;
}
}
这是预期的行为,它阻塞直到成功建立连接:这是预期的行为,它阻塞直到成功建立连接: