Skip to content

Commit fb1a053

Browse files
committed
Added a feature of Reading the ID of the NFC Card only with param of "id"
1 parent cff225e commit fb1a053

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

app/src/main/java/com/termux/api/apis/NfcAPI.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
import com.termux.api.util.ResultReturner;
2020
import com.termux.shared.logger.Logger;
2121

22+
import java.nio.charset.StandardCharsets;
23+
2224
public class NfcAPI {
2325

2426
private static final String LOG_TAG = "NfcAPI";
@@ -165,6 +167,9 @@ public void writeJson(JsonWriter out) throws Exception {
165167
case "full":
166168
readFullNDEFTag(intent,out);
167169
break;
170+
case "id":
171+
readNDEFID(intent,out);
172+
break;
168173
case "noData":
169174
readNDEFTag(intent,out);
170175
break;
@@ -245,6 +250,18 @@ public void readNDEFTag(Intent intent, JsonWriter out) throws Exception {
245250
}
246251
}
247252

253+
public void readNDEFID(Intent intent, JsonWriter out) throws Exception {
254+
NfcAdapter adapter = NfcAdapter.getDefaultAdapter(this);
255+
byte[] tag_id = intent.getByteArrayExtra(NfcAdapter.EXTRA_ID);
256+
out.beginObject();
257+
StringBuilder sb = new StringBuilder();
258+
for (byte b : tag_id) {
259+
sb.append(String.format("%02X", b));
260+
}
261+
out.name("card_id").value(sb.toString());
262+
out.endObject();
263+
}
264+
248265
public void readFullNDEFTag(Intent intent, JsonWriter out) throws Exception {
249266
Logger.logVerbose(LOG_TAG, "readFullNDEFTag");
250267

0 commit comments

Comments
 (0)