Communicating between a phone & Java Card emulator
This is just the building blocks to running your own applications, and here I will be using code free on the internet and with the samples included with the Java Card installation.
I will also be using the setup used as shown in this post here.
So to start off with we want to create a new Project in Netbeans and select ‘Java ME’ and ‘Mobile Application’ from the right hand side.
From here make sure the ‘Set as Main Project’ box is ticked and the ‘Create Hello MIDlet’ box is unticked. And that the Emulator platform is Sun Java Wireless Toolkit. It’s also best to make sure the Device Configuration is CLDC-1.1 and the Device Profile is MIDP-2.1. Though 2.0 should do fine as well.
Now you need to create a new MIDlet
Now call this APDUMIDlet and copy in the APDUMIDlet class from here.
Now you can either follow the instructions at the top of that website, or you can follow on to find out how to compile and run your own Java Card program.
First you need to change the package name at the top to whatever yours is called. Then go down to the first initialised variable, kPurseSlot0.
private final String kPurseSlot0 = "apdu:0;target=a0.00.00.00.62.03.01.0c.02.01";
The apdu slot can stay the same at 0 (which refers to port 9025 from last post), but for ease we will change the target to
target=01.02.03.04.05.06.07.08.09.00.01
Then you want to comment out the next 2 lines so that they look like
//private final String kPurseSlot1 = "apdu:1;target=a0.00.00.00.62.03.01.0c.02.01"; //private final String kWalletSlot0 = "apdu:0;target=a0.00.00.00.62.03.01.0c.06.01";
And also the lines referring to these further down should be commented out
/*
setProgress("Opening purse slot 1");
mPurseConnection1 = (APDUConnection)Connector.open(kPurseSlot1);
setProgress("Opening wallet slot 0");
mWalletConnection = (APDUConnection)Connector.open(kWalletSlot0);
*/
And then again comment out the code that has these Connections doing anything
/*setProgress("Exchanging APDUs with wallet");
for (int i = 0; i < kWalletAPDUs.length; i++) {
byte[] apdu = kWalletAPDUs[i];
byte[] response = mWalletConnection.exchangeAPDU(apdu);
// Process response.
}
setProgress("Closing wallet");
mWalletConnection.close();
setProgress("Exchanging APDUs with purse slot 1");
for (int i = 0; i < kPurseAPDUs.length; i++) {
byte[] apdu = kPurseAPDUs[i];
byte[] response = mPurseConnection1.exchangeAPDU(apdu);
// Process response.
}
setProgress("Closing purse slot 1");
mPurseConnection1.close();
And again the 2 try statements for closing the Connections need to be commented out
//try { mPurseConnection1.close(); } catch (Throwable t) {}
//try { mWalletConnection.close(); } catch (Throwable t) {}
Now the code is fine for our purposes. As there is only connections on the one slot and we are simply trying to have programs where the programs communicate.
We are going to be using HelloWorld program that simply takes in bytes and returns them.
This can be found within the Java Card installation directories. All you need to do is open this in your preferred editor and change the package name to the same as your Java ME package name. Then save this to a new directory, for instance:
%USER%\newProg\
(%USER% being your home directory. It is often a system variable in Windows, %USERPROFILE% also used)
How to compile and run in a lot of detail is explained here. But for everyone else who doesn’t want too much detail, here is the much shorter version (comments :
So your HelloWorld.java program is in this file (above) and now you need to create a new folder in here with the same package name you have. For example using the package name ‘prototype’ you would do
mkdir prototype
Now you need to compile the program like this
javac -g -d prototype -classpath “.;%JC_HOME%\lib\api.jar” HelloWorld.java
using the package prototype and program file HelloWorld.java. And now you need to convert the Java Card applet into a .cap file by doing
converter -applet 0×01:0×02:0×03:0×04:0×05:0×06:0×07:0×08:0×09:0×00:0×00 prototype.HelloWorld -classdir prototype -exportpath %JC_HOME%\api_export_files prototype 0×01:0×02:0×03:0×04:0×05:0×06:0×07:0×08:0×09:0×00 1.0
This has the unique ID for this applet (which is what the Java ME program connects to) which is why we changed target to 01.02.03.04.05.06.06.08.09.00.01 earlier.
And you have to put the -classdir parameter of where the .class file is (inside the prototype folder we created earlier). Then we use the say the export path (always the same) and state the package name, the package unique ID (I’ve just used the same as the Applet ID except leaving off the final byte 0×01). And the Major Version.Minor Version (if you don’t understand just put 1.0 every time).
Now the scripts have to be generated from the .cap file which is what will go onto the Java card.
scriptgen -o HelloWorld.scr prototype\prototype\javacard\protoytpe.cap
if you look at your file system, it creates directories in a strange way so instead of your .cap file just being in prototype/javacard/prototype.cap it will have created another inside the folder you already created. But if you don’t create the folder before you cannot create the .cap file (thus the hacky process of creating a folder)
Now you will have a script called HelloWorld.scr. Open this in your preferred editor and you need to make some changes for this. First of all add a line to the top of the script and just put in:
powerup;
Now you need to add in a line of bytes which will always be the same:
0×00 0xA4 0×04 0×00 0×09 0xa0 0×00 0×00 0×00 0×62 0×03 0×01 0×08 0×01 0x7F;
Now you need to add lines which will create and select the Applet (I often put in comments to remind me):
// create Applet
0×80 0xB8 0×00 0×00 0x0d 0x0b 0×01 0×02 0×03 0×04 0×05 0×6 0×07 0×08 0×09 0×00 0×01 0×00 0x7F;
// Select Applet
0×00 0xa4 0×04 0×00 0x0b 0×01 0×02 0×03 0×04 0×05 0×6 0×07 0×08 0×09 0×00 0×01 0x7F;
Now you can see the 2 part in italic are exactly the same. This is also the Applet ID (AID). The 0x0b is the length of the AID in hexadecimal. And in the create applet command it also has an extra 0×00 after the AID and before 0x7F. This means the length (before the AID in the create applet command) there is another byte 0x0d which is length here. Then you simply need to add at the end:
powerdown;
And open up a terminal window and type
start cref -o HelloWorld.eeprom
which will open a new terminal window and run the command, then in the first window type
apdutool HelloWorld.scr
and this will show a big print out. This is the debugging of everything, and all you have to look at is the last 2 returned bytes of each line (looking at the last 4 lines will suffice)
if SW1: 90 and SW2: 00 then it is fine, and has gone onto the card correctly and is ready to be run. Type
start cref -e -p 9025 -i HelloWorld.eeprom
And leave this running. Now go back to netbeans and run the program. Click through the emulator and it will work.
You can also look at the network monitor to see the bytes being sent back and forth.
The phone emulator screen should say something like this:
And you can see the bytes sent in the APDU tab of the network monitor:
As you can see it has the right target and it’s first establishing a connection to the card. And the different bytes being sent back and forth, where the arrow going left to right is phone to smart card and vice versa.
This is a good way to find out errors occurring in your own programs.
If you have any problems or another question, feel free to comment.



