安卓蓝牙Gatt超级方法



扩展BluetoothGattCallback实现者应该调用超级方法吗?

例:

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

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

编辑:

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

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

编辑2:

对此的官方参考并不难获得。硬件制造商的兼容性定义声明他们必须以 Android SDK 中声明的形式实现 Android API。

请注意,底层原生层是通过 Android 的 Binder 机制连接的。

看了BluetoothGattCallBack的源代码后,似乎没有必要调用super方法。

因为BluetoothGattCallBack是一个抽象类,onCharacteristicWrite也是一个空方法。

这是它的源代码:

/**
 * Callback indicating the result of a characteristic write operation.
 *
 * <p>If this callback is invoked while a reliable write transaction is
 * in progress, the value of the characteristic represents the value
 * reported by the remote device. An application should compare this
 * value to the desired value to be written. If the values don't match,
 * the application must abort the reliable write transaction.
 *
 * @param gatt GATT client invoked {@link BluetoothGatt#writeCharacteristic}
 * @param characteristic Characteristic that was written to the associated
 *                       remote device.
 * @param status The result of the write operation
 *               {@link BluetoothGatt#GATT_SUCCESS} if the operation succeeds.
 */
public void onCharacteristicWrite(BluetoothGatt gatt,
                                  BluetoothGattCharacteristic characteristic, int status) {
}

相关内容

最新更新