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?