diff options
| author | Martijn Coenen <martijn.coenen@nxp.com> | 2010-12-13 16:18:41 +0100 |
|---|---|---|
| committer | Jeff Hamilton <jham@android.com> | 2010-12-13 09:58:58 -0600 |
| commit | e3f6336bcffc250da90ec864bccfa73ad1d016b9 (patch) | |
| tree | 696c8f6498e8c690d7f67c9d5d0f81436aa3648b /core/java | |
| parent | 641dd62155fd2eeddd93b2036154b13c05b70ba2 (diff) | |
| download | frameworks_base-e3f6336bcffc250da90ec864bccfa73ad1d016b9.zip frameworks_base-e3f6336bcffc250da90ec864bccfa73ad1d016b9.tar.gz frameworks_base-e3f6336bcffc250da90ec864bccfa73ad1d016b9.tar.bz2 | |
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
Diffstat (limited to 'core/java')
| -rw-r--r-- | core/java/android/nfc/technology/Ndef.java | 55 | ||||
| -rw-r--r-- | core/java/android/nfc/technology/NdefFormatable.java | 27 |
2 files changed, 50 insertions, 32 deletions
diff --git a/core/java/android/nfc/technology/Ndef.java b/core/java/android/nfc/technology/Ndef.java index aa3f04b..7e194aa 100644 --- a/core/java/android/nfc/technology/Ndef.java +++ b/core/java/android/nfc/technology/Ndef.java @@ -56,6 +56,7 @@ public final class Ndef extends BasicTagTechnology { private final int mMaxNdefSize; private final int mCardState; + private final NdefMessage mNdefMsg; /** * Internal constructor, to be used by NfcAdapter @@ -66,6 +67,7 @@ public final class Ndef extends BasicTagTechnology { if (extras != null) { mMaxNdefSize = extras.getInt(EXTRA_NDEF_MAXLENGTH); mCardState = extras.getInt(EXTRA_NDEF_CARDSTATE); + mNdefMsg = extras.getParcelable(EXTRA_NDEF_MSG); } else { throw new NullPointerException("NDEF tech extras are null."); } @@ -76,27 +78,8 @@ public final class Ndef extends BasicTagTechnology { * Get the primary NDEF message on this tag. This data is read at discovery time * and does not require a connection. */ - public NdefMessage getNdefMessage() throws IOException, FormatException { - try { - int serviceHandle = mTag.getServiceHandle(); - NdefMessage msg = mTagService.ndefRead(serviceHandle); - if (msg == null) { - int errorCode = mTagService.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(); - } - } - return msg; - } catch (RemoteException e) { - attemptDeadServiceRecovery(e); - return null; - } + public NdefMessage getCachedNdefMessage() { + return mNdefMsg; } /** @@ -126,6 +109,36 @@ public final class Ndef extends BasicTagTechnology { // Methods that require connect() /** + * Get the primary NDEF message on this tag. This data is read actively + * and requires a connection. + */ + public NdefMessage getNdefMessage() throws IOException, FormatException { + try { + int serviceHandle = mTag.getServiceHandle(); + if (mTagService.isNdef(serviceHandle)) { + NdefMessage msg = mTagService.ndefRead(serviceHandle); + if (msg == null) { + int errorCode = mTagService.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(); + } + } + return msg; + } else { + return null; + } + } catch (RemoteException e) { + attemptDeadServiceRecovery(e); + return null; + } + } + /** * Overwrite the primary NDEF message * @throws IOException */ diff --git a/core/java/android/nfc/technology/NdefFormatable.java b/core/java/android/nfc/technology/NdefFormatable.java index 05fd67c..bd21e58 100644 --- a/core/java/android/nfc/technology/NdefFormatable.java +++ b/core/java/android/nfc/technology/NdefFormatable.java @@ -73,17 +73,22 @@ public final class NdefFormatable extends BasicTagTechnology { // Should not happen throw new IOException(); } - errorCode = mTagService.ndefWrite(serviceHandle, firstMessage); - switch (errorCode) { - case ErrorCodes.SUCCESS: - break; - case ErrorCodes.ERROR_IO: - throw new IOException(); - case ErrorCodes.ERROR_INVALID_PARAM: - throw new FormatException(); - default: - // Should not happen - throw new IOException(); + // Now check and see if the format worked + if (mTagService.isNdef(serviceHandle)) { + errorCode = mTagService.ndefWrite(serviceHandle, firstMessage); + switch (errorCode) { + case ErrorCodes.SUCCESS: + break; + case ErrorCodes.ERROR_IO: + throw new IOException(); + case ErrorCodes.ERROR_INVALID_PARAM: + throw new FormatException(); + default: + // Should not happen + throw new IOException(); + } + } else { + throw new IOException(); } } catch (RemoteException e) { attemptDeadServiceRecovery(e); |
