我的应用程序的一部分通过蓝牙连接到设备并且正常工作但偶尔无法连接并且我收到以下错误
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)
这是我得到异常的行
socket = mmServerSocket.accept();
这是完整的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);
}
}
}
这是我在 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);
}
}
}
我已经通读了 Bluetooth Docs等等问题,但我还没有找到对我有用的东西,这让我有点困惑,因为这是我第一次通过 BT 连接。
注意
发生这种情况时,我发现的唯一“解决方法”是关闭 BT 适配器,强制关闭程序,重新启动 BT 适配器并重新启动应用程序,这显然不好。我尝试以编程方式重新启动适配器,但仍然无法连接。
谁能看出我的 BlutoothService 类中可能有什么问题,AcceptThread
所在的位置?或者我将如何解决这个问题?谢谢!
更新
事实上,似乎连接有时在一个 Thread
上关闭并尝试在另一个上重新连接。问题是我无法弄清楚什么会导致它尝试连接到单独的 Thread
或发生这种情况时如何修复它。
我可以成功重现此问题的唯一方法是,如果我的 BT 设备已关闭,那么我会关闭 BT 适配器。当我重新打开所有内容时,出现异常并且无法连接。我有客户随机和定期发生这种情况,所以我希望这些问题是相关的。
最佳答案
好吧,我的部分问题是硬件问题,后来发现是第三方制造商端的问题。它们的固件不太正确,当它读取 BT 地址时,它偶尔会被损坏。
在软件方面,它定期在两个单独的Threads
中运行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();
Log.e("TAG", "close() of connect socket successfu;");
} catch (IOException e) {
Log.e("TAG", "close() of connect socket failed", e);}
}
然后在 BluetoothCom
类中,我注意到它并不总是在尝试重新启动服务之前检查 BT 对象是否为 null
。
private void setupService() {
// Initialize the BluetoothChatService to perform bluetooth connections
if((mChatService != null) && (mChatService.getState() != 0)) {
mChatService.stop();
}
// I didn't have this so it would always start a new instance of the Service
if (mChatService == null)
mChatService = new BluetoothService(mHandler);
mChatService.start();
}
这似乎有所帮助,我不再有这些问题。但是,现在在 Samsung Galaxy Tab 4 上进行测试,我再次遇到连接问题,但仅限于此设备。也许这些信息可以帮助某人,如果我对新问题有任何其他了解,我会更新我的答案。
注意:如上所述,此蓝牙应用使用来自 Android 的 BluetoothChat 应用的修改代码。
此外,我已经阅读(并注意到)不同的制造商以不同的方式实现 BT 堆栈,这可能会导致头痛(至少如果您对此了解不够的话)。
关于android - 接受线程上的 IOException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15341445/