Developing OPC UA Applications for Android

The Prosys OPC UA SDK for Java allows for the development of OPC UA Applications on Android. This updated guide walks you through the essential steps to use the Prosys OPC UA SDK for Java on Android while providing a simplistic sample client implementation using Android SDK API 35. It should be noted, that testing of the Prosys OPC UA SDK for Java on Android is limited, and thus guarantees on interoperability cannot be fully made.

In the version 5.4.0 of the SDK, SpongyCastle has been removed as a required dependency. For more information, please check our release notes.

Requirements

Adding dependencies and permissions

After installing Android studio, downloading Prosys OPC UA SDK for Java and the sample project, it is necessary to add the SDK as a dependency to the project.  This can be done either by directly adding and referencing the jars in the project or alternatively by using a local maven repository. In the sample, the local maven repository method is used.

To add the SDK to the local maven repository/cache, run

				
					cd {ua_java_sdk_root}\maven-integration\maven-install-helper
mvn install
				
			

In the sample, we then add the SDK as a dependency, alongside the slf4j-android package to build.gradle which provides android bindings for the logging of the SDK. META_INF/DEPENDENCIES also need to excluded from compilation. Make sure that the dependency you add matches the correct edition and version of the one you have available. Also note for the sample to work as-is, version 5.4.0 or higher of the SDK is required.

				
					...
android {
    ...
    packagingOptions {
        exclude 'META-INF/DEPENDENCIES'
    }
}

dependencies {
    ...
    implementation('org.slf4j:slf4j-android:1.7.36')
    implementation('com.prosysopc.ua:prosys-opc-ua-sdk-client-server:5.4.0-201')
    ...
}
				
			

We also need to make sure that mavenLocal is included as a repository in settings.gradle

				
					...
dependencyResolutionManagement {
    ...
    repositories {
        ...
        mavenLocal()
        ...
    }
}
...
				
			

Finally, we need to allow Internet access in AndroidManifest.xml

				
					<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools">
    <uses-permission android:name="android.permission.INTERNET"/>
    <application>
        ...
    </application>

</manifest>
				
			

Client implementation

The sample code creates a UA client that connects to Prosys OPC UA Simulation Server running on the localhost 

				
					//10.0.2.2 points to loopback of host machine
client = new UaClient("opc.tcp://10.0.2.2:53530/OPCUA/SimulationServer");

				
			

A certificate store and validator are also created using the application filepath

				
					                final PkiDirectoryCertificateStore applicationCertificateStore = new PkiDirectoryCertificateStore(getFilesDir().getPath() + "/PKI/CA");
                final PkiDirectoryCertificateStore applicationIssuerCertificateStore =
                        new PkiDirectoryCertificateStore("PKI/CA/issuers");

                // CertificateValidator defines the details about how to trust previously untrusted Applications
                final DefaultCertificateValidator certValidator =
                        new DefaultCertificateValidator(applicationCertificateStore, applicationIssuerCertificateStore);
                // Set validator to accept CA certificates without CRLs
                certValidator.getIgnoredChecks().add(DefaultCertificateValidator.IgnoredChecks.IGNORE_CA_MISSING_CRL);
                client.setCertificateValidator(certValidator);
				
			

An application identity is then assigned to the client, which then connects and reads a value from the server

				
					ApplicationIdentity identity = ApplicationIdentity.loadOrCreateCertificate(
        appDescription, "Sample Organisation", null,
        new File(applicationCertificateStore.getBaseDir(), "private"), true);

identity.setApplicationDescription(appDescription);
client.setApplicationIdentity(identity);
client.setTimeout(60000);
client.setSecurityMode(SecurityMode.BASIC128RSA15_SIGN_ENCRYPT);
client.setUserIdentity(new UserIdentity());
client.connect();
DataValue dv = client.readValue(Identifiers.Server_ServerStatus_CurrentTime);

				
			

Running the application

The sample can be ran using the Device Emulator provided by Android Studio. Create a device with matching API level, run Gradle Sync, and run the application on the emulated device.  In the sample application, clicking connect will attempt to connect to the specified server and read the server timestamp value. A successful read will show the timestamp value below the connect button.

Android phone screenshot showing OPC UA demo app with a Connect button.

The certificate provided by the client needs to be trusted on the server side. If using Prosys OPC UA Simulation Server, you can do this by going to the Certificates-tab, right-clicking on the SimpleAndroidClient row.

Prosys OPC UA Simulation Server showing certificate details and Trust option for SimpleAndroidClient.

Conclusion

In this article, we have shown the basics on incorporating Prosys OPC UA SDK for Java to an Android project to create a simple OPC UA Client. 

Headshot of Luukas Lusetti

Luukas Lusetti

Software Engineer

Email: luukas.lusetti@prosysopc.com

Related Posts

Interested in this topic?

Get updated about new posts through our newsletter!