Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot override Java function in Kotlin

I am currently developing a BLE-enabled Android app targeting API 27 using Kotlin.

I am attempting to override a function within android.bluetooth.BluetoothGatt. There are a number of callbacks available to be overridden to enable the handling of certain BLE events.

For example, I override onConnectionStateChange() in the following way:

private val bluetoothGattCallback = object : BluetoothGattCallback() {

    override fun onConnectionStateChange(gatt: BluetoothGatt, status: Int, newState: Int) {
        /* do stuff */
    }

This works just fine.

My issue stems from trying to override onConnectionUpdated(). This callback is defined in the same way as onConnectionStateChange() in the BLE API source, so how come I can't override it? This is how I am attempting to override it (still within the BluetoothGattCallback() object):

fun onConnectionUpdated(gatt: BluetoothGatt, interval: Int, latency: Int, timeout: Int, status: Int) {
    /* do stuff */
}

EDIT: I forgot to mention that, when I add the override keyword it provides the error message: OnConnectionUpdated overrides nothing..

Forgive my naivety, I don't often work with Kotlin/Java, thanks.

like image 584
amitchone Avatar asked Jan 01 '23 18:01

amitchone


1 Answers

You should not use this method, it is only for internal use and not part of the public API. Therefore it is hidden via @hide. For more information about @hide and how to access it regardless see What does @hide mean in the Android source code?

Note that using reflection to access it as described in the link above is discouraged

The method you want to use is on the dark-greylist with the following restrictions:

dark-greylist:

  • For apps whose target SDK is below API level 28: each use of a dark
    greylist interface is permitted.
  • apps whose target SDK is API level 28 or higher: same behavior as blacklist

blacklist: restricted regardless of target SDK. The platform will behave as if the interface is absent. For example, it will throw NoSuchMethodError/NoSuchFieldException whenever the app is trying to use it, and will not include it when the app wants to know the list of fields/methods of a particular class.

like image 81
leonardkraemer Avatar answered Jan 05 '23 04:01

leonardkraemer