1

I'm trying to write a NFC tag using NdefRecord/NdefMessage.

byte prefix = 0x04; // https://
byte[] uriBytes = "whatever.anywhere.com".getBytes();
byte[] recordBytes = new byte[uriBytes.length + 1];
recordBytes[0] = prefix;
System.arraycopy(uriBytes, 0, recordBytes, 1, uriBytes.length);

NdefRecord record = new NdefRecord(
    NdefRecord.TNF_WELL_KNOWN,
    NdefRecord.RTD_URI,
    null,
    recordBytes);

This is how I create my NdefRecord, I can't use createUri() due to compatibility issues (createUri() is available since API14 and I need API11 compat...)

It works great on Lollipop (tested on 5.0.2) and event on KitKat (tested on 4.4.3).

The thing that one of my users has a crash when creating the NdefRecord:

NdefRecord record = new NdefRecord(
    NdefRecord.TNF_WELL_KNOWN,
    NdefRecord.RTD_URI,
    null,
    recordBytes);

He is on ICS 4.0.2, I can't really debug this issue because I don't such phone available and I can't use AVD to simulate this issue.

Does anyone see something wrong with what I do? Or is there another/better way to do it?

EDIT: Here is the stack trace associated to this error

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.your.application/com.your.application.NFCWriter}: java.lang.IllegalArgumentException: Illegal null argument
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1967)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1992)
        at android.app.ActivityThread.access$600(ActivityThread.java:127)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1158)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:4448)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:823)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:590)
        at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.IllegalArgumentException: Illegal null argument
        at android.nfc.NdefRecord.<init>(NdefRecord.java:242)
        at android.nfc.NdefRecord.<init>(NdefRecord.java:233)
        at com.your.application.NFCWriter.onCreate(Unknown Source)
        at android.app.Activity.performCreate(Activity.java:4465)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1931)
        ... 11 more
4
  • Code looks okay. I suggest that you add some more details on the "crash" as otherwise this question is likely to be off-topic on SO. You could, for instance, use the stack trace from the bug report (if your app is on Play Store) or implement your own exception handler within your app that provides you debug information. Mar 3, 2015 at 17:01
  • That's the tricky part, the app is not on the play store because it's for internal use at my workplace so I don't have access to the bug report.
    – Jesus_21
    Mar 3, 2015 at 17:43
  • I knew that the issue came from this statement because the user was able to set breakpoints to check WHEN it crashed, but I don't have his stack trace though... Adding an handler to send debug info sounds like a great idea but how can I get debug info through the app (an then send it) ?
    – Jesus_21
    Mar 3, 2015 at 17:50
  • Definitely off-topic but the magic terms that you would want to search for are UncaughtExceptionHandler and setDefaultUncaughtExceptionHandler. You can use that to collect data on the exception that causes the crash and to send that data to you (or to pack that data into a file that the user can manually send to you). Mar 3, 2015 at 18:44

1 Answer 1

2

I guess I found my answer.

Even though the API document says that using null for type, id or payload is OK, it is not. --> API Reference

The thing is true since Android 4.1.1 only, for those interested here is source code:

In NdefRecord Constructor (4.1.1_r1) null is replace by an EMPTY_BYTE_ARRAY (which is a new byte[0] nonetheless)

While in NdefRecord Constructor (4.0.4_r2.1) using null as argument throws an IllegalArgumentException

So if you want to be backward compatible and construct NdefRecord, DON'T EVER PROVIDE null AS ARGUMENT, USE new byte[0] INSTEAD

byte prefix = 0x04; // https://
byte[] uriBytes = "whatever.anywhere.com".getBytes();
byte[] recordBytes = new byte[uriBytes.length + 1];
recordBytes[0] = prefix;
System.arraycopy(uriBytes, 0, recordBytes, 1, uriBytes.length);

NdefRecord record = new NdefRecord(
    NdefRecord.TNF_WELL_KNOWN,
    NdefRecord.RTD_URI,
    new byte[0],
    recordBytes);

That's about it.

Thanks @michael-roland for the leads on UncaughtExceptionHandler, it helped.

2
  • Would be great if you could document the exception that you received in your question. Mar 4, 2015 at 14:08
  • 1
    I added the stack trace in the question.
    – Jesus_21
    Mar 4, 2015 at 14:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.