【发布时间】:2018-10-11 12:17:39
【问题描述】:
我目前正在使用 Kotlin 开发面向 API 27 的支持 BLE 的 Android 应用。
我正在尝试覆盖android.bluetooth.BluetoothGatt 中的一个函数。有许多回调可被覆盖以启用某些 BLE 事件的处理。
例如,我通过以下方式覆盖onConnectionStateChange():
private val bluetoothGattCallback = object : BluetoothGattCallback() {
override fun onConnectionStateChange(gatt: BluetoothGatt, status: Int, newState: Int) {
/* do stuff */
}
这很好用。
我的问题源于试图覆盖 onConnectionUpdated()。这个回调的定义方式和BLE API source中的onConnectionStateChange()一样,那我怎么不能覆盖呢?这就是我试图覆盖它的方式(仍在BluetoothGattCallback() 对象内):
fun onConnectionUpdated(gatt: BluetoothGatt, interval: Int, latency: Int, timeout: Int, status: Int) {
/* do stuff */
}
编辑:我忘了提到,当我添加
override关键字时,它会提供错误消息:OnConnectionUpdated overrides nothing.。
原谅我的幼稚,我不经常使用 Kotlin/Java,谢谢。
【问题讨论】:
-
究竟是什么阻止您覆盖该方法?另外,在您的示例中,我看到一个错字:
fun onConnectionUpdate而不是fun onConnectionUpdated。 -
忘记了
override关键字? -
我已更正错字。我应该提到,当我添加
override关键字时,它只是说onConnectionUpdated overrides nothing。 -
因为
BluetoothGattCallback一开始就没有这样的功能。 developer.android.com/reference/android/bluetooth/… -
@AlexanderAgeychenko 但它在源代码中可用,为什么我无法访问它?
标签: java android kotlin bluetooth-lowenergy