Enterprise XR — Interoperability

Enterprise XR-Part 2, Interoperability is another key aspect when we build enterprise solutions using XR technologies. Read this article to understand interoperability between XR apps and other native apps.

Kuldeep Singh
XRPractices
8 min readMar 28, 2020

--

<< Previous | Next >>

Interoperability is another key aspect when we build enterprise solutions using XR technologies. The enterprises have digital assets in form of mobile apps, libraries, system APIs, and they can’t just throw away these existing investments, in fact, the XR apps must be implemented in such a way that it is interoperable with them seamlessly.

In the previous post, we have implemented an android app integrated with organizations’ web-based MFA.

This article describes interoperability between XR apps and other native apps. It takes Unity as the XR app development platform and explains how a Unity-based app can be interoperable with the existing Java-based android libraries.

Read the below article by Raju Kandaswamy, it describes steps to integrate the XR app built using Unity into any android app.

The article below describes interoperability in the opposite of what Raju has described, we will try to integrate an existing Android Library into the Unity-based XR App.

Build an Android Library

In the last article, we have implemented a user authenticator. Let’s now expose a simple user authenticator implementation as an android library.

  1. File > New > New Module > Select Android Library

2. Implement a UserAuthenticator with simple authentication logic. Each method is accepting an argument context, but currently, it is not being used so you may ignore it for now.

3. Go to Build > Make Module “userauthenticator” — It will generate aar file. under build/outputs/aar

This is the library that can be integrated with Unity in C#. The next section describes how it can be used in Unity.

Build an XR app in Unity

Let’s now build the XR app in unity to access the API exposed by the android library implemented in the last step. Unity refers to the external libraries as a plugin.

1. Create unity project —

2. We have 3 APIs in UserAuthenticator of the android library, let's build a 3D UI layout with buttons, text elements. as follows. The source code is shared at the end of the article.

3. Add a script ButtonController. This script describes the ways to access java classes.

UnityEngine.AndroidJavaClass - Load a given class in Unity, using reflection we may invoke any static method of the class.

UnityEngine.AndroidJavaObject - Keep the java object and automatically dispose of it once it's done. Using reflection we can invoke any method of the object.

4. Attached the ButtonController script to an empty game object. Link all the buttons, texts, and input field components.

5. Copy the android library in /Assets/Plugins/Android folder.

6. Switch to Android as a Platform if not already done. — Go to File > Build Settings > Select Android as a Platform > Click of Switch Platform.

7. Update the player settings — Go to File > Build Settings > Player Settings correct the package name and min API level in “Other Settings”

8. Connect your android device, and click on Build and Run. or export the project and test it in Android studio.

Type in the user name, and click on login, it will call the Android API from the library and show the value returned by the library in the text next to the button.

Save way button works, test the logic you have written in the library.

With this, we are done with basic integration. The next section describes an approach with callback methods.

Integration using Callbacks.

Callbacks are important, as the android system may not be always running result synchronously, there are multiple event and listeners which works on callbacks.

  1. Let’s implement a callback interface in the android library.

2. Update the UserAuthenticator implementation to call the OnSuccess and OnFailure Callbacks.

3. Build the Android library again and replace it in Unity project Assets/Plugins/Android

4. Update corresponding in ButtonController of Unity. Implement the Java interface using UnityEngine.AndroidJavaProxy.

The response callback implementation sets the color in the text fields.

Code Branch till this point is added here — Android Library, Unity App

In the next section, we will discuss more complex integration with native components and intents.

Single Sign-On Integration using device native WebView

In this section, we will integrate the android's web view with the unity app, and also see invocation using Intents.

Refactor Android Library

Let’s refactor the android app we have implemented the last article, and use it as a library. Also, learn to pass the arguments to Intent and gets the response from it.

  1. Wrap the WebView invocation in an activity — It takes URL, returnURI as input. It intercepts the returnURI and parse the “code” from it and returns in a ResultReceiver callback.

2. User Authenticators Login method invokes the web-view activity with an SSO URL, and callback.

3. CustomReceiver.OnReceivedResult gets an access_token using the “code” returned by the web-view activity. access_token is then saved in the UserAuthenticator.

4. The access_token is used in getting the user name from the Azure Graph API. This completes the refactoring. The source code is attached at the end.

All the HTTP communications are wrapped in a utility class HttpAuth, this completes the refactoring of the android library. Since we have added a new activity don’t forget to add it in the android manifest.

Unity App Invoking Native WebView in Android Libary

This example is now doing SSO with the organization’s active directory (AD), the username and password will be accepted at the organization’s AD page.

  1. Let’s change the layout a bit. We don’t need an input field now.

2. There is no major change in ButtonContoller, expect the signature change for the library APIs. Now android handles the intent in a separate thread, and callback in unity may not access the game object elements as they may be disabled when another activity spawned on. Status of unity element may be checked in the main thread. The callbacks now updating few helper variables and the update method updates the unity elements in main there whenever there is any change in the state.

3. Since the Android library now have an Activity, and it requires a dependency “androidx.appcompat:appcompat:1.1.0 in Gradle build.

We need to use a custom Gradle template in Unity, Go to File > Build Settings > Player Settings > Publishing Settings. Choose Custom Gradle Template. It will generate a template file in Plugins.

Add the following dependency in the Gradle template. Make sure don’t change **DEPS**

Build/Export the project and run on Android. You have completed the native integration. Here is a demo of integrated SSO in Unity.

Source code can be found at following git repo — Android Library, Unity App

Conclusion

This article describes the basics of interoperability between XR apps and android native libraries. In the next article, we will how to share the user context across XR apps.

<< Previous | Next >>

--

--

Kuldeep Singh
XRPractices

Engineering Director and Head of XR Practice @ ThoughtWorks India.