Some users of my app are complaining that NFC does not work all the time, on the same NFC tag. Sometimes resetting the NFC option from settings solve the issue.
We have HomeActivity
which receives the NFC intent and delivers it to the required fragment. Here is what I have tried.
Fragment side:
override fun onResume() {
super.onResume()
enableNFCForegroundDispatch()
....
}
override fun onPause() {
mNfcAdapter?.disableForegroundDispatch(requireActivity())
super.onPause()
...
}
private fun enableNFCForegroundDispatch() {
if (mNfcAdapter == null) {
mNfcAdapter =
(requireActivity().getSystemService(AppCompatActivity.NFC_SERVICE) as NfcManager).defaultAdapter
}
val intent = HomeActivity.newIntent(requireContext())
val flag = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
PendingIntent.FLAG_MUTABLE
} else 0
val pi = PendingIntent.getActivity(requireContext(), PENDING_INTENT_TECH_DISCOVERED, intent, flag)
if (pi != null) {
mNfcAdapter?.enableForegroundDispatch(
requireActivity(),
pi,
arrayOf(
IntentFilter(
NfcAdapter.ACTION_TECH_DISCOVERED
)
),
arrayOf(arrayOf("android.nfc.tech.NfcV"))
)
}
}
override fun onNewIntent(intent: Intent?) {
// just an implementation by all fragments to handle new intent from the parent
// we don't get this in the error cases
Timber.tag(SENSOR).d("NFC onNewIntent: intent received ${intent?.action} ${intent?.data}")
when (intent?.action) {
NfcAdapter.ACTION_TECH_DISCOVERED -> {
Timber.tag(SENSOR).d("NFC onNewIntent: nfc intent received")
resolveIntent(intent)
}
}
}
Activity side:
companion object {
fun newIntent(context: Context): Intent {
val intent = Intent(context, HomeActivity::class.java)
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_SINGLE_TOP or Intent.FLAG_ACTIVITY_NEW_TASK)
return intent
}
}
Manifest:
<activity
android:name=".home.HomeActivity"
android:exported="true"
android:label="@string/app_name"
android:screenOrientation="portrait"
android:theme="@style/UiSdkTheme.NoActionBar.NoNavigationBar">
<intent-filter>
<action android:name="android.nfc.action.TECH_DISCOVERED" />
</intent-filter>
<meta-data
android:name="android.nfc.action.TECH_DISCOVERED"
android:resource="@xml/nfc_tech_filter" />
</activity>
nfc_tech_filter:
<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
<tech-list>
<tech>android.nfc.tech.NfcV</tech>
</tech-list>
</resources>