插件类是
com.fchtech.btblock.plugin.BlePlugin
在Eclipse bin下看到class生成
<feature
name="ble"
value="com.fchtech.btblock.plugin.BlePlugin" />
<service
name="ble"
value="com.fchtech.btlock.plugin.BlePlugin" />
启动的时候BlePlugin类中的onStart方法能够被调用到,会打印 "onStart was called",表示类肯定存在
用js调用里面的方法就提示找不到类,莫名其妙啊
09-07 14:23:51.088: W/System.err(2556): java.lang.ClassNotFoundException: com.fchtech.btblock.plugin.BlePlugin
09-07 14:23:51.097: W/System.err(2556): at java.lang.Class.classForName(Native Method)
09-07 14:23:51.097: W/System.err(2556): at java.lang.Class.forName(Class.java:309)
09-07 14:23:51.097: W/System.err(2556): at java.lang.Class.forName(Class.java:273)
09-07 14:23:51.097: W/System.err(2556): at io.dcloud.feature.b.a(FeatureMgr.java:356)
09-07 14:23:51.097: W/System.err(2556): at io.dcloud.feature.b.a(FeatureMgr.java:297)
09-07 14:23:51.097: W/System.err(2556): at io.dcloud.feature.b.processEvent(FeatureMgr.java:132)
09-07 14:23:51.097: W/System.err(2556): at io.dcloud.common.b.a.dispatchEvent(Core.java:392)
09-07 14:23:51.097: W/System.err(2556): at io.dcloud.common.b.b.j.processEvent(WindowMgr.java:117)
09-07 14:23:51.097: W/System.err(2556): at io.dcloud.common.b.b.h.exec(DHWebview.java:166)
09-07 14:23:51.097: W/System.err(2556): at io.dcloud.common.util.Birdge.exec(Birdge.java:25)
09-07 14:23:51.097: W/System.err(2556): at io.dcloud.common.adapter.ui.AdaWebview$3.execute(AdaWebview.java:586)
09-07 14:23:51.097: W/System.err(2556): at io.dcloud.common.adapter.util.MessageHandler$1.handleMessage(MessageHandler.java:23)
09-07 14:23:51.097: W/System.err(2556): at android.os.Handler.dispatchMessage(Handler.java:102)
09-07 14:23:51.097: W/System.err(2556): at android.os.Looper.loop(Looper.java:135)
09-07 14:23:51.098: W/System.err(2556): at android.app.ActivityThread.main(ActivityThread.java:5343)
09-07 14:23:51.098: W/System.err(2556): at java.lang.reflect.Method.invoke(Native Method)
09-07 14:23:51.098: W/System.err(2556): at java.lang.reflect.Method.invoke(Method.java:372)
09-07 14:23:51.098: W/System.err(2556): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:905)
09-07 14:23:51.098: W/System.err(2556): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700)
09-07 14:23:51.098: W/System.err(2556): Caused by: java.lang.ClassNotFoundException: Didn't find class "com.fchtech.btblock.plugin.BlePlugin" on path: DexPathList[[zip file "/data/app/com.fchtech.btlock-1/base.apk"],nativeLibraryDirectories=[/vendor/lib, /system/lib]]
09-07 14:23:51.098: W/System.err(2556): at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
09-07 14:23:51.098: W/System.err(2556): at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
09-07 14:23:51.098: W/System.err(2556): at java.lang.ClassLoader.loadClass(ClassLoader.java:469)
09-07 14:23:51.098: W/System.err(2556): ... 19 more
09-07 14:23:51.098: W/System.err(2556): Suppressed: java.lang.ClassNotFoundException: com.fchtech.btblock.plugin.BlePlugin
09-07 14:23:51.098: W/System.err(2556): at java.lang.Class.classForName(Native Method)
09-07 14:23:51.098: W/System.err(2556): at java.lang.BootClassLoader.findClass(ClassLoader.java:781)
09-07 14:23:51.098: W/System.err(2556): at java.lang.BootClassLoader.loadClass(ClassLoader.java:841)
09-07 14:23:51.098: W/System.err(2556): at java.lang.ClassLoader.loadClass(ClassLoader.java:504)
09-07 14:23:51.098: W/System.err(2556): ... 20 more
09-07 14:23:51.098: W/System.err(2556): Caused by: java.lang.NoClassDefFoundError: Class not found using the boot class loader; no stack available
插件类代码:
package com.fchtech.btlock.plugin;
import java.io.UnsupportedEncodingException;
import org.json.JSONArray;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothManager;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import io.dcloud.common.DHInterface.IWebview;
import io.dcloud.common.DHInterface.StandardFeature;
import io.dcloud.common.util.JSUtil;
public class BlePlugin extends StandardFeature {
private BluetoothAdapter mBluetoothAdapter;
private String CallBackID;
private IWebview webview;
private BluetoothAdapter.LeScanCallback mLEScanCallback;
private String tag = "BlePlugin";
public void onStart(Context pContext, Bundle pSavedInstanceState,
String[] pRuntimeArgs) {
Log.d(tag, "onStart was called");
/**
* 如果需要在应用启动时进行初始化,可以继承这个方法,并在properties.
* xml文件的service节点添加扩展插件的注册即可触发onStart方法
* */
BluetoothManager manager = (BluetoothManager) pContext
.getSystemService(Context.BLUETOOTH_SERVICE);
mBluetoothAdapter = manager.getAdapter();
if (!mBluetoothAdapter.isEnabled())
mBluetoothAdapter.enable();
mLEScanCallback = new BluetoothAdapter.LeScanCallback() {
@Override
public void onLeScan(BluetoothDevice device, int rssi,
byte[] scanRecord) {
Log.d(tag, "onLeScan was called");
String device_name = parseLockName(scanRecord);
int manu_length = manuDataLength(scanRecord);
int offset_status = device_name.length()
+ scanRecord[device_name.length() + 1] + 1 + 4 + 1;
int num_btl = -1;
if (manu_length == 0x0B)
num_btl = scanRecord[offset_status + 6 + 1];
JSONArray newArray = new JSONArray();
newArray.put(device_name);
newArray.put(device.getAddress());
newArray.put(rssi);
newArray.put(num_btl);
JSUtil.execCallback(webview, CallBackID, newArray, JSUtil.OK,
true);
}
};
}
private String parseLockName(byte[] scanRecord) {
int name_length = scanRecord[0];
byte[] name = new byte[name_length];
System.arraycopy(scanRecord, 1, name, 0, name_length);
try {
String res = new String(name, "UTF-8");
return res;
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
return null;
}
}
private int manuDataLength(byte[] scanRecord) {
// device name length
int name_length = scanRecord[0];
// 020106 length
int second_length = scanRecord[name_length + 1];
int manu_length = scanRecord[name_length + second_length + 2];
return manu_length;
};
@SuppressWarnings("deprecation")
public void StartScan(IWebview pWebview, JSONArray array) {
Log.d(tag, "StartScan was called");
CallBackID = array.optString(0);
webview = pWebview;
mBluetoothAdapter.startLeScan(mLEScanCallback);
}
@SuppressWarnings("deprecation")
public String StopScan(IWebview pWebview, JSONArray array) {
Log.d(tag, "StopScan was called");
mBluetoothAdapter.stopLeScan(mLEScanCallback);
return JSUtil.wrapJsVar("ok", true);
}
}
调用JS API:
document.addEventListener("plusready", function() {
var _BARCODE = 'ble', B = window.plus.bridge;
var ble = {
StartScan : function(successCallback, errorCallback) {
mui.toast("startScan enter");
var success = typeof successCallback !== 'function'
? null
: function(args) {
successCallback(args);
}, fail = typeof errorCallback !== 'function'
? null
: function(code) {
errorCallback(code);
};
callbackID = B.callbackId(success, fail);
return B.exec(_BARCODE, "StartScan", [callbackID]);
},
StopScan : function() {
return B.execSync(_BARCODE, "StopScan", null);
}
};
window.plus.ble = ble;
}, true);
js调用方法
plus.ble.StartScan(function(result) {
var name = result[0];
var address = result[1];
var rssi = result[2];
var numbl = result[3];
var li1 = document.createElement('li');
li1.setAttribute('class', "mui-table-view-cell");
li1.setAttribute('id', address); // mac地址
on = name + " " + address;
var a1 = document.createElement('a');
a1.setAttribute('class', "mui-navigate-right");
a1.addEventListener("tap", function() {
});
a1.innerText = on;
li1.appendChild(a1);
vlist1.appendChild(li1);
}, function(result) {
alert(result);
});
1 个回复
j***@fchtech.com (作者)
解决了,包名问题
<feature
name="ble"
value="com.fchtech.btlock.plugin.BlePlugin">
</feature>
之前是手工输入,现在换成复制黏贴就可以了