java.io.IOException: read failed, socket might closed or timeout, read ret: -1

最近项目中连接蓝牙之后接收蓝牙设备发出的指令功能,在连接设备之后,创建RfcommSocket连接时候报java.io.IOException: read failed, socket might closed or timeout, read ret: -1错误,下面说一下我的解决方法,希望对各位有一点帮助。


private BluetoothSocket mSocket;
<span style="white-space:pre">	</span>private InputStream mInputSream;
<span style="white-space:pre">	</span>private UUID mUUID = UUID
<span style="white-space:pre">			</span>.fromString("00001101-0000-1000-8000-00805F9B34FB");

//找到蓝牙设备并判断是否连接上蓝牙,并创建socket
BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
		Set<BluetoothDevice> sets = adapter.getBondedDevices();
		Iterator<BluetoothDevice> iterator = sets.iterator();
		while (iterator.hasNext()) {
			BluetoothDevice device = iterator.next();
			if (mUtils.isConnected(device))
				try {
					mBluetoothDevice = device;
					mSocket = mBluetoothDevice
							.createRfcommSocketToServiceRecord(mUUID);


接下来就是socket的连接了,本来我是在一个子线程中做的这些:

public void run() {
		
				try {
					if (mSocket != null)
						mSocket.connect();
					if (mSocket != null) {
						mInputSream = mSocket.getInputStream();
						mIsRunning = true;
					}
					while (mIsRunning) {
						byte[] buffer = new byte[16];
						while (mInputSream != null
								&& mInputSream.read(buffer) > 0 && mIsRunning) {
							String val = new String(buffer);
							Log.i("SPP", val);
							Intent intent = new Intent();
							if (val.contains("+PTT=P")) {
								intent.setAction("android.intent.action.PTT.down");
							} else if (val.contains("+PTT=R")) {
								intent.setAction("android.intent.action.PTT.up");
							}
							mContext.sendBroadcast(intent);
							Arrays.fill(buffer, (byte) 0);
						}
					}

				} catch (IOException e) {
					try {
						if (mInputSream != null)
							mInputSream.close();
						if (mSocket != null) {
							mSocket.close();
							mSocket = null;
						}
					} catch (Exception e2) {
						// TODO: handle exception
					}
				}
		
	}
但是这样在socket连接的时候还是会报java.io.IOException: read failed, socket might closed or timeout, read ret: -1错误,

查了各种资料也没找到解决方法,<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">经过自己多次实验发现在</span>
mSocket.connect()时候还需要在另一个子线程中处理才正常连接上接收到指令,也就是如下代码:
<pre name="code" class="java">public void run() {
		new Thread(new Runnable() {

			@Override
			public void run() {
				try {
					if (mSocket != null)
						mSocket.connect();
					if (mSocket != null) {
						mInputSream = mSocket.getInputStream();
						mIsRunning = true;
					}
					while (mIsRunning) {
						byte[] buffer = new byte[16];
						while (mInputSream != null
								&& mInputSream.read(buffer) > 0 && mIsRunning) {
							String val = new String(buffer);
							Log.i("SPP", val);
							Intent intent = new Intent();
							if (val.contains("+PTT=P")) {
								intent.setAction("android.intent.action.PTT.down");
							} else if (val.contains("+PTT=R")) {
								intent.setAction("android.intent.action.PTT.up");
							}
							mContext.sendBroadcast(intent);
							Arrays.fill(buffer, (byte) 0);
						}
					}

				} catch (IOException e) {
					try {
						if (mInputSream != null)
							mInputSream.close();
						if (mSocket != null) {
							mSocket.close();
							mSocket = null;
						}
					} catch (Exception e2) {
						// TODO: handle exception
					}
				}
			}
		}).start();
	}

这里只是找到了解决方法,但是还不知道原因,也查了各种资料,没有得到为什么在子线程中做,connect的时候还需要再开一个子线程。

 


  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 8
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值