Untitled

 avatar
unknown
plain_text
5 months ago
2.6 kB
3
Indexable
   override fun detectCard(
        onContactCardDetected: () -> Unit,
        onContactlessEmvCardDetected: () -> Unit,
        onBoomContactlessDetected: () -> Unit,
        onTimeOut: () -> Unit,
        onError: (errorCode: Int) -> Unit,
        onCancelled: () -> Unit
    ) {
        Timber.e("detectCard => Called")
        if (this::scope.isInitialized && scope.isActive){
            scope.cancel()
        }
        isDetect = false
        job = SupervisorJob()
        scope = CoroutineScope(Dispatchers.IO + job)

        val timeoutJob = scope.launch {
            delay(30000)
            if (!isDetect) {
                onTimeOut.invoke()
                isDetect = true
                closeHostCardIfApplicable()
                scope.cancel()
            }
        }

        //for card detection we use a timeout of 50 milliseconds
        hostCard.activateNfc(onError, 50){
            when (hostCard.readAidData()) {
                ReadCardAidResult.IsBoom -> {
                    isDetect = true
                    scope.cancel()
                    onBoomContactlessDetected.invoke()
                }
                ReadCardAidResult.IsCard -> {
                    if (mShouldEnableEmvContactless) {
                        closeHostCardIfApplicable()
                        cardEntryModeUsed = CardEntryMode.NFC
                        isDetect = true
                        scope.cancel()
                        onContactlessEmvCardDetected.invoke()
                    }
                }
                ReadCardAidResult.OnError -> {
                    closeHostCardIfApplicable()
                    isDetect = true
                    scope.cancel()
                    onError.invoke(-11)
                }
            }
        }
        scope.launch {
            try {
                TelpoEmvManager.IccCloseReader()
                if (TelpoEmvManager.IccOpenReader() != TelpoEmvManager.EMV_DEVICE_TRUE) {
                    onError.invoke(4)
                }
                while (!isDetect) {
                    if (isCardInserted()) {
                        closeHostCardIfApplicable()
                        isDetect = true
                        cardEntryModeUsed = CardEntryMode.ICC
                        onContactCardDetected.invoke()
                        scope.cancel()
                    }
                }
            } catch (e: Exception) {
                Timber.e(e)
                onError.invoke(-1)
            } finally {
                isDetect = true
                timeoutJob.cancel()
                this.cancel()
            }
        }
    }
Editor is loading...
Leave a Comment