I need to read NFC tag when only user push a button.
The following is code that I wrote but it does not work.
I've tried that display NFC tag information to textview.
Nothing to display anything despite I hold up a NFC card.
It looks like onNewIntent Event does not fire when NFC is present.
Why following code is not work?
Update:
Now I tried to pass techlist argument to enableForegroundDispatch. It does not work yet.
techlist = arrayOf(
arrayOf(android.nfc.tech.NfcA::class.java.name),
arrayOf(android.nfc.tech.NfcB::class.java.name),
arrayOf(android.nfc.tech.IsoDep::class.java.name),
arrayOf(android.nfc.tech.MifareClassic::class.java.name),
arrayOf(android.nfc.tech.NfcV::class.java.name),
arrayOf(android.nfc.tech.NfcF::class.java.name),
arrayOf(android.nfc.tech.NdefFormatable::class.java.name),
arrayOf(android.nfc.tech.MifareUltralight::class.java.name)
)
class MainActivity : AppCompatActivity() {
var pendintent: PendingIntent? = null
var nfc_adapt: NfcAdapter? = null
var iFilter: IntentFilter? = null
var tview: TextView? = null
var read_button: Button? = null // read button
var delete_button: Button? = null // delete button for content of text view that display tag information
@SuppressLint("ClickableViewAccessibility")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
tview = findViewById(R.id.textView) // 表示用のTextView
nfc_adapt = NfcAdapter.getDefaultAdapter(applicationContext)
pendintent = PendingIntent.getActivity(this, 0, Intent(this, javaClass).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0
)
iFilter = IntentFilter(NfcAdapter.ACTION_TECH_DISCOVERED)
val iFilters = arrayOf<IntentFilter>(iFilter!!)
read_button = findViewById(R.id.button)
delete_button = findViewById(R.id.button2)
delete_button!!.isEnabled = false
read_button!!.setOnTouchListener(fun(v: View, m: MotionEvent) : Boolean {
when (m.action) {
MotionEvent.ACTION_DOWN -> {
tview!!.setBackgroundColor(Color.rgb(0,0,0))
tview!!.setTextColor(Color.rgb(0, 255, 0))
tview!!.text = "Now Reading A Tag"
Log.d(null, "ACTION UPPED")
nfc_adapt!!.enableForegroundDispatch(this, pendintent,iFilters, null)
return false
}
MotionEvent.ACTION_UP -> {
nfc_adapt!!.disableForegroundDispatch(this)
tview!!.text = ""
return false
}
}
return false
})
// Delete contents of TextView
delete_button!!.setOnClickListener {
tview!!.text = ""
tview!!.setBackgroundColor(Color.rgb(0, 0, 0))
tview!!.setTextColor(Color.rgb(0, 255, 0))
read_button!!.isEnabled = true
it.isEnabled = false
}
}
override fun onNewIntent(intent: Intent) {
super.onNewIntent(intent)
val action: String? = intent.action
if (action.equals(NfcAdapter.ACTION_TECH_DISCOVERED)) {
var nfc_tag_array = intent.getParcelableArrayExtra(NfcAdapter.EXTRA_TAG)
if (nfc_tag_array == null) {
tview?.setBackgroundColor(Color.YELLOW)
tview?.setTextColor(Color.rgb(0,0,0))
tview?.text = "read a tag but does not contain tag data"
return
}
val t = nfc_tag_array[0] as Tag?
if (t == null) {
tview!!.text = "Read a tag but it a null"
} else {
tview!!.text = t.toString() // print tag information as a string
}
tview!!.setBackgroundColor(Color.YELLOW)
tview!!.setTextColor(Color.rgb(0,0,0))
nfc_adapt!!.disableForegroundDispatch(this)
read_button!!.isEnabled = false // disable read button until user push the delete button
delete_button!!.isEnabled = true
}
else {
// nothing to do
}
}
}