我有安卓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類所有其他連接連接之前,停止?
可能與[這個bug(http://code.google.com/p/android/問題/細節?ID = 40101)。似乎Google喜歡打破藍牙。 –
請在下面的URl找到解決方案,它對我來說工作很好 http://stackoverflow.com/questions/11082819/bluetooth-connection-on-android-ics-not-possible –