Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
381 views
in Technique[技术] by (71.8m points)

android - BluetoothDevice.ConnectGatt() with transport parameter

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

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

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

and

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

I'd like to use the second one but the build fails:

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

It seems the compiler settings don't match the source code in Android Studio.

How can I fix this?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

If you want to use hidden API's you can invoke the method you want to use. But you have to bear in mind that a hidden API can change at any point. You have to use it at your own risk.

Here is an example code 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
        }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...