Read NFC tag UID with WebUSB API and acr122u external NFC reader
Asked Answered
E

0

6

I'm trying to read NFC Tag UID by utilizing external USB NFC Reader acr122u and WebUSB API

I'm able to authorize USB device, turn off/on buzzer, change led color etc., but unable to read 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();
    });

})();

Another question is how to detect a tag when it's in range of NFC reader, i.e. not to detect it only once, but detect every tag placed/removed on the reader.

Thank You!

Expiable answered 25/11, 2017 at 12:28 Comment(4)
It looks like you've successfully managed to send some commands using WebUSB so this looks like a question about what protocol is spoken by this RFID reader. Is there documentation provided by the manufacturer or is there an open source driver you can use as a reference for what commands to send?Rodge
@ReillyGrant, yes, I do have API docs But I'm stuck at this point.Expiable
@ReillyGrant, right now I'm utilizing libusb-win32 driver and also trying libusb Have no clue where to look further.Expiable
Did you find a solution? I ran your code with the same ACR122U reader and give me an error: "An attempt to claim a USB device interface has been blocked because it implements a protected interface class" a new Chrome security police that block access to these devices.... OMG!!!!Surplusage

© 2022 - 2024 — McMap. All rights reserved.