近期项目中连接蓝牙之后接收蓝牙设备发出的指令功能,在连接设备之后,创建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. 使用GetLogicalDriveStrings获取卷标

    #include <windows.h> #include <stdio.h> #define BUFSIZE 512 int main() { TCHAR szTemp[BU ...

  2. R-FCN:Object Detection via Region-based Fully Convolutional Networks

    fast.faster这些网络都可以被roi-pooling层分成两个子网络:1.a shared,'fully convolutional' subnetwork 2.an roi-wise sub ...

  3. Webstorm安装、破解、使用

    Webstorm是专用于web开发的号称最好的的编辑器,界面美观大方,有黑.白和经典三大主题可选,使用起来整体感觉良好,破解也不是很麻烦. 一.安装 1.去官网下载正版安装包(建议所有软件都去官网下载 ...

  4. apt-get update 报错 W: Unknown Multi-Arch type 'no' for package 'compiz-core'

    源 #deb包 deb http://mirrors.aliyun.com/ubuntu/ xenial main restricted universe multiverse deb http:// ...

  5. winfrom 多线程 并发 和定时任务

    static void Main(string[] args) { //多个线程修改同一个值,使用lock锁解决并发 for (int i = 0; i < 4; i++) { Task.Fac ...

  6. BZOJ 3876 支线剧情 有源汇有上下界最小费用可行流

    题意: 给定一张拓扑图,每条边有边权,每次只能从第一个点出发沿着拓扑图走一条路径,求遍历所有边所需要的最小边权和 分析: 这道题乍一看,可能会想到什么最小链覆盖之类的,但是仔细一想,会发现不行,一是因 ...

  7. Jdk动态代理和CGLIB动态代理大比拼

    前言: 这2种动态代理算是老生常谈的吧,面试还是会经常问到的,下面做下分析: jdk动态代理: import java.lang.reflect.InvocationHandler; import j ...

  8. mybatis中<![CDATA[]]>的作用

    此篇文章引自QH_JAVA的文章 在使用mybatis 时我们sql是写在xml 映射文件中,如果写的sql中有一些特殊的字符的话,在解析xml文件的时候会被转义,但我们不希望他被转义,所以我们要使用 ...

  9. ORM之连表操作

    ORM之连表操作 -----------------------------连表的正向操作------------------------- 在models.py中创建两张表UserType和User ...

  10. 怎样用JMeter做接口测试?

    本文介绍JMeter如何做web service测试,一般来说web服务,一般指的是HTTP请求相关的内容.这里就介绍一下如何利用JMeter做基于HTTP的web接口测试.接口也叫API(Appli ...