5

I am new in creating the KIOSK app with NFC and I faced a problem. The app works in KIOSK mode and scans NFC cards. But sometimes NFC stops working, and I reboot the device to keep NFC working, but sometimes even after a reboot NFC doesn't work. The device OS is Android 7. This is the Manifest file:

<activity
        android:screenOrientation="portrait"
        android:name=".MainActivity"
        android:launchMode="singleTask"
        android:finishOnTaskLaunch="true"
        android:clearTaskOnLaunch="true"
        android:stateNotNeeded="true"
        android:excludeFromRecents="true"
        android:autoRemoveFromRecents="true">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
            <category android:name="android.intent.category.HOME" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>

        <intent-filter>
            <action android:name="android.nfc.action.NDEF_DISCOVERED"/>
            <category android:name="android.intent.category.DEFAULT"/>
        </intent-filter>
    </activity>

And this is Code from activity class:

class MainActivity : BaseActivity() {

 private val kioskManager by lazy {
    KioskManager(
        activity = this,
        activityName = MainActivity::class.java.name,
        componentName = AppDeviceAdminReceiver.getComponentName(this)
    )
 }

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    isAdmin = devicePolicyManager.isDeviceOwnerApp(packageName)
    if (intent.getStringExtra(KioskManager.LOCK_ACTIVITY_KEY) == KioskManager.UNLOCK) {
        kioskManager.stopLock()
    } else {
        kioskManager.startLock()
    }
    setContentView(R.layout.activity_main)
}

override fun onNewIntent(intent: Intent?) {
    super.onNewIntent(intent)
    val tagFromIntent: Tag? = intent?.getParcelableExtra(NfcAdapter.EXTRA_TAG)
    // handle NFC data     
}

override fun onResume() {
    super.onResume()
    enableNfcForegroundDispatch()
}

override fun onPause() {
    disableNfcForegroundDispatch()
    super.onPause()
}


private fun enableNfcForegroundDispatch() {
    try {
        val intent = Intent(this, javaClass).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP)
        val nfcPendingIntent = PendingIntent.getActivity(this, 0, intent, 0)
        nfcAdapter.enableForegroundDispatch(this, nfcPendingIntent, null, null)
    } catch (ex: IllegalStateException) {
        ex.printStackTraceDebug()
    }
}

private fun disableNfcForegroundDispatch() {
    try {
        nfcAdapter.disableForegroundDispatch(this)
    } catch (ex: IllegalStateException) {
        ex.printStackTraceDebug()
    }
}
}

I spend a month to fix this bug, but can't find an answer? How to fix this issue? Thanks!

MDev25
  • 216
  • 2
  • 9
  • 1
    There seems to be some peculiarities with Kiosk Mode, especially around what is the foreground App and NFC detection only works when your App is in the foreground, this question and answers might help https://stackoverflow.com/q/61142449/2373819 – Andrew Sep 30 '20 at 15:07

0 Answers0