|
1 | | -package nfc.sample.nfctransaction.commands; |
2 | | - |
3 | | -/* |
4 | | - * Copyright (c) 2012 Research In Motion Limited. |
5 | | - * |
6 | | - * Licensed under the Apache License, Version 2.0 (the "License"); |
7 | | - * you may not use this file except in compliance with the License. |
8 | | - * You may obtain a copy of the License at |
9 | | - * |
10 | | - * http://www.apache.org/licenses/LICENSE-2.0 |
11 | | - * |
12 | | - * Unless required by applicable law or agreed to in writing, software |
13 | | - * distributed under the License is distributed on an "AS IS" BASIS, |
14 | | - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
15 | | - * See the License for the specific language governing permissions and |
16 | | - * limitations under the License. |
17 | | - */ |
18 | | -import javax.microedition.apdu.APDUConnection; |
19 | | -import javax.microedition.io.Connector; |
20 | | - |
21 | | -import net.rim.device.api.command.AlwaysExecutableCommand; |
22 | | -import net.rim.device.api.command.ReadOnlyCommandMetadata; |
23 | | -import net.rim.device.api.util.ByteArrayUtilities; |
24 | | -import nfc.sample.nfctransaction.Constants; |
25 | | -import nfc.sample.nfctransaction.Settings; |
26 | | -import nfc.sample.nfctransaction.Utilities; |
27 | | -import nfc.sample.nfctransaction.ui.NfcTransScreen; |
28 | | -import nfc.sample.nfctransaction.ui.buttons.MultiStateButtonField; |
29 | | - |
30 | | -/** |
31 | | - * This command sends a ISO7816-4 SELECT APDU using the JSR177 API followed by a proprietary APDU. For this to work, the target |
32 | | - * SecureElement must contain an applet with the AID used here. So developers who just run this code to see what happens are |
33 | | - * likely to see an error from this command. This is to be expected however. |
34 | | - * |
35 | | - * Authors: John Murray and Martin Woolley |
36 | | - * |
37 | | - */ |
38 | | - |
39 | | -public class Iso7816Command extends AlwaysExecutableCommand { |
40 | | - |
41 | | - public void execute(ReadOnlyCommandMetadata metadata, Object context) { |
42 | | - MultiStateButtonField btn = (MultiStateButtonField) context; |
43 | | - if(btn.getMsbState() == Constants.BTN_SELECT_OFF) { |
44 | | - Utilities.log("XXXX " + Thread.currentThread().getName() + " ISO 7816-4 command exiting because button is in OFF state"); |
45 | | - return; |
46 | | - } |
47 | | - Settings settings = Settings.getInstance(); |
48 | | - NfcTransScreen screen = NfcTransScreen.getInstance(); |
49 | | - Utilities.log("XXXX " + Thread.currentThread().getName() + " selecting applet"); |
50 | | - APDUConnection apduConn = null; |
51 | | - // this is an example and completely proprietary APDU included solely to demonstrate the basic use of JSR-177 |
52 | | - // Decode: |
53 | | - // 0x80 : CLASS = proprietary |
54 | | - // 0x01 : INS = invented command! Interpretation will require the selected applet to understand this command |
55 | | - // 0x00 : P1 = null |
56 | | - // 0x00 : P1 = null |
57 | | - byte[] command = { (byte) 0x80, (byte) 0x01, (byte) 0x00, (byte) 0x00 }; |
58 | | - try { |
59 | | - // Open an APDUConnection to our applet. This results in an ISO 7816-4 SELECT command being sent by the JSR-177 API |
60 | | - String connection_string = Utilities.makeApduConnectionString(settings.getRegistered_aid()); |
61 | | - Utilities.log("XXXX " + Thread.currentThread().getName() + " ISO 7816-4 command: opening APDUConnection to " + connection_string); |
62 | | - apduConn = (APDUConnection) Connector.open(connection_string); |
63 | | - Utilities.log("XXXX " + Thread.currentThread().getName() + " ISO 7816-4 command: APDUConnection opened"); |
64 | | - screen.setUserMessage("Selected AID OK"); |
65 | | - // Send the APDU and wait for a response - expect an error here unless your applet supports the command that was sent |
66 | | - Utilities.log("XXXX " + Thread.currentThread().getName() + " ISO 7816-4 command: sending APDU"); |
67 | | - byte[] ret = apduConn.exchangeAPDU(command); |
68 | | - String resp_string = ByteArrayUtilities.byteArrayToHex(ret); |
69 | | - Utilities.log("XXXX " + Thread.currentThread().getName() + " ISO 7816-4 command: response=" + resp_string); |
70 | | - screen.setUserMessage("APDU response=" + resp_string); |
71 | | - // Close the logical channel connection |
72 | | - apduConn.close(); |
73 | | - } catch(Exception e) { |
74 | | - Utilities.log("XXXX " + Thread.currentThread().getName() + " ISO 7816-4 command failed:" + e.getClass().getName() + ":" + e.getMessage()); |
75 | | - screen.setUserMessage("Error. Select failed: check log"); |
76 | | - } |
77 | | - } |
78 | | - |
79 | | -} |
| 1 | +package nfc.sample.nfctransaction.commands; |
| 2 | + |
| 3 | +/* |
| 4 | + * Copyright (c) 2012 Research In Motion Limited. |
| 5 | + * |
| 6 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | + * you may not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | +import javax.microedition.apdu.APDUConnection; |
| 19 | +import javax.microedition.io.Connector; |
| 20 | + |
| 21 | +import net.rim.device.api.command.AlwaysExecutableCommand; |
| 22 | +import net.rim.device.api.command.ReadOnlyCommandMetadata; |
| 23 | +import net.rim.device.api.util.ByteArrayUtilities; |
| 24 | +import nfc.sample.nfctransaction.Constants; |
| 25 | +import nfc.sample.nfctransaction.Settings; |
| 26 | +import nfc.sample.nfctransaction.Utilities; |
| 27 | +import nfc.sample.nfctransaction.ui.NfcTransScreen; |
| 28 | +import nfc.sample.nfctransaction.ui.buttons.MultiStateButtonField; |
| 29 | + |
| 30 | +/** |
| 31 | + * This command sends a ISO7816-4 SELECT APDU using the JSR177 API followed by a proprietary APDU. For this to work, the target |
| 32 | + * SecureElement must contain an applet with the AID used here. So developers who just run this code to see what happens are |
| 33 | + * likely to see an error from this command. This is to be expected however. |
| 34 | + * |
| 35 | + * Authors: John Murray and Martin Woolley |
| 36 | + * |
| 37 | + */ |
| 38 | + |
| 39 | +public class Iso7816Command extends AlwaysExecutableCommand { |
| 40 | + |
| 41 | + public void execute(ReadOnlyCommandMetadata metadata, Object context) { |
| 42 | + MultiStateButtonField btn = (MultiStateButtonField) context; |
| 43 | + if(btn.getMsbState() == Constants.BTN_SELECT_OFF) { |
| 44 | + Utilities.log("XXXX " + Thread.currentThread().getName() + " ISO 7816-4 command exiting because button is in OFF state"); |
| 45 | + return; |
| 46 | + } |
| 47 | + Settings settings = Settings.getInstance(); |
| 48 | + NfcTransScreen screen = NfcTransScreen.getInstance(); |
| 49 | + Utilities.log("XXXX " + Thread.currentThread().getName() + " selecting applet"); |
| 50 | + APDUConnection apduConn = null; |
| 51 | + // this is an example and completely proprietary APDU included solely to demonstrate the basic use of JSR-177 |
| 52 | + // Decode: |
| 53 | + // 0x80 : CLASS = proprietary |
| 54 | + // 0x01 : INS = invented command! Interpretation will require the selected applet to understand this command |
| 55 | + // 0x00 : P1 = null |
| 56 | + // 0x00 : P1 = null |
| 57 | + byte[] command = settings.getAPDU(); |
| 58 | + try { |
| 59 | + // Open an APDUConnection to our applet. This results in an ISO 7816-4 SELECT command being sent by the JSR-177 API |
| 60 | + String connection_string = Utilities.makeApduConnectionString(settings.getRegisteredAIDAsString()); |
| 61 | + Utilities.log("XXXX " + Thread.currentThread().getName() + " ISO 7816-4 command: opening APDUConnection to " + connection_string); |
| 62 | + apduConn = (APDUConnection) Connector.open(connection_string); |
| 63 | + Utilities.log("XXXX " + Thread.currentThread().getName() + " ISO 7816-4 command: APDUConnection opened"); |
| 64 | + screen.setUserMessage("Selected AID OK"); |
| 65 | + // Send the APDU and wait for a response - expect an error here unless your applet supports the command that was sent |
| 66 | + Utilities.log("XXXX " + Thread.currentThread().getName() + " ISO 7816-4 command: sending APDU"); |
| 67 | + byte[] ret = apduConn.exchangeAPDU(command); |
| 68 | + String resp_string = ByteArrayUtilities.byteArrayToHex(ret); |
| 69 | + Utilities.log("XXXX " + Thread.currentThread().getName() + " ISO 7816-4 command: response=" + resp_string); |
| 70 | + screen.setUserMessage("APDU response=" + resp_string); |
| 71 | + // Close the logical channel connection |
| 72 | + apduConn.close(); |
| 73 | + } catch(Exception e) { |
| 74 | + Utilities.log("XXXX " + Thread.currentThread().getName() + " ISO 7816-4 command failed:" + e.getClass().getName() + ":" + e.getMessage()); |
| 75 | + screen.setUserMessage("Error. Select failed: check log"); |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | +} |
0 commit comments