创建Android NativeScript插件

时间:2016-04-21 09:30:58

标签: javascript android mqtt nativescript paho

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

  

java.lang.RuntimeException:无法启动活动   ComponentInfo {org.nativescript.testMQTT / com.tns.NativeScriptActivity}:   com.tns.NativeScriptException:调用js方法onCreate失败

     

TypeError:无法读取未定义文件的属性'paho':“,   line:1,column:265

     

StackTrace:Frame:function:'NativeScriptMQTTClient',   文件: '/数据/数据/ org.nativescript.testMQTT /文件/应用/ tns_modules / nativescript-MQTT / mqtt.js',   line:8,column:44 Frame:function:'',   文件: '/数据/数据/ org.nativescript.testMQTT /文件/程序/主page.js',   line:4,column:14 Frame:function:'require',file:'',line:1,   column:266 Frame:function:'global.loadModule',   文件: '/数据/数据/ org.nativescript.testMQTT /文件/应用/ tns_modules /全局/ globals.js',   line:19,column:16 Frame:function:'resolvePageFromEntry',   文件: '/数据/数据/ org.nativescript.testMQTT /文件/应用程序/ tns_modules / UI /帧/帧common.js',   line:72,column:40 Frame:function:'Frame.navigate',   文件:“/数据/数据/ org.nativescript.testMQTT /文件/应用程序/ 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(); 

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:1)

基于MQTT api参考,我注意到以下内容

MemoryPersistence 在你的代码中是MemoryPersist a nce

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

确保您已将.jar文件包含在platforms / Android / libs中 (link to MQTT jar file v1.0.2

你的package.json应该是这样的 (如果你在TypeScript中编码,请确保你有插件中的mqtt.android。 js 文件)

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

你也应该像

那样使用你的插件
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 文件和逻辑导出connect方法。希望信息能帮助您完成项目!