I have an Issue with Android 4 (ICS) My Bluetooth-App connecting to a Serial Device works like a charm on Android 3.
However, when using Android 4, it shows the "Pairing" Dialog everytime I connect to the (already paired) device.
The user has to reenter the same pin over and over again. Is there some way to suppress this behavoir in Android 4? Is it a new bug? Is there a work around? Does the BluetoothDevice need some kind of adaption to Android 4? What am I doing wrong?
/**
* 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;
}
Thanks in advance
BTW: How can I ensure every other connection from my Tablet to this bluetoothDevice is stopped before connecting?