Developing OPC UA applications in Android Studio
There have recently been some questions on how to get started in developing OPC UA applications using Android Studio and Prosys OPC UA Java SDK. There has been a similar blog post earlier on OPC UA Android development. However, it focuses on Eclipse based development and some of the details in it are outdated. This short tutorial explains how you can start developing OPC UA applications in Android Studio and provides an example application that is a simple OPC UA Client. The application is compiled against Android SDK version 26. You can download the project here.
Set up and creating a project
To start developing, you will first need to install Android Studio and Android SDK. You will also need to have OPC UA Java Stack and Prosys OPC UA Java SDK as .jar files. You can request an evaluation version and purchase the latter on our website. Additionally, Spongy Castle (core, prov and bcpkix-jdk15on packages, download here) will be needed for cryptography and SLF4J Android (download here) for logging. You can also use the older 18.104.22.168 version of Spongy Castle in which case you can use the .jar files (named sc-core, scpkix and scprov) provided with SDK.
Create a new project in Android Studio with the New Project wizard, and add an Empty Activity to the project. If you named the activity as MainActivity and the layout as activity_main, MainActivity.java will be the main activity of the application and activity_main.xml the corresponding layout file that defines the UI of the activity.
After the project is created, add all the .jar files to the lib folder of the project. Then, add them to Gradle build dependencies as compile files dependencies pointing to the locations of the .jar files. If you use the example project, remove the comments from the lines that define the dependencies and check that the versions are correct. Also, the following code needs to be added to the AndroidManifest.xml file under the manifest tag in order to allow the application to create a connection.
Creating an OPC UA Client
Now that the project is created and all .jar files are added, the actual application can be developed. This tutorial will explain how the example OPC UA Client is developed, but does not go further into Android development nor explain deeply the features of the OPC UA Client. The steps in creating the app are as follows. First, the UI is created. Second, the application logic is created. Third, the UI and the logic are bound together.
In the automatically created main layout file, a button and a text view are defined using IDs connectButton and textView, respectively. Then, the actual OPC UA application logic is defined in the MainActivity using features of the SDK. This is performed inside an instance of AsyncTask class, which enables performing background operations and publishing results on UI thread. The task consists of creating an OPC UA Client that connects to a server, reads the current time and then disconnects. This may be a somewhat lengthy process, which is why it is better to run it on the background rather than on the UI thread. Another reason to use AsyncTask is that the UI thread does not allow any networking operations on Android versions 3.0 and above (see here for more information).
The server URI given to the UaClient object is opc.tcp://10.0.2.2:53530/OPCUA/SimulationServer, which means that the client will connect to SimulationServer running on the host machine of the Android emulator you are using (if you are running the application on an emulator). You can also connect to, for example, the Prosys demo server at opc.tcp://uademo.prosysopc.com:53530/OPCUA/SimulationServer. Before connecting to the server, some more settings are added to the client (such as ApplicationDescription and ApplicationIdentity).
The UI and the application logic are now defined, so it is time to bind them together, as mentioned before. In MainActivity, the findViewById method returns the button and the text view instances using the previously defined IDs. Using a click listener, clicking the button creates the AsyncTask and executes it. When the doInBackground method returns the current time read from the server (or an error message if an exception is caught), onPostExecute method sets the result of the background computation to the text view, and the result can be seen in the UI.
This tutorial showed merely the basics you need to know to start developing your OPC UA applications using Prosys OPC UA Java SDK in Android Studio. To further develop your OPC UA application, check out the tutorials to get deeper knowledge of the use of the SDK as well as the Android API guides to learn more about how to use this application framework to build your app.
Expertise and responsibility areas: OPC UA product development
Prosys is a leading provider of professional OPC software and services with over 10 years of experience in the field. OPC and OPC UA (Unified Architecture) are communications standards used especially by industrial and high-tech companies.
Newest blog posts
Security vulnerability in Java based OPC UA applications.
Azure and Power Bi offer a straightforward and powerful way to visualize data from your OPC UA servers.
OPC UA is getting popular in real-world installations and also going towards real-time with the latest announcements about support for field level.