0

I am trying to use NFC to send a URL from an Android app to other phone, both have enabled NFC and working fine.

when i tried to test the app nothing happen,

i have tried the following app from store and it work perfectly: https://play.google.com/store/apps/details?id=com.maxsoft.ndeftagemulator

this is my code:

 NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(this);
        if (nfcAdapter == null) {
            Toast.makeText(this, "NFC not supported on this device.", Toast.LENGTH_LONG).show();
            finish();
            return;
        }

        nfcAdapter.setNdefPushMessageCallback(event -> {
            String url = "https://www.example.com";
            NdefRecord ndefRecord = NdefRecord.createUri(url);
            NdefMessage ndefMessage = new NdefMessage(ndefRecord);
            return ndefMessage;
        }, this);

        nfcAdapter.setOnNdefPushCompleteCallback(event -> Toast.makeText(MainActivity.this, "URL sent via NFC.", Toast.LENGTH_LONG).show(), this);

i also added permission in Manifest

<uses-permission android:name="android.permission.NFC" />

i searched for answers here and checked the following with no success ( android API +29 )

Ho do I send a URL via NFC?

Sending URL from Android to Windows Phone via NFC gives Play Store link

Sending URL via NFC to be opened by browser

MustafaTaj
  • 11
  • 5
  • Does this answer your question? [How to substitute NfcAdapter.CreateNdefMessageCallback since android API level 29?](https://stackoverflow.com/questions/74351717/how-to-substitute-nfcadapter-createndefmessagecallback-since-android-api-level-2) – Andrew Jan 28 '23 at 11:06
  • unfortunately, this didn't work, I want to emulate ntag213 URL once any other enabled device touch it to open in web browser. – MustafaTaj Jan 29 '23 at 07:50
  • the code perfectly worked after changing NdefMessage(createTextRecord("en", intent.getStringExtra("ndefMessage"), NDEF_ID)) to NdefMessage(NdefRecord.createUri(intent.getStringExtra("ndefMessage"))) in line 119 file KHostApduService.kt – MustafaTaj Jan 29 '23 at 11:23
  • sometimes it work sometimes it doesn't, the services exit – MustafaTaj Jan 29 '23 at 14:33
  • I've not tried that code myself for HCE, but HCE is the only way now to send data between devices as all the P2P NFC stuff e.g. `setNdefPushMessageCallback` you were trying to use was removed in Android 10. As well as that example there is a section in the Android Docs on HCE at https://developer.android.com/guide/topics/connectivity/nfc/hce – Andrew Jan 29 '23 at 17:24

0 Answers0