我有一个接收线程监听BluetoothServerSocket accept()调用。这个线程将被另一个线程杀死并重新启动。当它获得kill信号时,它会关闭BluetoothServerSocket,从而在accept()中导致IOException,并退出无限循环。但偶尔,在这些启动和重启期间,listenUsingRfcommWithServiceRecord()调用将捕获“错误文件号”IOException并过早退出循环。我想我可能不会在kill(?)中正确关闭BT组件但是还没弄明白什么......有人可以给我一些指示吗?
private class ReceiveThread extends Thread {
private final static String TAG = "ReceiveThread";
private BluetoothServerSocket mmBtServer;
private BluetoothSocket mmBtSocket;
public ReceiveThread() {
setName(TAG);
}
public void run() {
if(D) Log.d(TAG,"RceiveThread running");
while(true) {
try {
SetState(SERVICE_STATE.LISTENING);
mmBtServer = mBtAdapter.listenUsingRfcommWithServiceRecord(mAppName, mUUID);
if(D) Log.d(TAG,"ReceiveThd accepting connection");
mmBtSocket = mmBtServer.accept();
// Close server once socket establishes
mmBtServer.close();
if(D) Log.d(TAG, "Connection Established");
SetState(SERVICE_STATE.CONNECTED_BUSY);
/** Handle Processing **/
// Close socket
mmBtSocket.close();
} catch (IOException e) {
SetState(SERVICE_STATE.NOT_CONNECTED);
Log.d(TAG,"IOException in ReceiveThread", e);
break;
}
SetState(SERVICE_STATE.CONNECTED_IDLE);
}
}
public void kill() {
try {
if(mmBtSocket!=null) mmBtSocket.close();
if(mmBtServer!=null) mmBtServer.close();
} catch (IOException e) {
Log.d(TAG,"Failure killing ReceiveThread", e);
}
SetState(SERVICE_STATE.NOT_CONNECTED);
}
}
答案 0 :(得分:2)
如果有人有兴趣..我发现这只发生在某些手机上。在大约20-30次停止和启动循环后,我可以在我的三星Galaxy S上始终如一地重现这一点。很多时候,我也会从bluez中找到一个“内存不足”,导致我将问题留在api下...我无法在Droid X,HTC G2或myTouch 4G上重现这个问题。