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的时候还须要再开一个子线程。
java.io.IOException: read failed, socket might closed or timeout, read ret: -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 ...
- android java.io.IOException: open failed: EBUSY (Device or resource busy)
今天遇到一个奇怪的问题, 测试在程序的下载界面,下载一个文件第一次下载成功,删除后再下载结果下载报错, 程序:file.createNewFile(); 报错:java.io.IOException: ...
- github提交失败并报错java.io.IOException: Authentication failed:
一.概述 我最近在写一个android的项目. 软件:android studio.Android studio VCS integration(插件) Android studio VCS inte ...
- java.io.IOException: open failed: EACCES (Permission denied)问题解决
1. 问题描述:在Android中,用程序访问Sdcard时,有时出现“java.io.IOException: open failed: EACCES (Permission denied)&qu ...
- 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 ...
- 关于 java.io.IOException: open failed: EACCES (Permission denied)
今天解决了一个问题,不得不来和大家分享.就是关于 java.io.IOException: open failed: EACCES (Permission denied)的问题,网上也有很多人把这个问 ...
- 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 ...
- Android - Error: "java.io.IOException: setDataSource failed.: status=0x80000000"
Error: "java.io.IOException: setDataSource failed.: status=0x80000000" 本文地址: http://blog.c ...
- "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 ...
随机推荐
- 背景渐变 background-image:linear-gradient(0deg,#fff,#ccc);
背景渐变 background-image:linear-gradient(0deg,#fff,#ccc);
- vue脚手架工具vue-cli
一.什么 是脚手架工具vue-cli? 类似于工人手里面的脚手架一样,帮助工人搭架子用,同样的vue脚手架工具也是帮助我们更好更快速的开发代码的工具 二.vue-cli能做什么? 三.vue-cli安 ...
- 09Windows编程
Windows编程 2.1 窗口 Windows应用程序一般都有一个窗口,窗口是运行程序与外界交换信息的界面.一个典型的窗口包括标题栏.最小化按钮.最大/还原按钮.关闭按钮.系统菜单图标.菜 ...
- js 发送短信验证码倒计时
html <input type="button" id="btn" value="免费获取验证码" onclick="se ...
- Android实战简易教程-第四十九枪(两种方式实现网络图片异步加载)
加载图片属于比较耗时的工作,我们需要异步进行加载,异步加载有两种方式:1.通过AsyncTask类进行:2.通过Handler来实现,下面我们就来看一下如何通过这两种方式实现网络图片的异步加载. 一. ...
- Linux系统用户、组和权限管理
一.用户与组 1.用户与组的概念 在linux系统中,根据系统管理需要将用户分为三种类型: 1.超级用户:root是linux系统的超级用户,对系统拥有绝对权限.由于root用户权限太大,只有在进行系 ...
- Laravel核心解读 -- 扩展用户认证系统
扩展用户认证系统 上一节我们介绍了Laravel Auth系统实现的一些细节知道了Laravel是如何应用看守器和用户提供器来进行用户认证的,但是针对我们自己开发的项目或多或少地我们都会需要在自带的看 ...
- PHP:Mysql 基础类
文章来源:http://www.cnblogs.com/hello-tl/p/7592547.html <?php /** * __construct($Mysql_config) 构造函数 $ ...
- C51 定时器/计数器 个人笔记
C51的周期 结构图 两个功能寄存器 51单片机定时/计数器的工作由两个特殊功能寄存器控制.TMOD用于设置其工作方式:TCON用于控制其启动和中断申请. 工作方式寄存器TMOD 其中方式一和方式二常 ...
- postman工具的应用实战(二)
在接口测试工具中,最好的应该是soapui,jmeter,postman,但是soapui需要安装和破解,当然也是有破解版的,但是不够灵活,jmeter工具 做接口测试还是性能测试,功能测试,都是一个 ...