Chr*_*n W 4 android bluetooth bluetooth-lowenergy gatt
我刚开始使用Android,并使用蓝牙LE在Android Studio中设置了一个API 21项目.
深入了解BluetoothDevice,向我展示了ConnectGatt()方法的两个签名:
public BluetoothGatt connectGatt(Context context, boolean autoConnect,
BluetoothGattCallback callback)
Run Code Online (Sandbox Code Playgroud)
和
public BluetoothGatt connectGatt(Context context, boolean autoConnect,
BluetoothGattCallback callback, int transport)
Run Code Online (Sandbox Code Playgroud)
我想使用第二个,但构建失败:
错误:(127,26)错误:类BluetoothDevice中的方法connectGatt不能应用于给定类型; required:上下文,布尔值,找到BluetoothGattCallback:Context,boolean,BluetoothGattCallback,int reason:实际和形式参数列表的长度不同
似乎编译器设置与Android Studio中的源代码不匹配.
我怎样才能解决这个问题?
小智 6
如果要使用隐藏的API,可以调用要使用的方法.但是你必须记住隐藏的API可以在任何时候改变.您必须自担风险使用它.
以下是如何使用隐藏的connectGatt()方法的示例代码.
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
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5474 次 |
| 最近记录: |