这是一篇笔记哈,记录怎么在Cordova上使用Java语言的Jar包。
首先,在Plugin中引入jar包,并且定义好target-dir
<source-file src="src/android/lib/element-connector-1.0.4.jar" target-dir="libs" />
<source-file src="src/android/lib/californium-core-1.0.4.jar" target-dir="libs" />
接着就可以在代码中使用了,如下是Cordova与CoAP协议的一个简单接口
package com.phodal.plugin.coap;
import org.apache.cordova.*;
import org.json.JSONArray;
import org.json.JSONException;
import org.eclipse.californium.core.CoapClient;
import org.eclipse.californium.core.CoapResponse;
import java.net.URI;
import java.net.URISyntaxException;
public class Coap extends CordovaPlugin {
@Override
public boolean execute(String action, JSONArray data, CallbackContext callbackContext) throws JSONException, URISyntaxException {
if (action.equals("get")) {
try {
URI uri = new URI("coap://iot.eclipse.org:5683/");
CoapClient mCoapClient = new CoapClient(uri);
CoapResponse response = mCoapClient.get();
callbackContext.success(response);
return true;
} catch (Exception e) {
}
} else {
return false;
}
}
}
围观我的Github Idea墙, 也许,你会遇到心仪的项目