Skip to content

Commit 5289b91

Browse files
Martijn CoenenNick Pelly
authored andcommitted
Implement ndef formatting.
Change-Id: I6e3e3abdc304bc14d7c93a413e42bf957963e288
1 parent 42a2378 commit 5289b91

File tree

3 files changed

+38
-3
lines changed

3 files changed

+38
-3
lines changed

core/java/android/nfc/INfcTag.aidl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,5 @@ interface INfcTag
3737
int write(int nativeHandle, in NdefMessage msg);
3838
int makeReadOnly(int nativeHandle);
3939
int getModeHint(int nativeHandle);
40-
}
40+
int formatNdef(int nativeHandle, in byte[] key);
41+
}

core/java/android/nfc/Tag.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import android.nfc.technology.MifareUltralight;
2222
import android.nfc.technology.NfcV;
2323
import android.nfc.technology.Ndef;
24+
import android.nfc.technology.NdefFormatable;
2425
import android.nfc.technology.NfcA;
2526
import android.nfc.technology.NfcB;
2627
import android.nfc.technology.NfcF;
@@ -157,6 +158,9 @@ public TagTechnology getTechnology(int tech) {
157158
case TagTechnology.NDEF: {
158159
return new Ndef(adapter, this, tech, extras);
159160
}
161+
case TagTechnology.NDEF_FORMATABLE: {
162+
return new NdefFormatable(adapter, this, tech, extras);
163+
}
160164
case TagTechnology.NFC_F: {
161165
return new NfcF(adapter, this, extras);
162166
}

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

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,37 @@ public boolean canBeFormatted() throws IOException {
5656
* Formats a tag as NDEF, if possible. You may supply a first
5757
* NdefMessage to be written on the tag.
5858
*/
59-
public void format(NdefMessage firstMessage) throws IOException {
60-
throw new UnsupportedOperationException();
59+
public void format(NdefMessage firstMessage) throws IOException, FormatException {
60+
try {
61+
byte[] DEFAULT_KEY = {(byte)0xFF,(byte)0xFF,(byte)0xFF,
62+
(byte)0xFF,(byte)0xFF,(byte)0xFF};
63+
int serviceHandle = mTag.getServiceHandle();
64+
int errorCode = mTagService.formatNdef(serviceHandle, DEFAULT_KEY);
65+
switch (errorCode) {
66+
case ErrorCodes.SUCCESS:
67+
break;
68+
case ErrorCodes.ERROR_IO:
69+
throw new IOException();
70+
case ErrorCodes.ERROR_INVALID_PARAM:
71+
throw new FormatException();
72+
default:
73+
// Should not happen
74+
throw new IOException();
75+
}
76+
errorCode = mTagService.write(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();
87+
}
88+
} catch (RemoteException e) {
89+
attemptDeadServiceRecovery(e);
90+
}
6191
}
6292
}

0 commit comments

Comments
 (0)