我目前正在使用 Kotlin 开发一个支持 BLE 的 Android 应用程序,目标是 API 27。
我正在尝试覆盖 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 */
}
原谅我的天真,我不经常使用 Kotlin/Java,谢谢。
最佳答案
您不应使用此方法,它仅供内部使用,而不是公共(public) API 的一部分。因此它通过 @hide
隐藏。有关 @hide
以及如何访问它的更多信息,请参阅 What does @hide mean in the Android source code?
请注意,使用反射访问它,如上面 is discouraged 链接中所述
您要使用的方法是在 dark-greylist 上使用以下 restrictions :
关于java - 无法在 Kotlin 中覆盖 Java 函数,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/52759890/