【发布时间】:2016-04-21 09:30:58
【问题描述】:
我正在尝试创建一个允许 NativeScript 应用程序连接到 MQTT 服务器的插件。当我尝试运行应用程序时,我的应用程序中出现以下错误:
java.lang.RuntimeException:无法启动活动 组件信息{org.nativescript.testMQTT/com.tns.NativeScriptActivity}: com.tns.NativeScriptException: 调用 js 方法 onCreate 失败
TypeError:无法读取未定义文件的属性“paho”:“, 行:1,列:265
StackTrace: Frame: function:'NativeScriptMQTTClient', 文件:'/data/data/org.nativescript.testMQTT/files/app/tns_modules/nativescript-mqtt/mqtt.js', 行:8,列:44 帧:函数:'', 文件:'/data/data/org.nativescript.testMQTT/files/app/main-page.js', 行:4,列:14 框架:函数:'require',文件:'',行:1, 列:266 帧:函数:'global.loadModule', 文件:'/data/data/org.nativescript.testMQTT/files/app/tns_modules/globals/globals.js', 行:19,列:16 帧:函数:'resolvePageFromEntry', 文件:'/data/data/org.nativescript.testMQTT/files/app/tns_modules/ui/frame/frame-common.js', 行:72,列:40 帧:函数:'Frame.navigate', 文件:'/data/data/org.nativescript.testMQTT/files/app/tns_modules/ui/fr
我在我的 mqtt.android.ts 文件中将以下代码与 Paho Java 库一起使用:
export class NativeScriptMQTTClient {
private _topic: String;
private _content: String;
private _qos: number;
private _broker: String;
private _clientId: String;
private _persistance: org.eclipse.paho.client.mqttv3.persist.MemoryPersistance;
private _client: org.eclipse.paho.client.mqttv3.MqttClient;
private _connectOptions: org.eclipse.paho.client.mqttv3.MqttConnectOptions;
constructor(topic: String, qos: number, broker: string, clientId: String) {
this._topic = topic;
this._qos = qos;
this._broker = broker;
this._clientId = clientId;
this._persistance = new org.eclipse.paho.client.mqttv3.persist.MemoryPersistance();
}
connect() {
this._client = new org.eclipse.paho.client.mqttv3.MqttClient(this._broker, this._clientId, this._persistance);
this._connectOptions = new org.eclipse.paho.client.mqttv3.MqttConnectOptions();
this._connectOptions.setCleanSession(true);
console.log("Connecting to the broker: " + this._broker);
this._client.connect(this._connectOptions);
console.log("Connected to the broker: " + this._broker);
console.log("Publishing message: Hello from NativeScript (Morné)");
this._client.publish(this._topic, "Hello from NativeScript (Morné)");
console.log("Published message: Hello from NativeScript (Morné)");
console.log("Disconnecting from the broker: " + this._broker);
this._client.disconnect();
console.log("Disconnected from the broker: " + this._broker);
}
}
我的 main-page.ts 文件中有以下代码:
import {NativeScriptMQTTClient} from "nativescript-mqtt";
let client: NativeScriptMQTTClient = new NativeScriptMQTTClient("MQTT Examples", 2, "tcp://iot.eclipse.org:1883", "NativeScriptClient");
client.connect();
任何帮助将不胜感激。
【问题讨论】:
-
您是否在插件的platforms/Android/libs 中包含了库jar 文件(org.eclipse.paho.client.mqttv3-1.0.2.jar)
标签: javascript android mqtt nativescript paho