近期项目中连接蓝牙之后接收蓝牙设备发出的指令功能,在连接设备之后,创建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. ubuntu 18.04 安装.net core

    要求某些软件包保持现状,就是它们破坏了软件包间的依赖关系那就只好在琢磨了,然后就发现了在github有安装的方法因为是18.04 所以 wget -qO- https://packages.micro ...

  2. 第3节 mapreduce高级:12、mapreduce相关的参数调整

    5.1 多job串联 一个稍复杂点的处理逻辑往往需要多个mapreduce程序串联处理,多job的串联可以借助mapreduce框架的JobControl实现 示例代码: ControlledJob ...

  3. 诊断:CLSRSC-400: A system reboot is required to continue installing.

    Linux7.5安装Grid Infrastructure 12.2.0.1时,在root.sh时会报错 2018/01/30 09:19:28 CLSRSC-330: Adding Clusterw ...

  4. ''tensorflow.python.framework.errors_impl.ResourceExhaustedError: OOM when allocating tensor with shape[?]'' 错误分析

    这是tensorflow 一个经常性错误,错误的原因在于:显卡内存不够. 解决方法就是降低显卡的使用内存,途径有以下几种措施: 1 减少Batch 的大小 2 分析错误的位置,在哪一层出现显卡不够,比 ...

  5. 关于Map的遍历

    想起之前有人问过我这个,那就顺手写一下Map的遍历 Map<Integer, String> map = new HashMap<Integer, String>(); map ...

  6. Python 面向对象 组合-多态与多态性-封装-property

    面向对象-组合 1.什么是组合 组合指的是某一个对象拥有一个属性,该属性的值是另外一个类的对象 class Foo: xxx = 111 class Bar: yyy = 222 obj = Foo( ...

  7. PHP解惑(一)

    PHP给人的印象是入门简单的语言.当你的技术能力达到一定阶段时,会发现情况并非如此. PHP采用"极简主义",就是以入门容易为准则设计的,在十几年的持续发展历程中,它早已成为一个开 ...

  8. MongoDB安装与配置启动

    1.下载安装包.mongodb-linux-x86_64-rhel62-3.6.3.tgz 2.解压.修改名字. 3.修改配置文件: # mongodb.conf #where to loglogpa ...

  9. xshell连接不了虚拟机处理方法(错误提示:Connection closing...Socket close.Connection closed by foreign host.Disconnected from remote host(localhost) at 08:47:23.)

    一.问题描述:xshell连接不了虚拟机,出现错误提示:Connection closing...Socket close.Connection closed by foreign host.Disc ...

  10. python3.x Day6 socketserver

    socketserver是啥? 简化了编写网络服务器,就是对于socket的再一次封装sockerserver包含4个类可以使用:A=socketserver.TCPServer() #用于TCP/I ...