近期项目中连接蓝牙之后接收蓝牙设备发出的指令功能,在连接设备之后,创建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的时候还须要再开一个子线程。



java.io.IOException: read failed, socket might closed or timeout, read ret: -1的更多相关文章

  1. vue app混合开发蓝牙串口连接(报错java.io.IOException: read failed, socket might closed or timeout, read ret: -1;at android.bluetooth.BluetoothSocket.connect at js/BluetoothTool.js:329)

    我使用的uni-app <template> <view class="bluetooth"> <!-- 发送数据 --> <view c ...

  2. android java.io.IOException: open failed: EBUSY (Device or resource busy)

    今天遇到一个奇怪的问题, 测试在程序的下载界面,下载一个文件第一次下载成功,删除后再下载结果下载报错, 程序:file.createNewFile(); 报错:java.io.IOException: ...

  3. github提交失败并报错java.io.IOException: Authentication failed:

    一.概述 我最近在写一个android的项目. 软件:android studio.Android studio VCS integration(插件) Android studio VCS inte ...

  4. java.io.IOException: open failed: EACCES (Permission denied)问题解决

    1.  问题描述:在Android中,用程序访问Sdcard时,有时出现“java.io.IOException: open failed: EACCES (Permission denied)&qu ...

  5. hadoop错误Ignoring exception during close for org.apache.hadoop.mapred.MapTask$NewOutputCollector@17bda0f2 java.io.IOException Spill failed

    1.错误    Ignoring exception during close for org.apache.hadoop.mapred.MapTask$NewOutputCollector@17bd ...

  6. 关于 java.io.IOException: open failed: EACCES (Permission denied)

    今天解决了一个问题,不得不来和大家分享.就是关于 java.io.IOException: open failed: EACCES (Permission denied)的问题,网上也有很多人把这个问 ...

  7. nutch爬取时Exception in thread “main” java.io.IOException: Job failed!

    用cygwin运行nutch 1.2爬取提示IOException: $ bin/nutch crawl urls -dir crawl -depth 3 -topN 10 crawl started ...

  8. Android - Error: &quot;java.io.IOException: setDataSource failed.: status=0x80000000&quot;

    Error: "java.io.IOException: setDataSource failed.: status=0x80000000" 本文地址: http://blog.c ...

  9. "main" java.io.IOException: Mkdirs failed to create /user/centos/hbase-staging (exists=false, cwd=file:/home/centos)

    Exception in thread "main" java.io.IOException: Mkdirs failed to create /user/centos/hbase ...

随机推荐

  1. react router activeclassName 没有显示

    import React, {Component} from 'react' import {NavLink, Switch, Route, Redirect} from 'react-router- ...

  2. Maven常用仓库地址以及手动添加jar包到仓库

    http://www.blogjava.net/fancydeepin 共有的仓库 http://repository.sonatype.org/content/groups/public/http: ...

  3. 第2节 mapreduce深入学习:2、3

    第2节 mapreduce深入学习:2.MapReduce的分区:3.分区案例的补充完成运行实现 在MapReduce中,通过我们指定分区,会将同一个分区的数据发送到同一个reduce当中进行处理,例 ...

  4. returnFloat_thousand() 以万计数 ,如100,结果是0.01

    function returnFloat_thousand(value){ var value=Math.ceil(Math.round(parseFloat(value)*100)/100)/100 ...

  5. 【makefile】symbol <函数> : can't resolve symbol 问题分析

    调试程序的时候,在linux编译器上可以编译通过,但是编译生成的firmware烧录到板子上的后出现以下异常: can't resolve symbol,无法解析元素符号. review一下code, ...

  6. 爬虫框架urllib 之(二) --- urllib基础

    urllib 官方文档:https://docs.python.org/zh-cn/3/library/urllib.html urllib介绍 Urllib是python内置的HTTP请求库,是py ...

  7. 基础知识:if条件、while循环、for循环 相关练习

    1.实现用户输入用户名和密码,当用户名为 seven 且 密码为 123 时,显示登陆成功,否则登陆失败! while True: name = input('请输入用户名:') psw = inpu ...

  8. LIBSVM使用方法及参数设置

    LIBSVM 数据格式需要---------------------- 决策属性 条件属性a 条件属性b ... 2 1:7 2:5 ... 1 1:4 2:2 ... 数据格式转换--------- ...

  9. 【03】HTML head 头部分的标签说明 和 手机头部标签说明

    HTML head 头部分的标签.元素有很多,涉及到浏览器对网页的渲染,SEO 等等,而各个浏览器内核以及各个国内浏览器厂商都有些自己的标签元素,这就造成了很多差异性.移动互联网时代,head 头部结 ...

  10. JavaEE JDBC 事务

    JDBC 事务 @author ixenos 事务 1.概念:我们将一组语句构建成一个事务(trans action),当所有语句顺利执行之后,事务可以被提交(commit):否则,如果其中某个语句遇 ...