2016-04-21 46 views
3

擴展時BluetoothGattCallback應該實現者調用超級方法嗎?Android的BluetoothGatt超級方法?

實施例:

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

    ... 
} 

回答

1

在這種情況下,它似乎沒有必要調用super方法。 BluetoothGattCallback類是抽象的,它的所有方法都是空的。

編輯:

BluetoothGattCallback是Android SDK和的本地層的上方部分。 它的方法從BluetoothGatt類中調用,該類處理來自本地層的回調並將它們轉換爲對BluetoothGattCallback方法的調用。

如果您想知道爲什麼BluetoothGattCallback是一個具有空白實現而不是接口的抽象類。這可能是因爲它有太多的回調方法,所以實現一個接口會導致代碼中不必要的膨脹。

EDIT2:

這個官方引用是有點難以得到的。硬件製造商的Compatiblity definition聲明他們必須以Android SDK中聲明的形式實現Android API。

請注意,下層本機層通過Android的Binder機制連接。

+0

這可能是因爲執行是按照製造商本土?這是我的恐懼。有一些本地底層實施 – Alix

+0

我不認爲這會是一個問題。肯定有一個下層的本地層,但是這已經在'BluetoothGatt'類中處理了。我用更多的細節更新了答案。 – Tomik

+0

太好了,我接受了這個答案,但是你能否提供一些官方的參考資料? – Alix

0

看完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) { 
} 
+0

是的,在上面接受的答案中有關於此的討論 – Alix