Skip to content

Commit e3f6336

Browse files
Martijn Coenenjham
authored andcommitted
Fixed API for active NDEF reading and NDEF formatting.
- Added getNdefCached() to return the message read at discovery time. - Fixed format() to check ndef before doing the write: libnfc actually requires a checkNdef to be done before writing. Change-Id: I9b3108299c05539bdef92dd74f62f911fb5a16bf
1 parent 641dd62 commit e3f6336

File tree

2 files changed

+50
-32
lines changed

2 files changed

+50
-32
lines changed

core/java/android/nfc/technology/Ndef.java

Lines changed: 34 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -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

core/java/android/nfc/technology/NdefFormatable.java

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -73,17 +73,22 @@ public void format(NdefMessage firstMessage) throws IOException, FormatException
7373
// Should not happen
7474
throw new IOException();
7575
}
76-
errorCode = mTagService.ndefWrite(serviceHandle, firstMessage);
77-
switch (errorCode) {
78-
case ErrorCodes.SUCCESS:
79-
break;
80-
case ErrorCodes.ERROR_IO:
81-
throw new IOException();
82-
case ErrorCodes.ERROR_INVALID_PARAM:
83-
throw new FormatException();
84-
default:
85-
// Should not happen
86-
throw new IOException();
76+
// Now check and see if the format worked
77+
if (mTagService.isNdef(serviceHandle)) {
78+
errorCode = mTagService.ndefWrite(serviceHandle, firstMessage);
79+
switch (errorCode) {
80+
case ErrorCodes.SUCCESS:
81+
break;
82+
case ErrorCodes.ERROR_IO:
83+
throw new IOException();
84+
case ErrorCodes.ERROR_INVALID_PARAM:
85+
throw new FormatException();
86+
default:
87+
// Should not happen
88+
throw new IOException();
89+
}
90+
} else {
91+
throw new IOException();
8792
}
8893
} catch (RemoteException e) {
8994
attemptDeadServiceRecovery(e);

0 commit comments

Comments
 (0)