semlette / nfc_in_flutter

Cross-platform flutter plugin for reading and writing NFC tags. Not maintained anymore - not looking for new maintainer, fork instead.
MIT License
120 stars 119 forks source link

java.lang.StringIndexOutOfBoundsException when writing to a tag #22

Open AntSwift opened 4 years ago

AntSwift commented 4 years ago

Hi,

Using the example app, without changes, I am hitting a StringIndexOutOfBoundsException exception when attempting to write to a tag. The message has a single record text/plain - Test.

Here is the stack trace:

W/Binder (24507): Caught a RuntimeException from the binder stub implementation. W/Binder (24507): java.lang.StringIndexOutOfBoundsException: length=36; regionStart=1; regionLength=55 W/Binder (24507): at java.lang.StringFactory.newStringFromBytes(StringFactory.java:79) W/Binder (24507): at me.andisemler.nfc_in_flutter.NfcInFlutterPlugin.formatNDEFMessageToResult(NfcInFlutterPlugin.java:389) W/Binder (24507): at me.andisemler.nfc_in_flutter.NfcInFlutterPlugin.onTagDiscovered(NfcInFlutterPlugin.java:206) W/Binder (24507): at android.nfc.NfcActivityManager.onTagDiscovered(NfcActivityManager.java:447) W/Binder (24507): at android.nfc.IAppCallback$Stub.onTransact(IAppCallback.java:128) W/Binder (24507): at android.os.Binder.execTransactInternal(Binder.java:1021) W/Binder (24507): at android.os.Binder.execTransact(Binder.java:994)

The current content of the tag is text/plain - 03ead229-6cf3-4a2e-bc1f-b8b930f6a2b8 - this was written to the tag via a different means and I wish to overwrite it.

I should add that reading the tag has the same result.

I need to do some more debugging but perhaps onTagDiscovered shouldn't assume the tag will contain correct data. Alternatively, perform any writes first in onTagDiscovered, before attempting to parse the contents.

semlette commented 4 years ago

I am not able to reproduce this.

I have tried

Neither caused an exception during my testing.

Are you using the example app? If not, would you mind showing the Dart code that writes to the tag?

AntSwift commented 4 years ago

Hi, I'm not at the desk right now but to add more info, the tag was written with a different app.

The tags current content will be missing any language bit and language code which is being parsed (and failing) by the library causing the error.

I am using the example app as it is in the repo.

Ant

semlette commented 4 years ago

The language code should only be checked if the record has a type of T along with a TNF of WELL_KNOWN. If the record does not meet both criteria it will not check the language code or URL prefix if it was type U.

Even reading or writing a non-conforming NDEF message similar to your current one does not cause an exception on my end.