2

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?

domenukk
  • 953
  • 15
  • 28
  • Could be related to [this bug](http://code.google.com/p/android/issues/detail?id=40101). Seems like Google loves to break bluetooth. – Jan Schejbal Feb 10 '13 at 17:51
  • please find the solution in below URl, its working fine for Me http://stackoverflow.com/questions/11082819/bluetooth-connection-on-android-ics-not-possible – karthik selvaraj Apr 01 '13 at 10:20

2 Answers2

1

I don't think this is an issue with your software. I am seeing the same problem with RunKeeper and my Polar BT heart rate monitor after I got a phone with ICS. I have not had that problem on HTC Desire with stock ROM or CM7.

Tiny
  • 78
  • 5
  • Strangely enough this issue is not present on Samsung Galaxy Note ICS (official) Do you have CM9 installed or a phone with official ICS aswell? As only CM9 fails for me, I can happily ignore it. – domenukk Jun 16 '12 at 18:08
1

Had similar problem: Bluetooth connection on Android ICS not possible

Just try to get an insecure Socket:

bluetoothSocket = bluetoothDevice.createInsecureRfcommSocketToServiceRecord(READER_UUID);
Community
  • 1
  • 1
  • Thanks for the solution but in my special case I don't want to leave out the security. (Android Docs state that _"The communication channel will not have an authenticated link key i.e it will be subject to man-in-the-middle attacks"_) – domenukk Jun 21 '12 at 23:57