Search code examples
androidandroid-fingerprint-api

getFingerprintGestureController is returning null


I'm trying to create an accessibility service that listens for fingerprint gesture swipes, however when I try to detect if gesture detection is available (using the isGestureDectionAvailable method), my app crashes and gives me a null object error, claiming that getFingerprintGestureController() is returning null.

Here is the snippet of code in my service's onCreate method that seems to be causing the error (Kotlin code, so fingerprintGestureController is the getFingerprintGestureController() call):

override fun onCreate() {
    super.onCreate()
    gestureController = fingerprintGestureController
    gestureDetectionAvailable = gestureController.isGestureDetectionAvailable
    if (!gestureDetectionAvailable) {
        return
    }
    …
}

And the error that follows:

java.lang.NullPointerException: Attempt to invoke interface method 'boolean android.accessibilityservice.IAccessibilityServiceConnection.isFingerprintGestureDetectionAvailable()' on a null object reference …

Any idea why getFingerprintGestureController() isn't returning the controller object? I'm testing on a Pixel and have granted the app accessibility access in the settings.


Solution

  • So I figured out what I was doing wrong. I can only call getFingerprintGestureController() once the service has been connected. This means I need to move the problem code to the onServiceConnected() method.

    Furthermore, onServiceConnected() is only called once the accessibility service is given permission by the user enabling it in the settings menu. Once the user enables it there, the service is automatically started.