Emulate Mifare/NFC tag using Ionic Angular

546 Views Asked by At

I have a Mifrare Ultralight card that I use for one reader. It can also read NFC smart cards. I need an Ionic - Angular app that behaves like the card / tag I have. It needs to send only specified ID - that's it - no data with it.

I went throgh lots of different docs and libs starting from phonegap-nfc ending up at cordova-plugin-hce. I was sure it's enaugh that I only send Ndef messages using phonegap-nfc but it seems that

Android Beam (The functionality used in peer to peer NFC sharing) was deprecated in Android 10

See https://source.android.com/setup/start/android-10-release#nfc

See Phonegap NFC Send and Read Data Between Two Devices

It seems that HCE is my only choice now. But I cannot make it working. I've started to use @ionic-native/hce that is a wrapper for cordova-plugin-hce. My code (through a lot of testing) looks like this:

        hce.registerCommandCallback((command) => {
            console.log('COMMAND RECEIVED', command);

            const SAMPLE_LOYALTY_CARD_AID = 'F222222222';
            const SELECT_APDU_HEADER = '00A40400';


            // Format: [CLASS | INSTRUCTION | PARAMETER 1 | PARAMETER 2 | LENGTH | DATA]
            const aidByteLength = ('00' + (SAMPLE_LOYALTY_CARD_AID.length / 2).toString(16)).substr(-2);

            const SELECT_APDU = (SELECT_APDU_HEADER + aidByteLength + SAMPLE_LOYALTY_CARD_AID).toLowerCase();
            const SELECT_OK_SW = '9000';
            const UNKNOWN_CMD_SW = '0000';

            console.log(command);
            const commandAsBytes = new Uint8Array(command);
            const commandAsString = hce.util.byteArrayToHexString(commandAsBytes);

            console.log('received command ' + commandAsString);
            console.log('expecting        ' + SELECT_APDU);

            if (SELECT_APDU === commandAsString) {
                const accountNumberAsBytes = hce.util.stringToBytes('1234567890');
                const data = hce.util.concatenateBuffers(accountNumberAsBytes, hce.util.hexStringToByteArray(SELECT_OK_SW));

                console.log('Sending ' + hce.util.byteArrayToHexString(data));
                hce.sendResponse(data);
            } else {
                console.log('UNKNOWN CMD SW');
                hce.sendResponse(hce.util.hexStringToByteArray(UNKNOWN_CMD_SW));
            }
        }, (e) => {
            console.log('error mtf ', e);
        });

        hce.registerDeactivatedCallback(reason => {
            console.log('Deactivated ' + reason);
        });

The problem is that registerCommandCallback is never triggered when I move the phone close to the 13.56 Mhz reader. Any clue?

0

There are 0 best solutions below