javaandroidkotlinuwb

How to use Android UWB API in Java


I'm trying to implement an UWB ranging app for Android in java. However, the the UWB API seems to be Kotlin only and I have a hard time integrating it into my existing java code.

The UWB API seems to be pretty straight forward (see Android UWB Guide):

  1. get ranging parameters
  2. create sessionScope
  3. start ranging

However, when using Java, I fail at step 2. What I've tried is:

UwbManager uwbManager = UwbManager.createInstance(context);
UwbControllerSessionScope uwbControllerSessionScope = (UwbControllerSessionScope) uwbManager.controllerSessionScope();

which doas not compile because uwbManager.controllerSessionScope() actually requires some Continuation object as a parameter. Then I tried to pass an anonymous Continuation object (altough I have no idea whether this code makes any sense, but thats a different problem):

UwbManager uwbManager = UwbManager.createInstance(context);
               UwbControllerSessionScope uwbControllerSessionScope = (UwbControllerSessionScope) uwbManager.controllerSessionScope(new Continuation<UwbControllerSessionScope>() {
                   @NonNull
                   @Override
                   public CoroutineContext getContext() {
                       return EmptyCoroutineContext.INSTANCE;     //what does this mean?
                   }

                   @Override
                   public void resumeWith(@NonNull Object o) {
                       //TODO ???
                   }
               });

Now, the app crashes because uwbManager.controllerSessionScope() does not return a UwbControllerSessionScope object, but instead a `kotlin.coroutines.intrinsics.CoroutineSingletons``object. At this point, it seems to me that the UWB feature is not compatible with java. Am I missing something?

I know that Android UWB is still in alpha, but it still appears odd to me that it is only supported by Kotlin.


Solution

  • I worked on Integrating the UWB into Android. For that, I created the library in Java.OOB mechanisms based on Ble to exchange the BLE required Ranging parameters.

    Link: https://github.com/ajayvamsee/Uwb-Library

    Under the uwbControl package: UwbRangingController.java is the main file to start Ranging like the base class to use the library.

    Under manager package: UwbManagerHelper.java have core uses of UWB API in Android. Go through this class you will get our solution.

    Dependencies:

    implementation "androidx.core.uwb:uwb-rxjava3:1.0.0-alpha04"
    implementation "androidx.core.uwb:uwb:1.0.0-alpha04"