1
votes

I am working on android project, where NFC is used as a communication. I am facing a weird problem, when mobile device has a NFC, it is enabled, but it is not working on some devices (adapter is not enabled when debugging). I am writing logs and it prints, NFC on, adapter disabled. For example: HTC One m9(os 7.0). Also happens with OnePlus One(os 9)! But again, it works on other devices. Did you experience the same issue?

Here is some code:

object NfcUtil {

    fun getNfcAdapter(c: Context): NfcAdapter? {
        val manager = c.getSystemService(Context.NFC_SERVICE) as NfcManager
        return manager.defaultAdapter
    }

    fun doesSupportHce(c: Context): Boolean {
        return c.packageManager.hasSystemFeature(PackageManager.FEATURE_NFC_HOST_CARD_EMULATION)
    }
}



val adapter = NfcUtil.getNfcAdapter(this)
if (adapter != null && NfcUtil.doesSupportHce(this)) {
     if (adapter.isEnabled) {
             tvNfcOff.extHide()
              } else {
                 tvNfcOff.extShow()
              }
     }
2
Have you tried in case of adapter.isEnabled == false to do a startActivity(new Intent(Settings.ACTION_NFC_SETTINGS)) just to check if the user have any option in settings to enable any feature of the NFC Adapter?denis_lor
Hi denis_lor. Thanks for idea. How would I see if user has an option in settings? Because I can go to settings and enable NFC. Is there a different way to see features of NFC adapter? In my if statement, doesSupportHce is true, then it checks isEnabled, and it is false... weird. I will give a try what you suggested.DM developing
I think the issue is not in the way you wrote any code. I think there is an issue with the ROM and indeed the Firmware that is doing the work between Hardware and Software. There are some articles around XDA forums and Reddit stating some unstability of NFC to work on the devices you mentioned in your question. Maybe you can find something in those forums. Here is something I found on reddit: reddit.com/r/LineageOS/comments/5u7fvg/… and here something on XDA: forum.xda-developers.com/oneplus-one/help/…denis_lor
And later on I will just try to debug it with that Device and that specific Operating System that is having this kind of issue. I'll try to see if other apps using NFC has same issues or they work fine, and by work fine I mean that the communication happens not that other apps dont show any warning/error popup message.denis_lor
Thanks. I just found out that my HTC device camera is also working weird, like it is missing some drivers. And OnePlusOne has custom ROM (my bad)!!! But yeah, I will let user know if somethings goes wrong. Thanks a lot!DM developing

2 Answers

2
votes

I think that if NFC is supported and enabled but the adapter is disabled (https://developer.android.com/reference/android/nfc/NfcAdapter#isEnabled()) I'll follow the guidelines and redirects the user to the settings screen with the intent mentioned in the documentation.

If the user come back few times you could monitor it and show a different message instead of redirecting to settings, something like: NFC is not working properly on your device. I'd check if you have lots of users using those devices, if yes, I will try to research more on the Operating System and Device having this issue.

And later on I will just try to debug it with that Device and that specific Operating System that is having this kind of issue. I'll try to see if other apps using NFC has same issues or they work fine, and by work fine I mean that the communication happens not that other apps dont show any warning/error popup message.

And if I found out its an issue in a specific OS Version, also with other apps, I'll just try to inform the users and get an update on which version the issue have been fixed. Otherwise if other apps can make a successful NFC communication in that device/OS that is not working for me, I'll just dig deeper.

For now I can say there is nothing wrong in your implementation and looks good.

It might be an issue with the current OS or if you have any Custom ROM that might not fully support or have a functional NFC driver.

1
votes

Two additional bits of info that might be useful

1) Use a Broadcaster receiver to get notified when the NFC state changes, because using the quick settings pull down does not pause your app, therefore retesting nfc status in onResume does not work (a user changing via the full settings app will pause you App, though)

Example of how to do it in Java


@Override
    protected void onCreate(Bundle savedInstanceState) {
    // All normal onCreate Stuff

    // Listen to NFC setting changes
    this.registerReceiver(mReceiver, filter);
    }

// Listen for NFC being turned on while in the App
    private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            final String action = intent.getAction();

            if (action.equals(NfcAdapter.ACTION_ADAPTER_STATE_CHANGED)) {
                final int state = intent.getIntExtra(NfcAdapter.EXTRA_ADAPTER_STATE,
                        NfcAdapter.STATE_OFF);
                switch (state) {
                    case NfcAdapter.STATE_OFF:
                    // Tell the user to turn NFC on if App requires it
                        break;
                    case NfcAdapter.STATE_TURNING_OFF:
                        break;
                    case NfcAdapter.STATE_ON:
                        // Do something with this to enable NFC listening
                        break;
                    case NfcAdapter.STATE_TURNING_ON:
                        break;
                }
            }
        }
    };

2) Don't assume that the device has a NFC settings page, if your app works with and without NFC, if the adapter is null don't assume you can start an Intent to the NFC settings page as suggested by @denis_lor as this will cause a crash if the OS does not have a NFC adapter to turn on.