2012-06-03 173 views
2

我有安卓4.0(ICS) 我的藍牙應用程序連接到串行設備的問題就像在Android魅力3.Android 4.0的藍牙配對:需要在使用時輸入的密碼每次

然而, Android 4,每當我連接到(已配對的)設備時,它會顯示「配對」對話框。

用戶必須重複輸入同一個引腳。 有沒有辦法在Android 4中抑制這種行爲?這是一個新的錯誤嗎?有沒有解決辦法? BluetoothDevice是否需要適應Android 4?我究竟做錯了什麼?

/** 
* Start the ConnectThread to initiate a connection to a remote bluetoothDevice. 
*/ 
public synchronized InitResponse init() { 
    if (D) Log.d(TAG, "init to: " + bluetoothDevice); 

    setState(State.CONNECTING); 

    try { 
     if (D) Log.i(TAG, "Trying to create RfcommSocket using reflection"); 
     Method m = bluetoothDevice.getClass().getMethod("createRfcommSocket", 
       new Class[] { int.class }); 
     bluetoothSocket = (BluetoothSocket)m.invoke(bluetoothDevice, 1); 
    } catch (Exception e) { 
     if (D) Log.w(TAG, "Reflection failed.", e); 
     if (D) Log.i(TAG, "Trying to create RfcommSocket using UUID"); 
     try { 
      bluetoothSocket = bluetoothDevice.createRfcommSocketToServiceRecord(READER_UUID); 
     } catch (IOException e2) { 
      Log.e(TAG, "create() failed", e2); 
      disconnect(); 
      return InitResponse.READER_NOT_FOUND; 
     } 
    } 

    if (D) Log.i(TAG, "Bluetooth Socket: " + bluetoothSocket); 
    if (D) Log.i(TAG, "Trying to connect."); 

    try { 
     bluetoothAdapter.cancelDiscovery(); 
     if (D) Log.i(TAG, "Cancelled discovery."); 
     if (D) Log.i(TAG, "Attempting to connect."); 
     bluetoothSocket.connect();     // Causes pairing dialog everytime I call it. 
    } catch (IOException e) { 
     if (D) Log.w(TAG, "Error while connecting."); 
     // Close the socket 
     try { 
      bluetoothSocket.close(); 
     } catch (IOException e2) { 
      Log.e(TAG, "unable to close() socket during connection failure", e2); 
     } finally { 
      disconnected(); 
     } 
     return InitResponse.CONNECTION_ERROR; 
    } 

    if(D) Log.d(TAG, "Connected to bluetoothReader " + bluetoothDevice + " established."); 

    try { 
     inputStream = bluetoothSocket.getInputStream(); 
     if(D) Log.d(TAG, "Input Stream: " + inputStream); 
     outputStream = bluetoothSocket.getOutputStream(); 
     if(D) Log.d(TAG, "Output Stream: " + outputStream); 
    } catch (IOException e) { 
     Log.e(TAG, "Temp sockets not created", e); 
     disconnected(); 
     return InitResponse.CONNECTION_ERROR; 
    } 

    if (D) Log.i(TAG, "Setting state to CONNECTED"); 

    setState(State.CONNECTED); 
    if (D) Log.i(TAG, "Init successfull."); 
    return InitResponse.SUCCESS; 
} 

在此先感謝

BTW:我怎樣才能確保我的平板電腦這個BluetoothDevice類所有其他連接連接之前,停止?

+0

可能與[這個bug(http://code.google.com/p/android/問題/細節?ID = 40101)。似乎Google喜歡打破藍牙。 –

+0

請在下面的URl找到解決方案,它對我來說工作很好 http://stackoverflow.com/questions/11082819/bluetooth-connection-on-android-ics-not-possible –

回答

1

我不認爲這是你的軟件的問題。在使用ICS的手機後,我看到了RunKeeper和Polar BT心率監視器的問題。我沒有在HTC Desire上購買ROM或CM7的問題。

+0

奇怪的是,這個問題不存在Samsung Galaxy Note ICS(官方) 您是否安裝了CM9或帶有官方ICS的手機?由於只有CM9失敗,我可以愉快地忽略它。 – domenukk

1

有類似的問題:Bluetooth connection on Android ICS not possible

只要設法得到一個不安全插座:

bluetoothSocket = bluetoothDevice.createInsecureRfcommSocketToServiceRecord(READER_UUID); 
+0

感謝您的解決方案,但在我的特殊情況下,我不想離開安全。 (Android Docs聲明_「通信渠道將不具有經過認證的鏈接密鑰,即它將受到中間人攻擊」_) – domenukk