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

时间:2025-02-14 08:06:08

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

: read failed, socket might closed or timeout, read ret: -1

这个错误是出现在:

             try {
                ();// This is a blocking call and will only return on a successful connection or an exception
            }
            catch (IOException e) {
                ("BS", ());
                //connectionFailed();
                try {
                    ();
                } catch (IOException e2) {}

                BluetoothService.this.start();// 引用来说明要调用的是外部类的方法 run
                return;
            }

在网上找了很多方法,比如修改UUID、开启子线程等方法均没有作用,后来我注意到在进行蓝牙串口测试时使用的蓝牙串口助手可以正常连接,于是采用反编译手段学习一下别人的代码。通过jd-gui可以看到,在创建连接这一部分,该软件用了Java的反射机制获取了createRfcommSocket方法,指定连接的是 1号端口:

try
          {
            if ((.this) != null) {
              (.this).connect();
            }
          }
          catch (IOException localIOException1)
          {
            synchronized (a.this)
            {
              for (;;)
              {
                (a.this, null);
                a.this.a((.this), (.this));
                return;
                localIOException1 = localIOException1;
                try
                {
                  ("", "trying fallback...");
                  (.this, (BluetoothSocket)(.this).getClass().getMethod("createRfcommSocket", new Class[] {  }).invoke((.this), new Object[] { (1) }));
                  (.this).connect();
                  ("", "Connected");
                }
                catch (Exception localException)
                {
                  ("", "Couldn't establish Bluetooth connection!");
                  (a.this);
                  try
                  {
                    if ((.this) != null) {
                      (.this).close();
                    }
                    a.this.b();
                    return;
                  }
                  catch (IOException localIOException2)
                  {
                    for (;;)
                    {
                      ("BluetoothChatService", "unable to close() socket during connection failure", localException);
                    }
                  }
                }
              }
            }

仔细查找原因发现,这是因为我们在树莓派上运行的服务程序,绑定在1号端口:

    # 创建一个服务器套接字,用来监听端口
    server_socket = ();
    # 允许任何地址的主机连接,未知参数: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
                ();
            } catch (IOException connectException) {
                // Unable to connect; close the socket and get out
                try {
                    Method m = ().getMethod("createRfcommSocket", new Class[] {int.class});
                    mmSocket = (BluetoothSocket) (mmDevice, 1);
                    ();
                } catch (Exception e) {
                    ("BLUE",());
                    try{
                        ();
                    }catch (IOException ie){}
                }
                return;
            }