1

Here code than work with NFC

  import android.app.Activity
import android.nfc.NfcAdapter
import android.nfc.Tag
import android.nfc.tech.IsoDep

  private val nfcAdapter: NfcAdapter? = NfcAdapter.getDefaultAdapter(activity)

  override fun start() {
        if (BuildConfig.DEBUG)
            Log.d(TAG, "start:" +
                    "\nactivity = $activity" +
                    "\ndefault_nfcAdapter = $nfcAdapter" +
                    "\nBuild.VERSION.SDK_INT = ${Build.VERSION.SDK_INT}")
        if (nfcAdapter == null
                || !nfcAdapter.isEnabled
                || Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
            return
        }
        if (BuildConfig.DEBUG)
            Log.d(TAG, "start: call_enableReaderMode(), nfcAdapter_isEnabled = ${nfcAdapter.isEnabled}")
        nfcAdapter.enableReaderMode(
                activity,
                this::onTagDiscovered,
                NfcAdapter.FLAG_READER_NFC_A or NfcAdapter.FLAG_READER_SKIP_NDEF_CHECK,
                null
        )


  private fun onTagDiscovered(tag: Tag) {
        if (BuildConfig.DEBUG)
            Log.d(TAG, "onTagDiscovered: findTag = $tag")
...
}

And here logcat:

start:
activity = org.tokend.contoredemptions.features.dashboard.view.DashboardActivity@a7be4f1
default_nfcAdapter = android.nfc.NfcAdapter@fbb6103
Build.VERSION.SDK_INT = 29
start: call_enableReaderMode(), nfcAdapter_isEnabled = true

On some device callback NfcAdapter.ReaderCallback, onTagDiscovered success call

onTagDiscovered: findTag = TAG: Tech [android.nfc.tech.IsoDep, android.nfc.tech.NfcA]

and as result NFC success work.

But on another devices callback NfcAdapter.ReaderCallback, onTagDiscovered not call and as result NFC not work.

Alexei
  • 14,350
  • 37
  • 121
  • 240
  • Why don't you use enableForegroundDispatch() ? – M D Jul 08 '20 at 13:02
  • @MD I call enableForegroundDispatch() but it not help – Alexei Jul 08 '20 at 13:38
  • Do you still get a sound from the platform? (has it detected it but not passed to your callback). There is also a known bug in some Broadcom Chipsets https://stackoverflow.com/a/20660634/2373819 but there is a work around by setting EXTRA_READER_PRESENCE_CHECK_DELAY . More info on the card model and device models you are using might be helpful – Andrew Jul 08 '20 at 13:47
  • @Andrew No sound no vibration when try send data by NFC – Alexei Jul 08 '20 at 13:49
  • @Andrew I try EXTRA_READER_PRESENCE_CHECK_DELAY, but it not help – Alexei Jul 08 '20 at 13:58
  • With those options if there was no sound on tag discover then it looks like the platform is not even seeing the card so using `enableForegroundDispatch` or the better API of `enableReaderMode` won't make a difference. The card does not happen to be a Mifare Classic Card? – Andrew Jul 08 '20 at 13:58
  • @Andrew If I send (with another app) any photo by NFC then it success send. But my application can't send data by NFC – Alexei Jul 08 '20 at 14:20
  • "I send (with another app) any photo by NFC" This sounds like you are trying to send data between 2 phone devices NOT read/write data to a NFC Card. If this is what you are trying to do then `enableForegroundDispatch` or `enableReaderMode` are not the API's to use. This is Peer to Peer NFC and has been deprecated in Android 10 – Andrew Jul 08 '20 at 14:43
  • @Andrew As I said before on some devices(Sony - Android 6, Goolge Pixel 3, Android 10) not problem to receive data by NFC with my app. But with another devices (Huawey, Android 10) I can't receive data by NFC in my app. – Alexei Jul 08 '20 at 16:04
  • 1
    It's still unclear what you are attempting to do, the code looks right for working with a device and a real card (or an emulated card). You need to update the question to detail exactly all the steps of what and how the app should behave. – Andrew Jul 08 '20 at 21:03
  • As a note on Android 10 beam deprecation from https://developer.android.com/about/versions/10/behavior-changes-all#beam-deprecation "In Android 10 we're officially deprecating Android Beam, an older feature for initiating data sharing across devices through Near Field Communication (NFC). We're also deprecating several of the related NFC APIs. Android Beam remains optionally available to device-maker partners who want to use it, but it's no longer in active development. " so Some device makers might still support it themselves in Android 10. – Andrew Jul 08 '20 at 21:05

0 Answers0