Skip to content

Commit f54ad8d

Browse files
author
Nick Pelly
committed
Clean up transceive().
BasicTagTechnology.transceive(byte[] raw) should work for most child classes, except those that want to disable (raw) transceive. Current plan is not to add transceiveMifare() etc - use explicit methods instead. Add package scoped BasicTagTechnology.transceive(byte[] rata, boolean raw) as a helper to remove code duplication. Change-Id: Iaea161022751c99058d291e2ed0f8c475d1c7282
1 parent 06d19ef commit f54ad8d

File tree

3 files changed

+27
-77
lines changed

3 files changed

+27
-77
lines changed

core/java/android/nfc/technology/BasicTagTechnology.java

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,22 @@ public void close() {
226226
}
227227
}
228228

229+
/** internal transceive */
230+
/*package*/ byte[] transceive(byte[] data, boolean raw) throws IOException {
231+
checkConnected();
232+
233+
try {
234+
byte[] response = mTagService.transceive(mTag.getServiceHandle(), data, raw);
235+
if (response == null) {
236+
throw new IOException("transceive failed");
237+
}
238+
return response;
239+
} catch (RemoteException e) {
240+
attemptDeadServiceRecovery(e);
241+
throw new IOException("NFC service died");
242+
}
243+
}
244+
229245
/**
230246
* Send data to a tag and receive the response.
231247
* <p>
@@ -238,17 +254,6 @@ public void close() {
238254
* @throws IOException if the target is lost or connection closed
239255
*/
240256
public byte[] transceive(byte[] data) throws IOException {
241-
checkConnected();
242-
243-
try {
244-
byte[] response = mTagService.transceive(mTag.getServiceHandle(), data, true);
245-
if (response == null) {
246-
throw new IOException("transceive failed");
247-
}
248-
return response;
249-
} catch (RemoteException e) {
250-
attemptDeadServiceRecovery(e);
251-
throw new IOException("NFC service died");
252-
}
257+
return transceive(data, true);
253258
}
254259
}

core/java/android/nfc/technology/MifareClassic.java

Lines changed: 7 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ public boolean authenticateBlock(int block, byte[] key, boolean keyA) {
263263
System.arraycopy(key, 0, cmd, 6, 6);
264264

265265
try {
266-
if ((transceive(cmd) != null)) {
266+
if ((transceive(cmd, false) != null)) {
267267
return true;
268268
}
269269
} catch (IOException e) {
@@ -308,7 +308,7 @@ public byte[] readBlock(int block) throws IOException {
308308
byte addr = (byte) block;
309309
byte[] blockread_cmd = { 0x30, addr };
310310

311-
return transceive(blockread_cmd);
311+
return transceive(blockread_cmd, false);
312312
}
313313

314314
/**
@@ -324,7 +324,7 @@ public void writeBlock(int block, byte[] data) throws IOException {
324324
blockwrite_cmd[1] = addr;
325325
System.arraycopy(data, 0, blockwrite_cmd, 2, data.length);
326326

327-
transceive(blockwrite_cmd);
327+
transceive(blockwrite_cmd, false);
328328
}
329329

330330
/**
@@ -345,7 +345,7 @@ public void increment(int block) throws IOException {
345345
byte addr = (byte) block;
346346
byte[] incr_cmd = { (byte) 0xC1, (byte) block };
347347

348-
transceive(incr_cmd);
348+
transceive(incr_cmd, false);
349349
}
350350

351351
public void decrement(int block) throws IOException {
@@ -354,7 +354,7 @@ public void decrement(int block) throws IOException {
354354
byte addr = (byte) block;
355355
byte[] decr_cmd = { (byte) 0xC0, (byte) block };
356356

357-
transceive(decr_cmd);
357+
transceive(decr_cmd, false);
358358
}
359359

360360
public void transfer(int block) throws IOException {
@@ -363,7 +363,7 @@ public void transfer(int block) throws IOException {
363363
byte addr = (byte) block;
364364
byte[] trans_cmd = { (byte) 0xB0, (byte) block };
365365

366-
transceive(trans_cmd);
366+
transceive(trans_cmd, false);
367367
}
368368

369369
public void restore(int block) throws IOException {
@@ -372,33 +372,6 @@ public void restore(int block) throws IOException {
372372
byte addr = (byte) block;
373373
byte[] rest_cmd = { (byte) 0xC2, (byte) block };
374374

375-
transceive(rest_cmd);
376-
}
377-
378-
/**
379-
* Send data to a tag and receive the response.
380-
* <p>
381-
* This method will block until the response is received. It can be canceled
382-
* with {@link #close}.
383-
* <p>Requires {@link android.Manifest.permission#NFC} permission.
384-
*
385-
* @param data bytes to send
386-
* @return bytes received in response
387-
* @throws IOException if the target is lost or connection closed
388-
*/
389-
@Override
390-
public byte[] transceive(byte[] data) throws IOException {
391-
checkConnected();
392-
393-
try {
394-
byte[] response = mTagService.transceive(mTag.getServiceHandle(), data, false);
395-
if (response == null) {
396-
throw new IOException("transceive failed");
397-
}
398-
return response;
399-
} catch (RemoteException e) {
400-
attemptDeadServiceRecovery(e);
401-
throw new IOException("NFC service died");
402-
}
375+
transceive(rest_cmd, false);
403376
}
404377
}

core/java/android/nfc/technology/MifareUltralight.java

Lines changed: 3 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public byte[] readBlock(int block) throws IOException {
6969
checkConnected();
7070

7171
byte[] blockread_cmd = { 0x30, (byte)block }; // phHal_eMifareRead
72-
return transceive(blockread_cmd);
72+
return transceive(blockread_cmd, false);
7373
}
7474

7575
/**
@@ -89,7 +89,7 @@ public void writePage(int block, byte[] data) throws IOException {
8989
pagewrite_cmd[1] = (byte) block;
9090
System.arraycopy(data, 0, pagewrite_cmd, 2, data.length);
9191

92-
transceive(pagewrite_cmd);
92+
transceive(pagewrite_cmd, false);
9393
}
9494

9595
public void writeBlock(int block, byte[] data) throws IOException {
@@ -100,34 +100,6 @@ public void writeBlock(int block, byte[] data) throws IOException {
100100
blockwrite_cmd[1] = (byte) block;
101101
System.arraycopy(data, 0, blockwrite_cmd, 2, data.length);
102102

103-
transceive(blockwrite_cmd);
103+
transceive(blockwrite_cmd, false);
104104
}
105-
106-
/**
107-
* Send data to a tag and receive the response.
108-
* <p>
109-
* This method will block until the response is received. It can be canceled
110-
* with {@link #close}.
111-
* <p>Requires {@link android.Manifest.permission#NFC} permission.
112-
*
113-
* @param data bytes to send
114-
* @return bytes received in response
115-
* @throws IOException if the target is lost or connection closed
116-
*/
117-
@Override
118-
public byte[] transceive(byte[] data) throws IOException {
119-
checkConnected();
120-
121-
try {
122-
byte[] response = mTagService.transceive(mTag.getServiceHandle(), data, false);
123-
if (response == null) {
124-
throw new IOException("transceive failed");
125-
}
126-
return response;
127-
} catch (RemoteException e) {
128-
attemptDeadServiceRecovery(e);
129-
throw new IOException("NFC service died");
130-
}
131-
}
132-
133105
}

0 commit comments

Comments
 (0)