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 ...
随机推荐
- 火狐删除配置文件 会删除目录下所有文件 切记不要把配置文件建立在桌面 恢复软件:易我数据恢复向导 9.0 DiskGenius500
火狐删除配置文件 会删除目录下所有文件 切记不要把配置文件建立在桌面 恢复软件:易我数据恢复向导 9.0 DiskGenius500 结果:由于时间比较常 恢复文件均失败了~
- 纯CSS3来自定义单选框radio与复选框checkbox
单选框(radio)自定义样式 ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 3 ...
- 复制Windows的等宽字体到Linux
1.从Windows的Fonts目录下复制字体 2.在Linux的/usr/share/fonts目录下创建子目录例如:sudo mkdir /usr/share/fonts/win 3.复制字体到该 ...
- 笔试算法题(43):布隆过滤器(Bloom Filter)
议题:布隆过滤器(Bloom Filter) 分析: BF由一个很长的二进制向量和一系列随机映射的函数组成,通过多个Hash函数将一个元素映射到一个Bit Array中的多个点,查询的时候仅当所有的映 ...
- [JOYOI] 1051 选课
题目限制 时间限制 内存限制 评测方式 题目来源 1000ms 131072KiB 标准比较器 Local 题目描述 学校实行学分制.每门的必修课都有固定的学分,同时还必须获得相应的选修课程学分.学校 ...
- <Linux> 下安装和卸载JDK
安装 下载jdk https://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html 在local ...
- Bootstrap-Table 总结
Bootstrap-Table 总结 jQuery Java Bootstrap-Table JS文件 传参:直接将需要的参数置于 queryParams 方法中,例如 line:formData注意 ...
- ubuntu 安装 navicat
下载navicat解压到opt目录 创建桌面快捷方式sudo vim /usr/share/applications/navicat.desktop [Desktop Entry] Encoding= ...
- FreeRTOS--疑难杂症
花了3个晚上,把这个章节看完,受益匪浅. 最有用的应该是与中断相关的错误,优先排查中断优先级设置. 堆栈溢出检查,可能用到,一般先把堆栈设置的足够大,只要没有溢出就是好事,溢出了,掌握了栈溢出钩子函数 ...
- 【05】AJAX实例-检测用户名是否存在(实例)
AJAX实例-检测用户名是否存在 用户注册时,需要填写个人信息,其中包括用户名.当用户输入完成时,JavaScript 需要及时检测用户名是否存在,如果存在给出提示,请用户更换用户名. 当然,这个 ...