android蓝牙开发read failed, socket might closed or timeout, read ret: -1
2016-10-09 15:22
453 查看
read failed, socket might closed or timeout, read ret: -1
这个问题的原因有很多种可能。
一、如果设备1没开启接收服务,设备2连接它理所当然会连接失败,此时当设备1开启服务,设备2再连接也可能会失败,
http://stackoverflow.com/questions/18657427/ioexception-read-failed-socket-might-closed-bluetooth-on-android-4-3
原因:well, i had the same problem with my code, and it's because since android 4.2 bluetooth stack has changed. so my code was running fine on devices with android < 4.2 , on the other devices i was getting the famous exception "read
failed, socket might closed or timeout, read ret: -1"
The problem is with the
When you create your socket using
integer value "-1", and this value seems doesn't work for android >=4.2 , so you need to set it to "1". The bad news is that
accepts UUID as parameter and not
we have to use other aproach. The answer posted by @matthes also worked for me, but i simplified it:
So the code is (for connecting to a SPP on an ELM327 device):
解决方法:
通过反射把端口改成1
二、服务器监听端口可能不是1
http://simonlei.iteye.com/blog/1522196
解决方法:
Method listenMethod = btClass.getMethod("listenUsingRfcommOn", new Class[]{int.class});
BluetoothServerSocket returnValue = ( BluetoothServerSocket) listenMethod.invoke(btAdapter, new Object[]{ 29});
三、设备1在搜索的时候设备2连接它会失败
这个问题的原因有很多种可能。
一、如果设备1没开启接收服务,设备2连接它理所当然会连接失败,此时当设备1开启服务,设备2再连接也可能会失败,
http://stackoverflow.com/questions/18657427/ioexception-read-failed-socket-might-closed-bluetooth-on-android-4-3
原因:well, i had the same problem with my code, and it's because since android 4.2 bluetooth stack has changed. so my code was running fine on devices with android < 4.2 , on the other devices i was getting the famous exception "read
failed, socket might closed or timeout, read ret: -1"
The problem is with the
socket.mPortparameter.
When you create your socket using
socket = device.createRfcommSocketToServiceRecord(SERIAL_UUID);, the
mPortgets
integer value "-1", and this value seems doesn't work for android >=4.2 , so you need to set it to "1". The bad news is that
createRfcommSocketToServiceRecordonly
accepts UUID as parameter and not
mPortso
we have to use other aproach. The answer posted by @matthes also worked for me, but i simplified it:
socket =(BluetoothSocket) device.getClass().getMethod("createRfcommSocket", new Class[] {int.class}).invoke(device,1);. We need to use both socket attribs , the second one as a fallback.
So the code is (for connecting to a SPP on an ELM327 device):
解决方法:
try { socket.connect(); Log.e("","Connected"); } catch (IOException e) { Log.e("",e.getMessage()); try { Log.e("","trying fallback..."); socket =(BluetoothSocket) device.getClass().getMethod("createRfcommSocket", new Class[] {int.class}).invoke(device,1); socket.connect(); Log.e("","Connected"); } catch (Exception e2) { Log.e("", "Couldn't establish Bluetooth connection!"); } }
通过反射把端口改成1
二、服务器监听端口可能不是1
http://simonlei.iteye.com/blog/1522196
解决方法:
Method listenMethod = btClass.getMethod("listenUsingRfcommOn", new Class[]{int.class});
BluetoothServerSocket returnValue = ( BluetoothServerSocket) listenMethod.invoke(btAdapter, new Object[]{ 29});
三、设备1在搜索的时候设备2连接它会失败
相关文章推荐
- run: read failed, socket might closed or timeout, read ret: -1
- java.io.IOException: read failed, socket might closed or timeout, read ret: -1
- java.io.IOException: read failed, socket might closed or timeout, read ret: -1
- java.io.IOException: read failed, socket might closed or timeout, read ret: -1
- java.net.SocketTimeoutException: Read timed out 的解决办法 android平台 腾讯微博开发
- Android开发之蓝牙Socket
- android开发,socket发送文件,read阻塞,得不到文件尾-1
- Android开发之蓝牙Socket
- Android开发之蓝牙Socket
- android 蓝牙开发 蓝牙 bluetoothsocket connect 经常出错 解决方法
- java.net.SocketTimeoutException: Read timed out
- 初涉Android蓝牙开发(转) 收藏以备后用
- Android 蓝牙开发之一设置蓝牙
- Android开发之--WIFI,蓝牙,电池,背光,SD卡,摄像头,按键,MIC,重力感应等各种操作
- Android-BluetoothSocket建立有关蓝牙通讯的套接字
- android ktv 开发过程6-undefined reference to `parse_number_or_die' 。。。。
- Android平台开发-Bluez function porting-蓝牙功能移植
- Android开发之Socket编程
- Android蓝牙开发
- android中基于蓝牙(Bluetooth)技术的开发