无法在“USBDevice”上执行“claimInterface”

问题描述 投票:0回答:1

我的电脑上实际上有一个 USB 设备插头。 该设备允许我读取 NFC,但是当我将 NFC 设置为红色时,导航器(Chrome)向我返回了此错误消息:

未捕获(承诺中)DOMException:无法在“USBDevice”上执行“claimInterface”:请求的接口实现受保护的类。

我的代码是下一个:

var usbd = {};
let device;
let deviceEndpoint = 0x02;

let powerUpDevice = new Uint8Array([0x62,0x00, 0x00, 0x00, 0x00,0x00,0x00,0x01,0x00, 0x00]).buffer;
let getCardUID = new Uint8Array([0xff,0xca,0x00,0x00,0x04]).buffer;

(function() {
    'use strict';

    usbd.authorize = function(){
        navigator.usb.requestDevice({ filters: [{ vendorId: 0x072f }] })
            .then(selectedDevice => {
                device = selectedDevice;
                console.log(device.configuration.interfaces[0].interfaceNumber);
                console.log(device.manufacturerName);
                console.log(device.productName);
                console.log(device);
                return device.open()
                    .then(() => {
                        if (device.configuration === null) {
                            return device.selectConfiguration(1);
                        }
                    });
            })
            .then(() => device.claimInterface(0))
            .then(() => device.transferOut(deviceEndpoint, powerUpDevice)
                .then(transferResult => {
                    console.log(transferResult);
                }, error => {
                    console.log(error);
                    device.close();
                })
                .catch(error => {
                    console.log(error);
                })
            );

    };

    usbd.getDevice = function(){
        return navigator.usb.getDevices().then(devices => {
            return devices.map(device => new usbd.device(device));
        });
    };

    usbd.device = function(device) {
        this.device_ = device;
    };

    usbd.getTagUID = function(){
        device = this.getDevice();

        device
            .then(device => {
                device[0].device_.open()
                    .then(() => device[0].device_.claimInterface(0))
                    .then(() => device[0].device_.transferOut(deviceEndpoint, getCardUID)
                        .then(transferResult => {
                            console.log(transferResult);
                        }, error => {
                            console.log(error);
                            device[0].device_.close();
                        })
                        .catch(error => {
                            console.log(error);
                        })
                    )
                    .then(() => device[0].device_.claimInterface(0))
                    .then(() => device[0].device_.transferIn(deviceEndpoint, 16)
                        .then(USBInTransferResult => {
                            if (USBInTransferResult.data) {
                                console.log(USBInTransferResult.data.getUint32(0));
                            }
                        }, error => {
                            console.log(error);
                            device[0].device_.close();
                        })
                        .catch(error => {
                            console.log(error);
                        })
                    );
            });
    };

    $('#connect').click(function(){
        usbd.authorize();
    });

    $('#readCard').click(function(){
        usbd.getTagUID();
    });

})();

如何在我的网站上读取我的 NFC?

nfc
1个回答
0
投票

我也遇到了和你一样的问题。你解决了吗

© www.soinside.com 2019 - 2024. All rights reserved.