tidev / ti.nfc

Read Android NFC sensors with the Titanium SDK.
Other
26 stars 27 forks source link

Foreground Dispatch NDEF intent object has null type and action #80

Open drunnells opened 1 year ago

drunnells commented 1 year ago

I recently attempted to bring a 3 year old project that uses ti.nfc up to date by targeting android 33, updating to Titanium SDK 12.1.2.GA and ti.nfc 5.0.0, but ran into problems scanning NDEF tags that are readable with an old build of my project. Here is my e.inent for a read of a ISO 14443-3A NXP-Mifare Classic 1k tag (Tech: NfcA,MifareClassic,Ndef) tag:

[INFO] Intent {
[INFO] url: undefined,
[INFO] type: null,
[INFO] packageName: 'com.something.something',
[INFO] className: 'org.appcelerator.titanium.TiActivity',
[INFO] action: null,
[INFO] flags: 805306368,
[INFO] data: null,
[INFO] _hasJavaListener: undefined,
[INFO] apiName: 'Ti.Android.Intent',
[INFO] bubbleParent: true
[INFO] }

Reading the tag did fire a 'newintent' event, but onNdefDiscovered, onTagDiscovered and onTechDiscovered never caught it, even though my intentFilters never changed. Catching the intents outside of foreground dispatch worked as expected.

Trying multiple combinations of different Titanium SDKs and ti.nfc versions didn't seem to help, so I can't be certain what changed or when. BUT I did find a potential fix here: https://stackoverflow.com/questions/76655730/why-nfc-works-with-pendingintent-flag-mutable-and-not-with-pendingintent-flag-im

Changing the createPendingIntent() in NfcForegroundDispatchFilter's to be FLAG_MUTABLE instead of FLAG_IMMUTABLE, with no other changes, solved my problem and I can read NDEF tags again. The e.intent now looks like:

[INFO] Intent {
[INFO] url: undefined,
[INFO] type: 'text/plain',
[INFO] packageName: 'com.something.something',
[INFO] className: 'org.appcelerator.titanium.TiActivity',
[INFO] action: 'android.nfc.action.NDEF_DISCOVERED',
[INFO] flags: 805306368,
[INFO] data: null,
[INFO] _hasJavaListener: undefined,
[INFO] apiName: 'Ti.Android.Intent',
[INFO] bubbleParent: true
[INFO] }

To be honest, this is a little over my head and I'm not sure if this is the best fix. If anyone is interested, my super minor edit can be found in pull request https://github.com/tidev/ti.nfc/pull/79

m1ga commented 1 year ago

I was testing https://github.com/tidev/ti.nfc/tree/master/android/example/TagForeground/app with and without your PR and both returned the same values for me

[INFO]  {
[INFO]  action: 'android.nfc.action.NDEF_DISCOVERED',
[INFO]  messages: [
[INFO]  NdefMessage {
[INFO]  records: [Array],
[INFO]  _hasJavaListener: undefined,
[INFO]  apiName: 'Ti.Proxy',
[INFO]  bubbleParent: true
[INFO]  }
[INFO]  ],
[INFO]  tag: NfcTag {
[INFO]  techList: [ 'android.nfc.tech.NfcV', 'android.nfc.tech.Ndef' ],
[INFO]  id: '1628464C000104E0',
[INFO]  _hasJavaListener: undefined,
[INFO]  apiName: 'Ti.Proxy',
[INFO]  bubbleParent: true
[INFO]  }
[INFO]  }

That said: I don't have a real test app and different tags. But the change to MUTABLE shouldn't be hurting the module or other users. So I'll vote for including it