JSR82 connect: Connection to the Bluetooth socket is not created (failed or aborted)

2.2k views Asked by At

So there are many SO posts related to this issue -

Other Posts -

Tried everything from reflection to without reflection but none worked -

if(!mDeviceAddress.equals("") && BluetoothAdapter.checkBluetoothAddress(mDeviceAddress))
{
    Log.i(TAG, "Remote Device Name  "+mDeviceName);

    bdDevice = mBluetoothAdapter.getRemoteDevice(mDeviceAddress);
    getConnected(bdDevice);
}


@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
public void getConnected(BluetoothDevice bdDevice)
{           
    if(bdDevice == null)
    {
        setSetting("STATUS", "Disconnected");
        Toast.makeText(getActivity(), 
        "Unable to get Remote Device!", Toast.LENGTH_SHORT).show();
        return;
    }
    else
    {
        Log.i(TAG, "Connecting Address--"+ bdDevice.getAddress());

        boolean isConnected = createInsecureRfcommSocket(bdDevice, 1);          

        if(!isConnected) 
        {
            for(int i=2;i<4;i++)
            {  
                if(!isConnected) 
                    isConnected = createInsecureRfcommSocket(bdDevice, i); 
                else
                    break;
            }
        }

    if(isConnected)
    {
        Log.i(TAG, "Connected Socket");
        setSetting("STATUS", "Connected");

        mConnectedThread = new ConnectedThread(socket);
        mConnectedThread.start();
        timeSyncCommand();
        mConnectedThread.writeByte(runCommand);

        startTime = System.currentTimeMillis();
    }
    else
    {
         try
         {
             socket = bdDevice.
             createInsecureRfcommSocketToServiceRecord(
             UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"));
         }   
         catch(IOException io)
         {
            Toast.makeText(getActivity(), "Socket Create -" 
            + io.toString() , Toast.LENGTH_SHORT).show();
         }
         try
         {
            mBluetoothAdapter.cancelDiscovery();
            socket.connect();
         }
         catch(IOException io)
         {
            Log.i(TAG, "Socket Connect -"+io.toString());
         }

         if(socket.isConnected())
         {
            Log.i(TAG, "Connected Socket");
            setSetting("STATUS", "Connected");

            mConnectedThread = new ConnectedThread(socket);
            mConnectedThread.start();
            timeSyncCommand();
            mConnectedThread.writeByte(runCommand);

            startTime = System.currentTimeMillis();
         }
         else
         {
            Log.i(TAG, "Disconnected Socket");
            setSetting("STATUS", "Disconnected");
         }

       }    
     }
    }


    @TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
    public boolean createInsecureRfcommSocket(BluetoothDevice bdDevice, int i)
     {
         try 
        {
            Log.i(TAG,
            "Creating RFCOMM socket using reflection with Object "+i);
            //socket = bdDevice.createRfcommSocketToServiceRecord(my_UUID);
            Method m = bdDevice.getClass().
            getMethod("createInsecureRfcommSocket", new Class[] {int.class});
            socket = (BluetoothSocket) m.invoke(bdDevice, i);
            mBluetoothAdapter.cancelDiscovery();

            Log.i(TAG,"Attempt to connect to a remote device");
            socket.connect();
        }
        catch(IOException e)
        {
            setSetting("STATUS", "Disconnected");
            Log.i(TAG,"Exception raised "+e.getMessage());

            try 
            {
                socket.close();
                Log.i(TAG,
                "Cannot connect with address "+bdDevice.getAddress());
                e.printStackTrace();
            } 
            catch (IOException e1) 
            {
                Log.i(TAG,"Socket not closed");
                e1.printStackTrace();
            }   
        }
        catch (NoSuchMethodException e1) 
        {
            Log.i(TAG,"NoSuchMethodException");

            e1.printStackTrace();
        }
        catch (InvocationTargetException e2) 
        {
            Log.i(TAG,"InvocationTargetException");

            e2.printStackTrace();
        }
        catch (IllegalAccessException e3) 
        {
            Log.i(TAG,"IllegalAccessException");

            e3.printStackTrace();
        }
        catch (NullPointerException e4) 
        {
            Log.i(TAG,"NullPointerException");

            e4.printStackTrace();
        }
    }

If you see carefully to the above code then you notice -

1) Tried reflection with port 1

2) If 1 fails then tried reflection with port 2

3) If 2 fails then tried reflection with port 3

4) If 3 fails then tried without reflection

In short, used everything but none worked.

My exception trace -

Remote Device Name  RN-IAP-E281
Connecting Address--00:06:68:4D:E2:81
Creating RFCOMM socket using reflection with Object 1
Attempt to connect to a remote device
Exception raised [JSR82] connect: Connection is not created (failed or aborted).
Cannot connect with address 00:06:68:4D:E2:81
 Creating RFCOMM socket using reflection with Object 2
 Attempt to connect to a remote device
Exception raised [JSR82] connect: Connection is not created (failed or aborted).
Cannot connect with address 00:06:68:4D:E2:81
Creating RFCOMM socket using reflection with Object 3
Attempt to connect to a remote device
Exception raised [JSR82] connect: Connection is not created (failed or aborted).
Cannot connect with address 00:06:68:4D:E2:81
Disconnected Socket
------onReceive BroadcastReceiver------
Received Bluetooth Disconnected Request
------Returned from broadcast after disconnect------

Any help will be appreciated!

0

There are 0 answers