entur / android-nfc-lib

Library for working with internal + external NFC readers on Android
European Union Public License 1.2
23 stars 2 forks source link

Return tag lost via better exception handling for transceive #36

Closed skjolber closed 8 months ago

skjolber commented 8 months ago

Add an exception mapper which maps ACS driver errors like UnresponsiveCardException to tag lost.

Tested using logging and

        IsoDep isoDep = IsoDep.get(tag);
        if(isoDep != null) {

            try {
                isoDep.connect();
                while(true) {
                    LOGGER.info("Sending select application");

                    isoDep.transceive(wrapMessage(SELECT_APPLICATION, new byte[]{(byte) 0x00, (byte) 0x80, (byte) 0x57}));
                    Thread.sleep(10);
                }
            } catch (Exception e) {
                LOGGER.info("Problem sending select application", e);
            } finally {
                try {
                    isoDep.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }

            }
        } else {
            LOGGER.info("Sending select application");
        }
2024-03-18 12:13:27.886 23292-23370 no.entur.a...UsbService no.entur.abt.nfc.example             I  [Thread-4] onTagAbsent
2024-03-18 12:13:27.894 23292-23365 no.entur.a...inActivity no.entur.abt.nfc.example             I  [pool-2-thread-1] Sending select application
2024-03-18 12:13:27.910 23292-23365 no.entur.a...inActivity no.entur.abt.nfc.example             I  [pool-2-thread-1] Problem sending select application
                                                                                                    android.nfc.TagLostException: Tag was lost.
                                                                                                        at no.entur.android.nfc.wrapper.TransceiveResult.getResponseOrThrow(TransceiveResult.java:49)
                                                                                                        at no.entur.android.nfc.wrapper.tech.BasicTagTechnologyImpl.transceive(BasicTagTechnologyImpl.java:149)
                                                                                                        at no.entur.android.nfc.wrapper.tech.IsoDepImpl.transceive(IsoDepImpl.java:166)
                                                                                                        at no.entur.abt.nfc.example.MainActivity.onExternalTagDiscovered(MainActivity.java:182)
                                                                                                        at no.entur.android.nfc.external.ExternalNfcTagCallbackSupport$1.lambda$onReceive$0$no-entur-android-nfc-external-ExternalNfcTagCallbackSupport$1(ExternalNfcTagCallbackSupport.java:38)
                                                                                                        at no.entur.android.nfc.external.ExternalNfcTagCallbackSupport$1$$ExternalSyntheticLambda0.run(Unknown Source:6)
                                                                                                        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
                                                                                                        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:644)
                                                                                                        at java.lang.Thread.run(Thread.java:1012)