4

我正在尝试NFC Tag UID通过使用外部USB NFC 阅读器 acr122uWebUSB API来阅读

我可以授权USB device、关闭/打开蜂鸣器、更改 LED 颜色等,但无法读取Tag UID

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 阅读器的范围内检测标签,即不是只检测一次,而是检测放置/移除阅读器上的每个标签。

谢谢你!

4

0 回答 0