From a032783241cbbed47ed05df32c56298ee0f9902b Mon Sep 17 00:00:00 2001 From: Martijn Coenen Date: Thu, 29 Mar 2012 10:53:08 -0700 Subject: [PATCH] Fix NDEF documentation to indicate the message may be null. The current NFC stack formats tags to the INITIALIZED state as defined by NFC forum; in that state the tag has the NDEF Capability Container, but does not contain any message yet. Tags in that state (correctly) return the NDEF technology, but the documentation does not specify that the message may be null. Also, get rid of buggy getLastErrorCode and use (cached) presence check value to determine if tag was lost during read. Change-Id: If4293428093024ba9cda5dd7c9979b8b06353234 --- core/java/android/nfc/INfcTag.aidl | 2 -- core/java/android/nfc/tech/Ndef.java | 24 +++++++++++------------- 2 files changed, 11 insertions(+), 15 deletions(-) diff --git a/core/java/android/nfc/INfcTag.aidl b/core/java/android/nfc/INfcTag.aidl index bb5a9fd32e6d7..102b6af5f64e1 100644 --- a/core/java/android/nfc/INfcTag.aidl +++ b/core/java/android/nfc/INfcTag.aidl @@ -34,8 +34,6 @@ interface INfcTag boolean isPresent(int nativeHandle); TransceiveResult transceive(int nativeHandle, in byte[] data, boolean raw); - int getLastError(int nativeHandle); - NdefMessage ndefRead(int nativeHandle); int ndefWrite(int nativeHandle, in NdefMessage msg); int ndefMakeReadOnly(int nativeHandle); diff --git a/core/java/android/nfc/tech/Ndef.java b/core/java/android/nfc/tech/Ndef.java index 226e079ca226e..b1d53036aaa88 100644 --- a/core/java/android/nfc/tech/Ndef.java +++ b/core/java/android/nfc/tech/Ndef.java @@ -176,8 +176,11 @@ public final class Ndef extends BasicTagTechnology { *

If the NDEF Message is modified by an I/O operation then it * will not be updated here, this function only returns what was discovered * when the tag entered the field. + *

Note that this method may return null if the tag was in the + * INITIALIZED state as defined by NFC Forum, as in this state the + * tag is formatted to support NDEF but does not contain a message yet. *

Does not cause any RF activity and does not block. - * @return NDEF Message read from the tag at discovery time + * @return NDEF Message read from the tag at discovery time, can be null */ public NdefMessage getCachedNdefMessage() { return mNdefMsg; @@ -245,11 +248,15 @@ public final class Ndef extends BasicTagTechnology { * *

This always reads the current NDEF Message stored on the tag. * + *

Note that this method may return null if the tag was in the + * INITIALIZED state as defined by NFC Forum, as in that state the + * tag is formatted to support NDEF but does not contain a message yet. + * *

This is an I/O operation and will block until complete. It must * not be called from the main application thread. A blocked call will be canceled with * {@link IOException} if {@link #close} is called from another thread. * - * @return the NDEF Message, never null + * @return the NDEF Message, can be null * @throws TagLostException if the tag leaves the field * @throws IOException if there is an I/O failure, or the operation is canceled * @throws FormatException if the NDEF Message on the tag is malformed @@ -265,17 +272,8 @@ public final class Ndef extends BasicTagTechnology { int serviceHandle = mTag.getServiceHandle(); if (tagService.isNdef(serviceHandle)) { NdefMessage msg = tagService.ndefRead(serviceHandle); - if (msg == null) { - int errorCode = tagService.getLastError(serviceHandle); - switch (errorCode) { - case ErrorCodes.ERROR_IO: - throw new IOException(); - case ErrorCodes.ERROR_INVALID_PARAM: - throw new FormatException(); - default: - // Should not happen - throw new IOException(); - } + if (msg == null && !tagService.isPresent(serviceHandle)) { + throw new TagLostException(); } return msg; } else {