Search code examples
androidbluetoothtimeout

Android pairing does not work - "connection reset by peer"


As the title explains I'm having a hard time sending some data via Bluetooth to my PC. I'm trying to establish a connection with my android phone as client and my PC as server. When I'm trying to actually establish a connection via BluetoothSocket.connect() my phone prompts for a pin. After entering it my PC also prompts for the same pin but before I can enter it, the connect() - method throws an IOException. I assume that the connect()-Method times out before I can enter the correct pin on my PC, but how can I get it to wait long enough for me to enter the PIN?

EDIT: After Re-Pairing the PC with my Phone it worked, because the pairing dialog doesn't appear in my app anymore. If I unpair the PC and start my app, the pairing dialog pops up but disappears after several seconds and the socket throws an exception ("connection reset by peer"). Apparently the connection is reset before the pairing is done, but why?

Here is my code:

private void connectToDevice(BluetoothDevice device)
{
    mBluetoothAdapter.cancelDiscovery();
    BluetoothSocket socket = null;
    try 
    {
        socket = device.createRfcommSocketToServiceRecord(UUID.fromString("00001101-                        0000-  1000-8000-00805F9B34FB"));
    } 
    catch (IOException e) 
    {
        Log.e("HeliRemote", "Couldn't get socket.");
        return;
    }
    try 
    {
        socket.connect();
    } 
    catch (IOException e) 
    {
        try 
        {
            socket.close();
        } 
        catch (IOException e1) 
        {   
            Log.e("HeliRemote", "Couldn't close connection");
        }
        // That's the message I get in LogCat
        Log.e("HeliRemote", "Couldn't connect to Socket.");
        return;
    }

    Log.i("HeliRemote", "connected.");
}

I would be glad if somebody could give me any good words of advice regarding the problem.


Solution

  •     Method m = mBluetoothDevice.getClass().getMethod("createRfcommSocket", new Class[] {int.class});
            mBluetoothSocket = (BluetoothSocket) m.invoke(mBluetoothDevice, 1);
    
    
           // mBluetoothSocket = mBluetoothDevice.createRfcommSocketToServiceRecord(applicationUUID);
            mBluetoothAdapter.cancelDiscovery();
    
            mBluetoothSocket.connect();
    
    
    
    
    
        }
        catch (IOException eConnectException) 
        {
            Log.d(TAG, "CouldNotConnectToSocket", eConnectException);
             closeSocket(mBluetoothSocket);
             return;
        } catch (SecurityException e) {
    
            Log.d(TAG, "CouldNotConnectToSocket", e);
            closeSocket(mBluetoothSocket);
        } catch (NoSuchMethodException e) {
    
            Log.d(TAG, "CouldNotConnectToSocket", e);
            closeSocket(mBluetoothSocket);
        } catch (IllegalArgumentException e) {
            Log.d(TAG, "CouldNotConnectToSocket", e);
            closeSocket(mBluetoothSocket);
        } catch (IllegalAccessException e) {
            Log.d(TAG, "CouldNotConnectToSocket", e);
            closeSocket(mBluetoothSocket);
        } catch (InvocationTargetException e) {
            Log.d(TAG, "CouldNotConnectToSocket", e);
            closeSocket(mBluetoothSocket);
        }
    }
    

    try this one...