BluetoothDevice.ConnectGatt () with transport parameter

I just started with Android and created an API 21 project in Android Studio using Bluetooth LE.

Digging into BluetoothDevice shows me two signatures of the ConnectGatt () method:

public BluetoothGatt connectGatt(Context context, boolean autoConnect,
                                 BluetoothGattCallback callback)

      

and

public BluetoothGatt connectGatt(Context context, boolean autoConnect,
                                 BluetoothGattCallback callback, int transport)

      

I would like to use the second one, but the build fails:

Error: (127, 26) error: The connectGatt method in the BluetoothDevice class cannot be applied to the specified types; required: Context, boolean, BluetoothGattCallback found: Context, boolean, BluetoothGattCallback, int reason: actual and formal argument lengths differ in length

It seems the compiler settings do not match the source code in Android Studio.

How can I fix this?

+4


source to share


3 answers


If you want to use the hidden API, you can call the method you want to use. But you must remember that the hidden API can change at any time. You must use it at your own risk.

Here is some sample code on how to use the hidden connectGatt () method.



        Method connectGattMethod;
        BluetoothGatt connectGatt;

        try {
            connectGattMethod = device.getClass().getMethod("connectGatt", Context.class, boolean.class, BluetoothGattCallback.class, int.class);
        } catch (NoSuchMethodException e) {
          //NoSuchMethod
        }

        try {
            connectGatt = (BluetoothGatt) connectGattMethod.invoke(device, this, false, mBluetoothGattCallback, 2); // (2 == LE, 1 == BR/EDR)
        } catch (IllegalAccessException e) {
            //IllegalAccessException
        } catch (IllegalArgumentException e) {
            //IllegalArgumentException
        } catch (InvocationTargetException e) {
            //InvocationTargetException
        }

      

+6


source


UPDATE !!! Perhaps I found a way to overcome this shortcoming that worries us so much. As android connects to L2CAP channel 5 by default with iOS fail. This is due to the bit flag in the iOS ad bundle indicating BR / EDR.

After looking through the Android code, I found that the connectGatt () function has a hidden TRAN TRANSPORT variable that will allow you to use only AUTO or a specific LE or BR / EDR.



For some reason this is available in the bluetoothDevice.java file in android studio, but not mentioned in the android code link. When you try to use this, you get an error. If anyone can explain how we can fix the compiler in android studio to compile our code using the TRANSPORT_LE option instead of TANSPORT_AUTO which I believe is used.

Okay, back to work, let it work!

+3


source


I hope this works for you:

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        device.connectGatt(context, true, getBluetoothGattCallback(), BluetoothDevice.TRANSPORT_LE);
    } else {
        device.connectGatt(context, true, getBluetoothGattCallback());
    }

      

0


source







All Articles