2016-04-21 45 views
0

我试图创建一个允许NativeScript应用程序连接到MQTT服务器的插件。当我尝试运行应用程序,我得到我的应用程序出现以下错误:创建Android NativeScript插件

java.lang.RuntimeException: Unable to start activity ComponentInfo{org.nativescript.testMQTT/com.tns.NativeScriptActivity}: com.tns.NativeScriptException: Calling js method onCreate failed

TypeError: Cannot read property 'paho' of undefined File: ", line: 1, column: 265

StackTrace: Frame: function:'NativeScriptMQTTClient', file:'/data/data/org.nativescript.testMQTT/files/app/tns_modules/nativescript-mqtt/mqtt.js', line: 8, column: 44 Frame: function:'', file:'/data/data/org.nativescript.testMQTT/files/app/main-page.js', line: 4, column: 14 Frame: function:'require', file:'', line: 1, column: 266 Frame: function:'global.loadModule', file:'/data/data/org.nativescript.testMQTT/files/app/tns_modules/globals/globals.js', line: 19, column: 16 Frame: function:'resolvePageFromEntry', file:'/data/data/org.nativescript.testMQTT/files/app/tns_modules/ui/frame/frame-common.js', line: 72, column: 40 Frame: function:'Frame.navigate', file:'/data/data/org.nativescript.testMQTT/files/app/tns_modules/ui/fr

我用下面的代码与泛美卫生组织的Java库在我mqtt.android.ts文件:

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); 
    } 
} 

而且我在我的主page.ts文件下面的代码:

import {NativeScriptMQTTClient} from "nativescript-mqtt"; 

let client: NativeScriptMQTTClient = new NativeScriptMQTTClient("MQTT Examples", 2, "tcp://iot.eclipse.org:1883", "NativeScriptClient"); 
client.connect(); 

任何帮助将不胜感激。

+0

你是否在你的插件的平台/ Android /库中包含了库jar文件(org.eclipse.paho.client.mqttv3-1.0.2.jar) –

回答

1

基于MQTT API参考我注意到以下

MemoryPersistence 并在你的代码是MemoryPersist 一个 NCE

this._persistance = new org.eclipse.paho.client.mqttv3.persist.MemoryPersistence(); 

请确保您已包含在你的.jar文件平台/ Android /库 (link to MQTT jar file v1.0.2

你的package.json应该看起来像这样 (确保你有mqtt.android。 JS文件中,如果你在打字稿代码transpiled插件)

{ 
    "name": "nativescript-mqtt", 
    "version": "0.0.3", 
    "main": "mqtt.js", 
    "nativescript": { 
    "platforms": { 
     "tns-android": { 
     "version": "1.7.1" 
     }, 
     "tns-ios": { 
     "version": "1.7.0" 
     } 
    } 
    } 
} 

你也应该reffer到你的插件像

var mqttModule = require("nativescript-mqtt"); 

let client = new mqttModule.NativeScriptMQTTClient("MQTT Examples", 2, "tcp://test.mosquitto.org:1883", "NativeScriptClient"); 

在这一点上我收到

JS: Connecting to the broker: tcp://test.mosquitto.org:1883 

从这一点开始,您应该将连接方法导出为正确的d.ts文件和逻辑。希望这些信息能帮助你与你合作!

+0

编辑的package.json –

+1

谢谢修复它。现在试图找出如何实现它的iOS的一部分。 – MorneZaayman