一种解决Android进行蓝牙连接时发生socket已关闭或超时错误的方法

在进行树莓派与android蓝牙通信时遇到一个难以解决的错误:

java.io.IOException: read failed, socket might closed or timeout, read ret: -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();// 引用来说明要调用的是外部类的方法 run
                return;
            }

在网上找了很多方法,比如修改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号端口:

    # 创建一个服务器套接字,用来监听端口
    server_socket = bluetooth.BluetoothSocket(bluetooth.RFCOMM);
    # 允许任何地址的主机连接,未知参数:1(端口号,通道号)
    server_socket.bind(("", 1))
    # 监听端口/通道
    server_socket.listen(1);

所以修改代码如下:

try {
                // Connect the device through the socket. This will block
                // until it succeeds or throws an exception
                mmSocket.connect();
            } catch (IOException connectException) {
                // Unable to connect; close the socket and get out
                try {
                    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;
            }

猜你喜欢

转载自blog.csdn.net/vic_torsun/article/details/79650865