Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android.bluetooth.IBluetooth.createBond() not found in 4.2.1 but works in earlier OS versions

I have some code to automatically pair with a bluetooth device by calling createBond(), registering a broadcast receiver for android.bluetooth.device.action.PAIRING_REQUEST and then manually entering the PIN code to pair.

This has worked great with all devices tested so far up to Andoid 4.0 but today I tried this on my Nexus 7 with Android 4.2.1 and got the following error:

java.lang.noSuchMethodException: android.bluetooth.IBluetooth.createBond

Did they actually remove this function from the library?

UPDATE

What's actually happening is the IBluetooth interface object that I am using to call createBond is not being initialized. In the following code the line that tries to get the IBinder named BTBinder returns null when this process fails causing BTInterface to be set to null at the end. So, my question now is why on my Nexus 7 with Android 4.2.1 does the call to get the binder return null but works correctly on 5 other devices I've tested?

public static IBluetooth getBluetoothInterface()
{
    //Gets a bluetooth interface from private Android system API
    IBluetooth BTInterface = null;

    try
    {
        Class<?> ServiceManager = Class.forName("android.os.ServiceManager");
        Method getService = ServiceManager.getDeclaredMethod("getService", String.class);
        IBinder BTBinder = (IBinder) getService.invoke(null, "bluetooth");
        Class<?> IBluetooth = Class.forName("android.bluetooth.IBluetooth");
        Class<?>[] IBluetoothClasses = IBluetooth.getDeclaredClasses();
        Class<?> IBluetoothClass0 = IBluetoothClasses[0];
        Method asInterface = IBluetoothClass0.getDeclaredMethod("asInterface",IBinder.class);
        asInterface.setAccessible(true);
        BTInterface = (IBluetooth) asInterface.invoke(null, BTBinder);
    }
    catch (Exception e)
    {
        return null;
    }

    return BTInterface;
}
like image 475
CHollman82 Avatar asked Dec 07 '12 17:12

CHollman82


People also ask

How do I find my Bluetooth UUID Android?

Set<BluetoothDevice> devices = BluetoothAdapter. getDefaultAdapter(). getBondedDevices(); BluetoothDevice glass = null; for (BluetoothDevice d : devices { ParcelUuid[] uuids = d. getUuids(); for (ParcelUuid p : uuids) { System.

What is Bluetooth GATT Service Android?

This class provides Bluetooth GATT functionality to enable communication with Bluetooth Smart or Smart Ready devices. To connect to a remote peripheral device, create a BluetoothGattCallback and call BluetoothDevice#connectGatt to get a instance of this class.


2 Answers

 public boolean createBond(BluetoothDevice btDevice)  
        throws Exception  
        { 
            Class class1 = Class.forName("android.bluetooth.BluetoothDevice");
            Method createBondMethod = class1.getMethod("createBond");  
            Boolean returnValue = (Boolean) createBondMethod.invoke(btDevice);  
            return returnValue.booleanValue();  
    }

This worked on 4.2.1 Galaxy Nexus. Haven't tried on Nexus 7, but I had the same issue of MethodNotFoundException when I use IBluetooth method. So it might also fix for Nexus 7 too.

like image 36
Patrick Cho Avatar answered Sep 25 '22 15:09

Patrick Cho


In Android 4.2 they changed the bluetooth stack implementation.

"Android 4.2 introduces a new Bluetooth stack optimized for use with Android devices. The new Bluetooth stack developed in collaboration between Google and Broadcom replaces the stack based on BlueZ and provides improved compatibility and reliability."

There are a lot of bt related things not working even with the public api on Nexus 7.

like image 108
Andrei Adiaconitei Avatar answered Sep 26 '22 15:09

Andrei Adiaconitei