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

Commit 5289b91c authored by Martijn Coenen's avatar Martijn Coenen Committed by Nick Pelly
Browse files

Implement ndef formatting.

Change-Id: I6e3e3abdc304bc14d7c93a413e42bf957963e288
parent 42a23783
Loading
Loading
Loading
Loading
+2 −1
Original line number Diff line number Diff line
@@ -37,4 +37,5 @@ interface INfcTag
    int write(int nativeHandle, in NdefMessage msg);
    int makeReadOnly(int nativeHandle);
    int getModeHint(int nativeHandle);
    int formatNdef(int nativeHandle, in byte[] key);
}
+4 −0
Original line number Diff line number Diff line
@@ -21,6 +21,7 @@ import android.nfc.technology.MifareClassic;
import android.nfc.technology.MifareUltralight;
import android.nfc.technology.NfcV;
import android.nfc.technology.Ndef;
import android.nfc.technology.NdefFormatable;
import android.nfc.technology.NfcA;
import android.nfc.technology.NfcB;
import android.nfc.technology.NfcF;
@@ -157,6 +158,9 @@ public class Tag implements Parcelable {
                case TagTechnology.NDEF: {
                    return new Ndef(adapter, this, tech, extras);
                }
                case TagTechnology.NDEF_FORMATABLE: {
                    return new NdefFormatable(adapter, this, tech, extras);
                }
                case TagTechnology.NFC_F: {
                    return new NfcF(adapter, this, extras);
                }
+32 −2
Original line number Diff line number Diff line
@@ -56,7 +56,37 @@ public final class NdefFormatable extends BasicTagTechnology {
     * Formats a tag as NDEF, if possible. You may supply a first
     * NdefMessage to be written on the tag.
     */
    public void format(NdefMessage firstMessage) throws IOException {
      throw new UnsupportedOperationException();
    public void format(NdefMessage firstMessage) throws IOException, FormatException {
        try {
            byte[] DEFAULT_KEY = {(byte)0xFF,(byte)0xFF,(byte)0xFF,
                                  (byte)0xFF,(byte)0xFF,(byte)0xFF};
            int serviceHandle = mTag.getServiceHandle();
            int errorCode = mTagService.formatNdef(serviceHandle, DEFAULT_KEY);
            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();
            }
            errorCode = mTagService.write(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();
            }
        } catch (RemoteException e) {
            attemptDeadServiceRecovery(e);
        }
    }
}