6

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.

amitchone
  • 1,630
  • 3
  • 21
  • 45
  • 1
    What exactly prevents you from overriding the method? Also, in your sample, I see a typo: `fun onConnectionUpdate` instead of `fun onConnectionUpdated`. – hotkey Oct 11 '18 at 12:20
  • forgot `override` keyword? – leonardkraemer Oct 11 '18 at 12:20
  • I've corrected the typo. I should've mentioned, when I add the `override` keyword it simply says `onConnectionUpdated overrides nothing`. – amitchone Oct 11 '18 at 12:21
  • Because `BluetoothGattCallback` doesn't have such a function in the first place. https://developer.android.com/reference/android/bluetooth/BluetoothGattCallback – Wackaloon Oct 11 '18 at 12:22
  • @AlexanderAgeychenko but it's available in the source, so why am I unable to access it? – amitchone Oct 11 '18 at 12:23
  • When I run my app in debug mode, the following is printed to the console: `D/BluetoothGatt: onConnectionUpdated() - Device=XX:XX:XX:XX:XX:XX interval=9 latency=0 timeout=600 status=0`, so it seems stupid not to have any access to this function at all. – amitchone Oct 11 '18 at 12:24
  • No, it's not, read the docs on the official site. Don't forget, it is an android package so it depends on the platform and android version. – Wackaloon Oct 11 '18 at 12:24

1 Answers1

11

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.

Community
  • 1
  • 1
leonardkraemer
  • 6,573
  • 1
  • 31
  • 54
  • Thanks for the explanation as to why it's unavailable. Is there *any* way to access the connection interval? The Android device is the central in this case and the peripheral to which it connects is dependent on knowing the connection interval. There's no way to read this value on the peripheral side, so is there literally *no* way to access this const on the central side? – amitchone Oct 11 '18 at 12:30
  • Ahh, I see. Reflection. Thanks. – amitchone Oct 11 '18 at 12:32
  • All the best stuff has `@hide` .. :( be wary of using these via reflection, a lot of implementation uses in AOSP check that the calling process has permission to do so .. maybe ok here, but just something to be aware of. – Mark Oct 11 '18 at 12:36
  • I'm still having no luck using reflection to access the function, any pointers? – amitchone Oct 11 '18 at 13:48
  • Nice answers you got! – GhostCat Dec 04 '18 at 10:29