一种解决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();// 引用来说明要调用的是外部类的方法 run
                return;
            }
  • 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 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;
            }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值