2016-07-12 105 views
0

我嘗試連接到Android應用程序中的藍牙設備,但遇到問題。 看來,我永遠無法連接到藍牙設備的第一次嘗試。Android藍牙連接無法在第一次嘗試時建立

我在BluetoothConnectThread以下代碼:

public class BluetoothConnectThread extends Thread { 
    private BluetoothSocket mmSocket; 
    private BluetoothDevice mmDevice; 
    private Context context; 
    private BluetoothManager manager; 

    public BluetoothConnectThread(BluetoothDevice mmDevice, UUID uuid, Context context, BluetoothManager manager) { 
     this.context = context; 
     this.manager = manager; 
     this.mmDevice = mmDevice; 
     this.uuid = uuid; 
    } 

    public void run() { 
     try { 
      System.out.println("Try to connect"); 
      mmSocket = (BluetoothSocket) mmDevice.getClass().getMethod("createRfcommSocket", new Class[]{int.class}).invoke(mmDevice, Integer.valueOf(1)); 
      mmSocket.connect(); 
     } catch (Exception connectException) { 
      connectException.printStackTrace(); 
      try { 
       mmSocket.close(); 
       System.out.println("Couldn't establish Bluetooth connection! (1)"); 
      } catch (IOException closeException) { 
       closeException.printStackTrace(); 
       System.out.println("Couldn't establish Bluetooth connection! (2)"); 
      } 

      try { 
       System.out.println("Try to connect again"); 
       mmSocket = (BluetoothSocket) mmDevice.getClass().getMethod("createRfcommSocket", new Class[]{int.class}).invoke(mmDevice, Integer.valueOf(1)); 
       mmSocket.connect(); 
      } catch (Exception connectException2) { 
       connectException.printStackTrace(); 
       try { 
        mmSocket.close(); 
        System.out.println("Couldn't establish Bluetooth connection! (3)"); 
       } catch (IOException closeException) { 
        closeException.printStackTrace(); 
        System.out.println("Couldn't establish Bluetooth connection! (4)"); 
       } 
      } 
     } 

     if(mmSocket.isConnected()) { 
      if(mmSocket.isConnected()) { 
       System.out.println("Connected"); 
    //Do something with the connected socket 

調用運行方法時,收到以下的日誌:

07-12 14:17:10.906 9941-10518/com.example.niekdewit.test I/System.out: Try to connect 
07-12 14:17:10.910 9941-10518/com.example.niekdewit.test W/BluetoothAdapter: getBluetoothService() called with no BluetoothManagerCallback 
07-12 14:17:13.025 9941-10518/com.example.niekdewit.test W/System.err: java.io.IOException: read failed, socket might closed or timeout, read ret: -1 
07-12 14:17:13.025 9941-10518/com.example.niekdewit.test W/System.err:  at android.bluetooth.BluetoothSocket.readAll(BluetoothSocket.java:573) 
07-12 14:17:13.025 9941-10518/com.example.niekdewit.test W/System.err:  at android.bluetooth.BluetoothSocket.waitSocketSignal(BluetoothSocket.java:550) 
07-12 14:17:13.025 9941-10518/com.example.niekdewit.test W/System.err:  at android.bluetooth.BluetoothSocket.connect(BluetoothSocket.java:325) 
07-12 14:17:13.025 9941-10518/com.example.niekdewit.test W/System.err:  at com.example.niekdewit.test.BluetoothConnectThread.run(BluetoothConnectThread.java:33) 
07-12 14:17:13.025 9941-10518/com.example.niekdewit.test I/System.out: Couldn't establish Bluetooth connection! (1) 
07-12 14:17:13.025 9941-10518/com.example.niekdewit.test I/System.out: Try to connect again 
07-12 14:17:13.025 9941-10518/com.example.niekdewit.test W/BluetoothAdapter: getBluetoothService() called with no BluetoothManagerCallback 
07-12 14:17:13.147 9941-9941/com.example.niekdewit.test I/Timeline: Timeline: Activity_idle id: [email protected] time:762005544 
07-12 14:17:16.503 9941-9941/com.example.niekdewit.test I/Timeline: Timeline: Activity_idle id: [email protected] time:762008901 
07-12 14:17:17.279 9941-10518/com.example.niekdewit.test I/System.out: Connected 

我試圖更換2行

mmSocket = (BluetoothSocket) mmDevice.getClass().getMethod("createRfcommSocket", new Class[]{int.class}).invoke(mmDevice, Integer.valueOf(1)); 

mmSocket = mmDevice.createRfcommSocketToServiceRecord(uuid); 

但這也不起作用,它會生成不同的日誌。使用這種方法我也無法連接第二次嘗試。

07-12 14:27:15.968 15135-15622/com.example.niekdewit.test I/System.out: Try to connect 
07-12 14:27:15.969 15135-15622/com.example.niekdewit.test W/BluetoothAdapter: getBluetoothService() called with no BluetoothManagerCallback 
07-12 14:27:18.205 15135-15622/com.example.niekdewit.test W/System.err: java.io.IOException: read failed, socket might closed or timeout, read ret: -1 
07-12 14:27:18.205 15135-15622/com.example.niekdewit.test W/System.err:  at android.bluetooth.BluetoothSocket.readAll(BluetoothSocket.java:573) 
07-12 14:27:18.205 15135-15622/com.example.niekdewit.test W/System.err:  at android.bluetooth.BluetoothSocket.waitSocketSignal(BluetoothSocket.java:550) 
07-12 14:27:18.205 15135-15622/com.example.niekdewit.test W/System.err:  at android.bluetooth.BluetoothSocket.connect(BluetoothSocket.java:325) 
07-12 14:27:18.205 15135-15622/com.example.niekdewit.test W/System.err:  at com.example.niekdewit.test.BluetoothConnectThread.run(BluetoothConnectThread.java:36) 
07-12 14:27:18.205 15135-15622/com.example.niekdewit.test I/System.out: Couldn't establish Bluetooth connection! (1) 
07-12 14:27:18.206 15135-15622/com.example.niekdewit.test I/System.out: Try to connect again 
07-12 14:27:18.206 15135-15622/com.example.niekdewit.test W/BluetoothAdapter: getBluetoothService() called with no BluetoothManagerCallback 
07-12 14:27:18.319 15135-15135/com.example.niekdewit.test I/Timeline: Timeline: Activity_idle id: [email protected] time:762610717 
07-12 14:27:20.669 15135-15622/com.example.niekdewit.test W/System.err: java.io.IOException: read failed, socket might closed or timeout, read ret: -1 
07-12 14:27:20.669 15135-15622/com.example.niekdewit.test W/System.err:  at android.bluetooth.BluetoothSocket.readAll(BluetoothSocket.java:573) 
07-12 14:27:20.669 15135-15622/com.example.niekdewit.test W/System.err:  at android.bluetooth.BluetoothSocket.waitSocketSignal(BluetoothSocket.java:550) 
07-12 14:27:20.669 15135-15622/com.example.niekdewit.test W/System.err:  at android.bluetooth.BluetoothSocket.connect(BluetoothSocket.java:325) 
07-12 14:27:20.669 15135-15622/com.example.niekdewit.test W/System.err:  at com.example.niekdewit.test.BluetoothConnectThread.run(BluetoothConnectThread.java:36) 
07-12 14:27:20.669 15135-15622/com.example.niekdewit.test I/System.out: Couldn't establish Bluetooth connection! (3) 
07-12 14:27:20.854 15135-15135/com.example.niekdewit.test I/Timeline: Timeline: Activity_idle id: [email protected] time:762613252 

當我第一次嘗試用這條線

mmSocket = mmDevice.createRfcommSocketToServiceRecord(uuid); 

連接,並嘗試第二次嘗試(「嘗試重新連接」)這條線

mmSocket = (BluetoothSocket) mmDevice.getClass().getMethod("createRfcommSocket", new Class[]{int.class}).invoke(mmDevice, Integer.valueOf(1)); 

然後生成與我連接的第一個日誌相同的日誌

我不知道發生了什麼事? 我希望你們其中一位能看到我的代碼出了什麼問題,或者指向正確的方向。

編輯: 當設備已經配對,那麼它可能使用這兩種方法的第一次嘗試連接。

EDIT2:

mmSocket = mmDevice.createInsecureRfcommSocketToServiceRecord(uuid); 

也不起作用

回答

0

我會努力改變

mmSocket = (BluetoothSocket) mmDevice.getClass().getMethod("createRfcommSocket", new Class[]{int.class}).invoke(mmDevice, Integer.valueOf(1)); 

這個

// BluetoothDevice device= BluetoothAdapter.getDefaultAdapter().getRemoteDevice("00:1C:4D:02:A6:55"); 
    // You already have a device so capture the method then invoke a socket 
    Method m = device.getClass().getMethod("createRfcommSocket", new Class[] { int.class }); 
    socket = (BluetoothSocket)m.invoke(device, Integer.valueOf(1)); 
    socket.connect(); 
    // Do some cool stuff 

這是我怎麼總是連接到設備

+0

感謝您的回答,但這並沒有解決我的問題。輸出日誌與我附加到我的文章中的第一個日誌相同:( – Niek

+0

你有一個藍牙管理器已定義,但你不使用它。我可能會看看,因爲你得到一個錯誤'W/BluetoothAdapter:getBluetoothService()調用沒有BluetoothManagerCallback' –

+0

藍牙管理器是我自己寫的一個類,它充當了一個藍牙連接等活動的接口。 我在線程類中使用它來告訴管理器連接有已製作: – Niek