官方文檔及資源vue
uni-app 原生插件文檔:https://uniapp.dcloud.io/api/extend/native-plugin
uni-app 原生插件開發文檔:https://nativesupport.dcloud.net.cn/NativePlugin/README
App 離線 SDK 下載地址:https://nativesupport.dcloud.net.cn/AppDocs/download/android
android
App 離線 SDK 下載解壓後,使用 Android Studio
打開UniPlugin-Hello-AS
工程,刪除自帶的三個模塊:json
uniplugin_component
uniplugin_module
uniplugin_richalert
- 添加模塊,將須要調用的 jar 包拷貝到新增模塊的 libs文件夾下,並添加依賴:
- 編寫 js 與 jar包相互調用的中間件:
package com.example.handheld; import android.util.Log; import com.alibaba.fastjson.JSONObject; import com.cmcid.lib.Common; import com.cmcid.lib.Device; import com.cmcid.lib.OnHandleListener; import com.cmcid.lib.Power; import com.taobao.weex.annotation.JSMethod; import com.taobao.weex.bridge.JSCallback; import com.taobao.weex.common.WXModule; public class HandheldModule extends WXModule { private Device device; private Beep beep; public static final String TAG = "Lu Chang li"; private JSCallback handleCallback; OnHandleListener onHandleListener = new OnHandleListener() { @Override public void onDown() { if (handleCallback == null) return; mWXSDKInstance.runOnUiThread(new Runnable() { @Override public void run() { JSONObject data = new JSONObject(); data.put("code", "down"); // invokeAndKeepAlive 能夠持續回調, invoke只能回調一次 handleCallback.invokeAndKeepAlive(data); } }); } @Override public void onUp() { if (handleCallback == null) return; mWXSDKInstance.runOnUiThread(new Runnable() { @Override public void run() { JSONObject data = new JSONObject(); data.put("code", "up"); handleCallback.invokeAndKeepAlive(data); } }); } }; /** * 初始化 */ @JSMethod public void initHandheld(JSCallback callback) { device = Device.getInstance(mWXSDKInstance.getContext()); device.setOnHandleListener(onHandleListener); JSONObject result = new JSONObject(); if (device.connect()) { result.put("code", "success"); } else { result.put("code", "failure"); } callback.invoke(result); beep = new Beep(mWXSDKInstance.getContext()); } /** * 設置監聽器 */ @JSMethod public void setOnHandleListener(JSCallback callback) { this.handleCallback = callback; } /* * 設置功率 */ @JSMethod public void setPower(JSONObject option, JSCallback callback) { Power power = JSONObject.parseObject(option.toJSONString(), Power.class); JSONObject result = new JSONObject(); try { device.setPower(power); result.put("code", "success"); beep.playOk(); } catch (Exception e) { e.printStackTrace(); result.put("code", "failure"); beep.playError(); } callback.invoke(result); } }
- 註冊中間件:
在app/assets/dcloud_uniplugins.json
文件中添加:
"plugins": [ { "type": "module", "name": "HandheldModule", "class": "com.example.handheld.HandheldModule" } ]
- 在 vue 中調用 jar 包中的方法:
<script> var handheld = uni.requireNativePlugin("HandheldModule") const defaultPower = { db: 28, dwell: 200, idle: 50 } export default { data () { return { power: Object.assign({}, defaultPower), } }, mounted() { handheld.setOnHandleListener(this.onHandler); }, methods: { onHandler: function(e) { if (e.code === 'up') { } else if (e.code === 'down') { } }, setPowerClick: function(e) { handheld.setPower(this.power, (res)=> { }) } } } </script>
- 使用 HBuilder
生成本地打包App資源
拷貝到 app 資源文件夾下。