chrome.usb.openDevice返回null句柄

时间:2015-09-09 12:26:48

标签: android cordova google-chrome

我使用cordova-plugin-chrome-apps-usb来访问USB设备(指纹打印机阅读器)。

chrome.usb.getDevices() 正确返回连接到Andriod主机的USB设备(我得到了正确的VendorId和ProductId)。

chrome.usb.openDevice() 会返回USB设备的空设备句柄。

我追溯到openDevice()代码,发现此步骤返回msgs =" 62 F09 ChromeUsb595160930 sPermission request尚未实现 " 。这向我表明了许可问题吗?

function androidExec(success, fail, service, action, args) {
if (bridgeSecret < 0) {
    // If we ever catch this firing, we'll need to queue up exec()s
    // and fire them once we get a secret. For now, I don't think
    // it's possible for exec() to be called since plugins are parsed but
    // not run until until after onNativeReady.
    throw new Error('exec() called without bridgeSecret');
}
// Set default bridge modes if they have not already been set.
// By default, we use the failsafe, since addJavascriptInterface breaks too often
if (jsToNativeBridgeMode === undefined) {
    androidExec.setJsToNativeBridgeMode(jsToNativeModes.JS_OBJECT);
}

// Process any ArrayBuffers in the args into a string.
for (var i = 0; i < args.length; i++) {
    if (utils.typeName(args[i]) == 'ArrayBuffer') {
        args[i] = base64.fromArrayBuffer(args[i]);
    }
}

var callbackId = service + cordova.callbackId++,
    argsJson = JSON.stringify(args);

if (success || fail) {
    cordova.callbacks[callbackId] = {success:success, fail:fail};
}

var msgs = nativeApiProvider.get().exec(bridgeSecret, service, action, callbackId, argsJson);
// If argsJson was received by Java as null, try again with the PROMPT bridge mode.
// This happens in rare circumstances, such as when certain Unicode characters are passed over the bridge on a Galaxy S2.  See CB-2666.
if (jsToNativeBridgeMode == jsToNativeModes.JS_OBJECT && msgs === "@Null arguments.") {
    androidExec.setJsToNativeBridgeMode(jsToNativeModes.PROMPT);
    androidExec(success, fail, service, action, args);
    androidExec.setJsToNativeBridgeMode(jsToNativeModes.JS_OBJECT);
} else if (msgs) {
    messagesFromNative.push(msgs);
    // Always process async to avoid exceptions messing up stack.
    nextTick(processMessages);
}

AndroidManifest.xml 包含以下权限: -

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-feature android:name="android.hardware.usb.host"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.USE_FINGERPRINT"/>
<uses-permission android:name="android.permission.MANAGE_USB"/>

我错过了相关的吗?

2 个答案:

答案 0 :(得分:0)

您使用的是哪个版本的Android? Cordova尚不支持Android M的新权限模型,因此这可能是一个问题。

答案 1 :(得分:0)

你正在打这个TODO:

if (!mUsbManager.hasPermission(usbDev)) {
  // TODO: Implement dynamic permission request.
  throw new UsbError("Permission request not yet implemented");
}

在实施之前,您需要根据Android SDK在APK的清单中为您要打开的设备的特定VID / PID静态分配权限。

即。把这样的东西放在AndroidManifest.xml

<intent-filter>
    <action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" />
</intent-filter>
<meta-data android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" android:resource="@xml/usb_device_filter" />

并添加适当的res / xml / usb_device_filter.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
  <usb-device vendor-id="6353" product-id="1234" />
</resources>

显然用VID&amp;的十进制编码替换6353和1234; PID,与普通chrome应用程序中manifest.json中“usbDevices”字段中使用的相同。 (理想情况下,清单xml行将由CCA工具链根据提供的manifest.json自动生成。)