Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit e3f6336b authored by Martijn Coenen's avatar Martijn Coenen Committed by Jeff Hamilton
Browse files

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
parent 641dd621
Loading
Loading
Loading
Loading
+34 −21
Original line number Diff line number Diff line
@@ -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;
    }

    /**
@@ -125,6 +108,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
+16 −11
Original line number Diff line number Diff line
@@ -73,6 +73,8 @@ public final class NdefFormatable extends BasicTagTechnology {
                    // 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:
@@ -85,6 +87,9 @@ public final class NdefFormatable extends BasicTagTechnology {
                        // Should not happen
                        throw new IOException();
                }
            } else {
                throw new IOException();
            }
        } catch (RemoteException e) {
            attemptDeadServiceRecovery(e);
        }