@@ -176,8 +176,11 @@ public Ndef(Tag tag) throws RemoteException {
176176 * <p>If the NDEF Message is modified by an I/O operation then it
177177 * will not be updated here, this function only returns what was discovered
178178 * when the tag entered the field.
179+ * <p>Note that this method may return null if the tag was in the
180+ * INITIALIZED state as defined by NFC Forum, as in this state the
181+ * tag is formatted to support NDEF but does not contain a message yet.
179182 * <p>Does not cause any RF activity and does not block.
180- * @return NDEF Message read from the tag at discovery time
183+ * @return NDEF Message read from the tag at discovery time, can be null
181184 */
182185 public NdefMessage getCachedNdefMessage () {
183186 return mNdefMsg ;
@@ -245,11 +248,15 @@ public boolean isWritable() {
245248 *
246249 * <p>This always reads the current NDEF Message stored on the tag.
247250 *
251+ * <p>Note that this method may return null if the tag was in the
252+ * INITIALIZED state as defined by NFC Forum, as in that state the
253+ * tag is formatted to support NDEF but does not contain a message yet.
254+ *
248255 * <p>This is an I/O operation and will block until complete. It must
249256 * not be called from the main application thread. A blocked call will be canceled with
250257 * {@link IOException} if {@link #close} is called from another thread.
251258 *
252- * @return the NDEF Message, never null
259+ * @return the NDEF Message, can be null
253260 * @throws TagLostException if the tag leaves the field
254261 * @throws IOException if there is an I/O failure, or the operation is canceled
255262 * @throws FormatException if the NDEF Message on the tag is malformed
@@ -265,17 +272,8 @@ public NdefMessage getNdefMessage() throws IOException, FormatException {
265272 int serviceHandle = mTag .getServiceHandle ();
266273 if (tagService .isNdef (serviceHandle )) {
267274 NdefMessage msg = tagService .ndefRead (serviceHandle );
268- if (msg == null ) {
269- int errorCode = tagService .getLastError (serviceHandle );
270- switch (errorCode ) {
271- case ErrorCodes .ERROR_IO :
272- throw new IOException ();
273- case ErrorCodes .ERROR_INVALID_PARAM :
274- throw new FormatException ();
275- default :
276- // Should not happen
277- throw new IOException ();
278- }
275+ if (msg == null && !tagService .isPresent (serviceHandle )) {
276+ throw new TagLostException ();
279277 }
280278 return msg ;
281279 } else {
0 commit comments