cod*_*gic 7 android bluetooth ioexception iobluetooth
我的应用程序的一部分通过蓝牙连接到设备,通常工作正常但偶尔它不会连接,我得到以下错误
03-11 10:29:20.328: E/BluetoothComService(8059): accept() failed
03-11 10:29:20.328: E/BluetoothComService(8059): java.io.IOException: Operation Canceled
03-11 10:29:20.328: E/BluetoothComService(8059): at android.bluetooth.BluetoothSocket.acceptNative(Native Method)
03-11 10:29:20.328: E/BluetoothComService(8059): at android.bluetooth.BluetoothSocket.accept(BluetoothSocket.java:316)
03-11 10:29:20.328: E/BluetoothComService(8059): at android.bluetooth.BluetoothServerSocket.accept(BluetoothServerSocket.java:105)
03-11 10:29:20.328: E/BluetoothComService(8059): at android.bluetooth.BluetoothServerSocket.accept(BluetoothServerSocket.java:91)
03-11 10:29:20.328: E/BluetoothComService(8059): at com.mypackage.name.bluetooth.BluetoothService$AcceptThread.run(BluetoothService.java:298)
Run Code Online (Sandbox Code Playgroud)
这是我得到例外的那一行
socket = mmServerSocket.accept();
Run Code Online (Sandbox Code Playgroud)
这是完整的AcceptThread
private class AcceptThread extends Thread {
// The local server socket
private BluetoothServerSocket mmServerSocket;
public boolean successInit = false;
public AcceptThread() {
closeAllConnections();
/*
* if(mmServerSocket != null) { try { mmServerSocket.close(); } catch
* (IOException e) { e.printStackTrace(); } }
*/
BluetoothServerSocket tmp = null;
// Create a new listening server socket
while (!successInit) {
try {
tmp = mAdapter
.listenUsingRfcommWithServiceRecord(NAME, MY_UUID);
successInit = true;
} catch (Exception e) {
successInit = false;
}
}
/*
* try { tmp = mAdapter.listenUsingRfcommWithServiceRecord(NAME,
* MY_UUID); successInit= true; } catch (IOException e) { Log.e(TAG,
* "listen() failed", e); tmp = null; successInit = false; }
*/
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
mAdapter.cancelDiscovery();
socket = mmServerSocket.accept();
} catch (IOException e) {
Log.e(TAG, "accept() failed", e);
Log.e("Error", "This isn't connecting");
break;
}
// If a connection was accepted
if (socket != null) {
synchronized (BluetoothService.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");
}
public void cancel() {
if (D)
Log.d(TAG, "cancel " + this);
try {
mmServerSocket.close();
} catch (IOException e) {
Log.e(TAG, "close() of server failed", e);
}
}
}
Run Code Online (Sandbox Code Playgroud)
这是我在开始时调用的函数AcceptThread,希望关闭所有内容以重新启动它
public void closeAllConnections() {
if (mmInStream != null) {
try {mmInStream.close();}
catch (Exception e){Log.e(TAG, "close() of connect socket failed", e);}
}
if (mmOutStream != null) {
try {mmOutStream.close();}
catch (Exception e){Log.e(TAG, "close() of connect socket failed", e);}
}
if (mmSocket != null) {
try {
mmSocket.close();
//mmSocket.connect();
}
catch (IOException e) {
Log.e(TAG, "close() of connect socket failed", e);
}
}
}
Run Code Online (Sandbox Code Playgroud)
我已经阅读了蓝牙文档和SO问题,但我没有发现任何对我有用的东西,这对我来说有点混乱,因为这是我第一次通过BT连接.
注意
发生这种情况时,我发现的唯一"修复"是关闭BT适配器,强制关闭程序,重启BT适配器并重启应用程序,这显然不是很好.我尝试以编程方式重新启动适配器,但我仍然无法连接.
任何人都可以看到我的BlutoothService类可能出现的问题,它位于何处AcceptThread?或者我将如何解决这个问题?谢谢!
事实上,它看起来似乎有时在一个Thread连接上关闭并试图在另一个连接上重新连接.问题是,我无法弄清楚是什么原因导致它尝试单独连接Thread或如何在发生这种情况时进行修复.
我能成功重现这一点的唯一方法是,如果我的BT设备关闭,那么我关闭BT适配器.当我重新打开所有东西然后我得到异常并且无法连接.我有客户认为它是随机和定期发生的,所以我希望问题是相关的.
当 BluetoothServerSocket 关闭或垃圾收集时,应该发生该异常。我怀疑异常发生在线程的旧副本上。就像这样:当您创建新线程时,旧线程被取消,因此 BluetoothServerSocket 被关闭,因此正确接受失败并出现该错误。在调试器中检查这一点和/或记录在哪个线程上发生各种事件;例如,在accept之后的行上以及可能在cancel函数上设置断点,然后检查那里的线程ID——异常是否发生在前一个线程上?
| 归档时间: |
|
| 查看次数: |
5878 次 |
| 最近记录: |