1

I know that at the moment there is no API in IOS 11 to write data to NFC tags, but it's possible to read data from NFC tags and want to pass text from Android device to iPhone. I assumed that it's possible to write NdefMessage and it will be received on IOS, but it just doesnt work for me. There is no Intent received when I start NFC scanning on IOS (using NfcActions application).

Source code of my main activity:

class MainActivity : AppCompatActivity() {

var nfc: NfcAdapter? = null

@SuppressLint("SetTextI18n")
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)
    setSupportActionBar(toolbar)


    hintLabel.text = "Initializing..."
    nfc = NfcAdapter.getDefaultAdapter(this)

    if (nfc == null) hintLabel.text = "NFC is not available on this device"
    else hintLabel.text = "NFC initialized"
}

override fun onResume() {
    super.onResume()

    nfc?.enableNFCInForeground(this, javaClass)
}

override fun onPause() {
    super.onPause()
    nfc?.disableForegroundDispatch(this)
}

override fun onNewIntent(intent: Intent?) {
    super.onNewIntent(intent)
    val pathPrefix = "/testuser:nfctest"
    val isSuccess = createNFCMessage(pathPrefix, "Hello World", intent)
    if(isSuccess)
        hintLabel.text = "Successfully wrote data"
    else
        hintLabel.text = "Couldnt write any data"

}

fun createNFCMessage(pathPrefix: String, payload: String, intent: Intent?) : Boolean {

    val nfcRecord = NdefRecord(NdefRecord.TNF_EXTERNAL_TYPE, pathPrefix.toByteArray(), ByteArray(0), payload.toByteArray())
    val nfcMessage = NdefMessage(arrayOf(nfcRecord))
    intent?.let {
        val tag = it.getParcelableExtra<Tag>(NfcAdapter.EXTRA_TAG)
        return writeMessageToTag(nfcMessage, tag)
    }
    return false
}


fun <T>NfcAdapter.enableNFCInForeground(activity: Activity, classType: Class<T>) {

    val pendingIntent = PendingIntent.getActivity(activity, 0,
            Intent(activity,classType).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0)

    val filters = arrayOf(IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED))
    val techList = arrayOf(arrayOf(Ndef::class.java.name), arrayOf(NdefFormatable::class.java.name))

    this.enableForegroundDispatch(activity, pendingIntent, filters, techList)
}

private fun writeMessageToTag(nfcMessage: NdefMessage, tag: Tag?): Boolean {

    // This functions writes given NdefMessage to the tag, if it's possible
    // and returns whether it was successful or not
}

And I also added following permissions and intent filter to main manifest

<uses-permission android:name="android.permission.NFC" />
<uses-feature
    android:name="android.hardware.nfc"
    android:required="false" />
<application ...>
    <activity ...>     
        <intent-filter>
            <action android:name="android.nfc.action.NDEF_DISCOVERED"/>
            <category android:name="android.intent.category.DEFAULT"/>
            <data android:host="ext"
                android:pathPrefix="/testuser:nfctest"
                android:scheme="vnd.android.nfc"
                android:mimeType="text/plain"/>
        </intent-filter>
    </activity>
</application>

What am I doing wrong and how to properly pass data from Android device to iPhone using NFC?

Michael Roland
  • 39,663
  • 10
  • 99
  • 206
Daniil Dubrovsky
  • 459
  • 7
  • 17

2 Answers2

3

Apparently the usual peer-2-peer way to send NDEF messages from one phone to another only works between 2 android devices. To send a message from Android to iOS you need to use host card emulation. Basically the Android phone needs to emulate a Tag 4 Type (based on NDEF Forum specification), for the iPhone to read the content. Hope that gets you on the right track ...

ErikM
  • 591
  • 1
  • 4
  • 13
  • I tried emulating a Type 4 tag on Android, but iOS was not capable of reading it. It does read standard Type 2 tags though, but I don't know how to emulate it on Android or even if it is possible. – erickva Aug 31 '18 at 06:54
  • If you're using react native, there's a nice library implementation you can use to emulate NFC tag in android using >> [react-native-hce](https://www.npmjs.com/package/react-native-hce) There is no support for iOS though – sandulasanth-7 Jan 02 '23 at 05:32
1

For anyone who stuck on this issue, I have read NFCForum-TS-Type-4-Tag which proposed by @Michael Roland. The whole idea is correct. All you need is to simulate the process SEND and RECEIVED commands to convert the byte array to a NDEF message. I created two repositories, one conclude the whole package about converting string to a NDEF message and the other one is a iOS Reader NDEF TAG to verify whether Android HCE is correct or not.

So Good luck!

Also see the response by Michael Roland to NDEF Message with HCE Android.

Michael Roland
  • 39,663
  • 10
  • 99
  • 206
underwood
  • 199
  • 1
  • 4