一种解决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;
            }
  • 6
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 9
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

丧心病狂の程序员

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值