Prosys OPC UA SDK for Java支持在Android上开发OPC UA应用程序。本文将介绍在Android上使用Prosys OPC UA SDK for Java的基本步骤,并提供使用Android SDK API 35的简化客户端示例实现。需要注意的是,由于Prosys OPC UA SDK for Java在Android上的测试有限,因此无法完全保证互操作性。
点击了解Prosys OPC UA SDK for Java>>
注意:在SDK5.4.0版本中,SpongyCastle已不再作为必需依赖项。
要求
- Android Studio(包含 SDK)
- 适用于 Java 的 Prosys OPC UA SDK
- Maven 命令行界面
- SimpleAndroidUaClient项目
添加依赖项和权限
安装Android Studio、下载Prosys OPC UA SDK for Java和示例项目后,需要将SDK作为依赖项添加到项目中。可以直接在项目中添加并引用jar文件,也可以使用本地Maven仓库。本示例使用了本地Maven仓库的方法。
要将SDK添加到本地maven存储库/缓存,请运行
cd {ua_java_sdk_root}\maven-integration\maven-install-helper mvn install
在示例中,SDK作为依赖项与 slf4j-android 包一起添加到 build.gradle 中,该依赖项为SDK 的日志记录提供Android绑定。此外,还需要将META_INF/DEPENDENCIES从编译中排除。请确保添加的依赖项与现有的版本号匹配。另请注意,要使示例按原样运行,需要5.4.0或更高版本的SDK。
... 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') ... }
还需要确保mavenLocal作为存储库包含在settings.gradle中
... dependencyResolutionManagement { ... repositories { ... mavenLocal() ... } } ...
最后,需要在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>
客户端实现
示例代码创建一个UA客户端,连接到在本地主机上运行的Prosys OPC UA模拟服务器
//10.0.2.2 points to loopback of host machine client = new UaClient("opc.tcp://10.0.2.2:53530/OPCUA/SimulationServer");
还使用应用程序文件路径创建证书存储和验证器
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);
然后将应用程序标识分配给客户端,然后客户端连接并从服务器读取值
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);
运行应用程序
可以使用Android Studio提供的设备模拟器运行此示例。创建一个具有匹配API级别的设备,运行Gradle Sync,然后在模拟设备上运行应用程序。在示例应用程序中,点击“连接”将尝试连接到指定的服务器并读取服务器时间戳值。读取成功后,时间戳值将显示在“连接”按钮下方。
客户端提供的证书需要在服务器端获得信任。如果使用Prosys OPC UA模拟服务器,可以前往“证书”选项卡,右键单击“SimpleAndroidClient”行来执行此操作。
结论
在本文中,详细介绍了将Prosys OPC UA SDK for Java合并到Android项目以创建简单的OPC UA客户端的基础知识。
慧都科技(EVGET)成⽴于2003年,是⼀家⾏业数字化解决⽅案公司,⻓期专注于软件、油⽓与制造⾏业。公司基于深⼊的业务理解与管理洞察,以系统化的业务建模驱动技术落地,帮助企业实现智能化运营与⻓期竞争优势。
慧都科技作为 Prosys OPC在中国的官方合作伙伴,提供 Prosys OPC产品免费试用,咨询,正版销售等于一体的专业化服务。
如果你希望进一步了解 Prosys OPC 产品、报价,扫描下方二维码或者直接拨打023-68661681联系我们,我们将在第一时间为您提供专业解答!
发表评论