@@ -56,6 +56,7 @@ public final class Ndef extends BasicTagTechnology {
5656
5757 private final int mMaxNdefSize ;
5858 private final int mCardState ;
59+ private final NdefMessage mNdefMsg ;
5960
6061 /**
6162 * Internal constructor, to be used by NfcAdapter
@@ -66,6 +67,7 @@ public Ndef(NfcAdapter adapter, Tag tag, int tech, Bundle extras) throws RemoteE
6667 if (extras != null ) {
6768 mMaxNdefSize = extras .getInt (EXTRA_NDEF_MAXLENGTH );
6869 mCardState = extras .getInt (EXTRA_NDEF_CARDSTATE );
70+ mNdefMsg = extras .getParcelable (EXTRA_NDEF_MSG );
6971 } else {
7072 throw new NullPointerException ("NDEF tech extras are null." );
7173 }
@@ -76,27 +78,8 @@ public Ndef(NfcAdapter adapter, Tag tag, int tech, Bundle extras) throws RemoteE
7678 * Get the primary NDEF message on this tag. This data is read at discovery time
7779 * and does not require a connection.
7880 */
79- public NdefMessage getNdefMessage () throws IOException , FormatException {
80- try {
81- int serviceHandle = mTag .getServiceHandle ();
82- NdefMessage msg = mTagService .ndefRead (serviceHandle );
83- if (msg == null ) {
84- int errorCode = mTagService .getLastError (serviceHandle );
85- switch (errorCode ) {
86- case ErrorCodes .ERROR_IO :
87- throw new IOException ();
88- case ErrorCodes .ERROR_INVALID_PARAM :
89- throw new FormatException ();
90- default :
91- // Should not happen
92- throw new IOException ();
93- }
94- }
95- return msg ;
96- } catch (RemoteException e ) {
97- attemptDeadServiceRecovery (e );
98- return null ;
99- }
81+ public NdefMessage getCachedNdefMessage () {
82+ return mNdefMsg ;
10083 }
10184
10285 /**
@@ -125,6 +108,36 @@ public boolean isWritable() {
125108 }
126109
127110 // Methods that require connect()
111+ /**
112+ * Get the primary NDEF message on this tag. This data is read actively
113+ * and requires a connection.
114+ */
115+ public NdefMessage getNdefMessage () throws IOException , FormatException {
116+ try {
117+ int serviceHandle = mTag .getServiceHandle ();
118+ if (mTagService .isNdef (serviceHandle )) {
119+ NdefMessage msg = mTagService .ndefRead (serviceHandle );
120+ if (msg == null ) {
121+ int errorCode = mTagService .getLastError (serviceHandle );
122+ switch (errorCode ) {
123+ case ErrorCodes .ERROR_IO :
124+ throw new IOException ();
125+ case ErrorCodes .ERROR_INVALID_PARAM :
126+ throw new FormatException ();
127+ default :
128+ // Should not happen
129+ throw new IOException ();
130+ }
131+ }
132+ return msg ;
133+ } else {
134+ return null ;
135+ }
136+ } catch (RemoteException e ) {
137+ attemptDeadServiceRecovery (e );
138+ return null ;
139+ }
140+ }
128141 /**
129142 * Overwrite the primary NDEF message
130143 * @throws IOException
0 commit comments