Android蓝牙连接SPP后断开会出现IOException异常

dztai 2015-10-31 01:40:49
Android蓝牙连接SPP后断开会出现IOException异常, log如下

E/BluetoothSocket(5549): java.io.IOException: Operation Canceled
E/BluetoothSocket(5549): at android.bluetooth.BluetoothSocket.readNative(Native Method)
E/BluetoothSocket(5549): at android.bluetooth.BluetoothSocket.read(BluetoothSocket.java:386)
E/BluetoothSocket(5549): at android.bluetooth.BluetoothInputStream.read(BluetoothInputStream.java:96)
E/BluetoothSocket(5549): at java.io.InputStream.read(InputStream.java:163)
E/BluetoothSocket(5549): at com.adayome.hudsettingdemo.bt.BluetoothConnection$5.run(BluetoothConnection.java:335)

整个逻辑是这样的,连接SPP后,开启一个线程一直在read数据,

private class readThread extends Thread {
public void run() {

byte[] buffer = new byte[1024];
int bytes;
InputStream mmInStream = null;
try {
mmInStream = GlobalVariables.sSocket.getInputStream();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
while (!isInterrupted()) {
try {
// Read from the InputStream
if ((bytes = mmInStream.read(buffer)) > 0) {

byte[] buf_data = new byte[bytes];
for (int i = 0; i < bytes; i++) {
buf_data[i] = buffer[i];
}
String s = new String(buf_data);
LogUtils.i("readThread---------------2 s = " + s);
if (s.trim().equals("exit"))
break;
Message msg = new Message();
msg.obj = s;
msg.what = 1;
LinkDetectedHandler.sendMessage(msg);
}
} catch (IOException e) {
try {
mmInStream.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
break;
}
}
}
}

在退出时会中断这个线程和关闭相应的Socket

if (mReadThread != null) {
mReadThread.interrupt();
mReadThread = null;
}

if (GlobalVariables.sSocket != null) {
try {
InputStream in = GlobalVariables.sSocket.getInputStream();
OutputStream ou = GlobalVariables.sSocket.getOutputStream();

ou.close();
in.close();
GlobalVariables.sSocket.close();

} catch (IOException e) {
LogUtils.debugLog(e);
}

GlobalVariables.sSocket = null;
}

此时就会报上面的 java.io.IOException: Operation Canceled
各位大神有什么好的解决办法吗?
有什么办法可以中止I/O阻塞的线程?或是让read返回-1?
...全文
372 3 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
dztai 2016-12-13
  • 打赏
  • 举报
回复
找到一种解决办法,如下:

	private class ReadThread extends Thread {

		@Override
		public void run() {
			InputStream inStream = null;
			while (isRunning) {
				int size;
				try {
					byte[] buffer = new byte[1024];
					try {
						Thread.sleep(10);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
					}
					if (mSocket == null) {
						break;
					}
					inStream = mSocket.getInputStream();
					int available = inStream.available();
					if (available != 0) {
						size = inStream.read(buffer);
						if (size > 0 && mListener != null) {
							mListener.onDataReceived(buffer, size);
						}
					}
				} catch (IOException e) {
					try {
						if (inStream != null)
							inStream.close();
					} catch (IOException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
				}catch (NullPointerException e){
					LogUtils.e(e.getMessage());
				}
			}
			LogUtils.i("-----ReadThread is exit");
			closeSocket();
		}
	}
主要是利用 inStream = mSocket.getInputStream(); int available = inStream.available(); 这段代码来判断是否有数据再去读取。
dztai 2015-11-04
  • 打赏
  • 举报
回复
用标志位也不行的,因为read是I/O阻塞的,当前线程就停在 if ((bytes = mmInStream.read(buffer)) > 0)这一句,无法往下走,即使标志位为false也不会立刻执行
lucky_tom 2015-11-03
  • 打赏
  • 举报
回复
mReadThread.interrupt();是否中断了线程呢?最好使用标志位来消亡线程。 boolean flag = true; while (flag) { } 销毁时:flag = false;

80,472

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧