2013-03-13 47 views
8

我正在開發Android應用程序。這個應用程序應該與藍牙(BT)設備通信(發送一些字節)。我在我的設備(Samsung Galaxy mini)上調試/運行此應用時遇到問題。當我創建BT套接字並停止調試時,電話會凍結,我必須通過取出電池重新啓動它。在運行這個應用程序的情況下(從Eclipse)一切正常,但是當我嘗試再次運行它時,電話凍結和應用程序未安裝。如果我在第二次運行之前嘗試卸載此應用程序,手機會再次凍結。這是一個有問題的代碼:藍牙插槽凍結手機

private final BluetoothDevice mmDevice; 
private UUID uuid; 

public ConnectionThread(BluetoothDevice device) { 
    Log.d(TAG, "create ConnectionThread"); 

    uuid = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"); 
    BluetoothSocket tmp = null; 
    mmDevice = device; 

    try { 
     tmp = mmDevice.createRfcommSocketToServiceRecord(uuid); 
    } catch (IOException e) { } 
    mmSocket = tmp; 
    socketConnected = true; 
} 

這是線程的構造函數。當我評論線

tmp = mmDevice.createRfcommSocketToServiceRecord(uuid); 

手機不凍結,所以問題是與創建套接字(不連接)。每次調試或運行後重新啓動手機都非常煩人,我還得做很多工作。

如果我從手機上運行這個應用程序(從Eclipse斷開連接),它沒有任何問題。任何想法可能是一個問題或如何解決它?謝謝。

+3

聽起來像一個固件錯誤,不是嗎? – 2013-03-13 11:15:13

+0

@CodePainters:固件或IDE錯誤。我發現了一個相同的主題:http://stackoverflow.com/questions/4408287/android-bluetooth-socket-freeze-application。所以如果我在onDestroy回調中關閉BT,一切都可以。 – DanielH 2013-03-13 12:51:28

+1

IDE?不太可能。而且Android無論如何都充滿了bug ... – 2013-03-13 12:54:50

回答

0

我正在使用SGSIII mini進行開發。下面的代碼很適合我:

private class ConnectThread extends Thread { 
    private final BluetoothSocket mmSocket; 
    private final BluetoothDevice mmDevice; 

    public ConnectThread(BluetoothDevice device) { 
     mmDevice = device; 

     BluetoothSocket tmp = null; 

     // Get a BluetoothSocket for a connection with the 
     // given BluetoothDevice 
     try { 
      //tmp = device.createRfcommSocketToServiceRecord(MY_UUID); 
      tmp = device.createInsecureRfcommSocketToServiceRecord(MY_UUID); 
     } catch (IOException e) { 
      Log.e(LOG_TAG, "create() failed", e); 
     } 
     mmSocket = tmp; 

     Main.myBluetoothSocket = mmSocket; 
     Main.myBluetoothDevice = mmDevice; 
    } 

    @Override 
    public void run() { 
     Log.i(LOG_TAG, "BEGIN mConnectThread"); 
     setName("ConnectThread"); 

     // Always cancel discovery because it will slow down a connection 
     mAdapter.cancelDiscovery(); 

     // Send a failure message back to the Activity 
     Message msg = mHandler.obtainMessage(MESSAGE_TOAST); 
     Log.e(LOG_TAG, "Attempting connection to " + mmSocket.getRemoteDevice().getName()); 
     String ss = "Attempting connection to " + mmSocket.getRemoteDevice().getName(); 
     Bundle bundle = new Bundle(); 
     bundle.putString(TOAST, ss); 
     msg.setData(bundle); 
     mHandler.sendMessage(msg); 

     // Make a connection to the BluetoothSocket 
     try { 
      // This is a blocking call and will only return on a 
      // successful connection or an exception 
      mmSocket.connect(); 
     } catch (IOException e) { 
      Log.e(LOG_TAG, "*+*+*+* Connection Failed"); 
      connectionFailed(); 
      // Close the socket 
      try { 
       mmSocket.close(); 
      } catch (IOException e2) { 
       Log.e(LOG_TAG, "unable to close() socket during connection failure", e2); 
      } 
      // Start the service over to restart listening mode 
      BluetoothCommandService.this.start(); 
      return; 
     } 

     // Reset the ConnectThread because we're done 
     synchronized (BluetoothCommandService.this) { 
      mConnectThread = null; 
     } 

     // Start the connected thread 
     connected(mmSocket, mmDevice); 
    } 

    public void cancel() { 
     try { 
      mmSocket.close(); 
     } catch (IOException e) { 
      Log.e(LOG_TAG, "close() of connect socket failed", e); 
     } 
    } 
} 
0

,我也面臨着你可以使用反射法同樣的問題,將工作

Method m = device.getClass().getMethod("createRfcommSocket", new Class[] {int.class}); 
BluetoothSocket socket = socket = (BluetoothSocket) m.invoke(device, 1);