一種解決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;
            }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章