Android BluetoothGatt super 方法?

标签 android android-bluetooth

当扩展 BluetoothGattCallback 时,实现者应该调用 super 方法吗?

例子:

public void onCharacteristicWrite(BluetoothGatt gatt,
                                  BluetoothGattCharacteristic characteristic, 
                                  int status) {
     // Is this needed?
     super.onCharacteristicWrite(gatt, characteristic, status);

     ...
}

最佳答案

在这种情况下,似乎没有必要调用 super 方法。 BluetoothGattCallback 类是抽象的,它的所有方法都是空的。

编辑:

BluetoothGattCallback 是 Android SDK 的一部分,位于 native 层之上。 它的方法是从 BluetoothGatt 类调用的,该类处理来自 native 层的回调并将它们转换为对 BluetoothGattCallback 方法的调用。

如果您想知道为什么 BluetoothGattCallback 是一个具有空实现的抽象类而不是接口(interface)。可能是因为它有太多的回调方法,所以实现一个接口(interface)会导致代码不必要的膨胀。

编辑2:

这方面的官方引用并不难获得。 Compatiblity definition硬件制造商声明他们必须以 Android SDK 中声明的形式实现 Android API。

注意底层native层是通过Android的Binder机制连接起来的。

关于Android BluetoothGatt super 方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36768916/

相关文章:

android - Android设备的蓝牙或wifi的MAC地址

android - 如何保持蓝牙连接后台?

java - 如何在FinishInflate 上向该 View 添加 toast

java - 我的 android 项目测试设置中缺少任何内容吗?

安卓蓝牙后台监听

Android BLE Gatt 连接更改状态

android-intent - 拦截蓝牙耳机上的 VOICE_COMMAND

java - 可以在 GridView 单元格中添加 View 吗?

java - 尝试查看用户通过使用数组列表填写了多少编辑文本

android - 自定义 RelativeLayout 以具有圆形边界