本文主要是介绍一种解决Android进行蓝牙连接时发生socket已关闭或超时错误的方法,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
在进行树莓派与android蓝牙通信时遇到一个难以解决的错误:
java.io.IOException: read failed, socket might closed or timeout, read ret: -1
- 1
这个错误是出现在:
try {mmSocket.connect();// This is a blocking call and will only return on a successful connection or an exception}catch (IOException e) {Log.d("BS", e.toString());//connectionFailed(this.index);try {mmSocket.close();} catch (IOException e2) {}BluetoothService.this.start();// 引用来说明要调用的是外部类的方法 runreturn;}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
在网上找了很多方法,比如修改UUID、开启子线程等方法均没有作用,后来我注意到在进行蓝牙串口测试时使用的蓝牙串口助手可以正常连接,于是采用反编译手段学习一下别人的代码。通过jd-gui可以看到,在创建连接这一部分,该软件用了Java的反射机制获取了createRfcommSocket方法,指定连接的是 1号端口:
try{if (a.b.a(a.b.this) != null) {a.b.a(a.b.this).connect();}}catch (IOException localIOException1){synchronized (a.this){for (;;){a.a(a.this, null);a.this.a(a.b.a(a.b.this), a.b.b(a.b.this));return;localIOException1 = localIOException1;try{Log.e("", "trying fallback...");a.b.a(a.b.this, (BluetoothSocket)a.b.b(a.b.this).getClass().getMethod("createRfcommSocket", new Class[] { Integer.TYPE }).invoke(a.b.b(a.b.this), new Object[] { Integer.valueOf(1) }));a.b.a(a.b.this).connect();Log.e("", "Connected");}catch (Exception localException){Log.e("", "Couldn't establish Bluetooth connection!");a.c(a.this);try{if (a.b.a(a.b.this) != null) {a.b.a(a.b.this).close();}a.this.b();return;}catch (IOException localIOException2){for (;;){Log.e("BluetoothChatService", "unable to close() socket during connection failure", localException);}}}}}
- 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
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
仔细查找原因发现,这是因为我们在树莓派上运行的服务程序,绑定在1号端口:
# 创建一个服务器套接字,用来监听端口server_socket = bluetooth.BluetoothSocket(bluetooth.RFCOMM);# 允许任何地址的主机连接,未知参数:1(端口号,通道号)server_socket.bind(("", 1))# 监听端口/通道server_socket.listen(1);
- 1
- 2
- 3
- 4
- 5
- 6
- 7
所以修改代码如下:
try {// Connect the device through the socket. This will block// until it succeeds or throws an exceptionmmSocket.connect();} catch (IOException connectException) {// Unable to connect; close the socket and get outtry {Method m = mmDevice.getClass().getMethod("createRfcommSocket", new Class[] {int.class});mmSocket = (BluetoothSocket) m.invoke(mmDevice, 1);mmSocket.connect();} catch (Exception e) {Log.e("BLUE",e.toString());try{mmSocket.close();}catch (IOException ie){}}return;}
这篇关于一种解决Android进行蓝牙连接时发生socket已关闭或超时错误的方法的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!