带传输参数的 BluetoothDevice.ConnectGatt() [英] BluetoothDevice.ConnectGatt() with transport parameter

查看:39
本文介绍了带传输参数的 BluetoothDevice.ConnectGatt()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚开始使用 Android,并使用蓝牙 LE 在 Android Studio 中设置了一个 API 21 项目.

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

深入BluetoothDevice 向我展示了ConnectGatt() 方法的两个签名:

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

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

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

我想使用第二个,但构建失败:

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

错误:(127, 26) 错误:BluetoothDevice 类中的方法connectGatt不能应用于给定类型;必需的:上下文、布尔值、BluetoothGattCallback 发现:Context,boolean,BluetoothGattCallback,int 原因:实际和正式参数列表的长度不同

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

编译器设置似乎与 Android Studio 中的源代码不匹配.

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

我该如何解决这个问题?

How can I fix this?

推荐答案

如果您想使用隐藏的 API,您可以调用您想使用的方法.但是您必须记住,隐藏的 API 可以随时更改.您必须自行承担使用它的风险.

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.

这是如何使用隐藏的 connectGatt() 方法的示例代码.

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
        }

这篇关于带传输参数的 BluetoothDevice.ConnectGatt()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆