微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何发送SET_UP_MENU命令

如何解决如何发送SET_UP_MENU命令

我开发了自己的STK小程序并将其安装在SIM卡中。 但是,当我将SIM卡插入智能手机时,SET_UP_MENU主动命令不会发送到终端。 我的小程序代码如下。

package com.example.ToolkitAppletExample;

import uicc.toolkit.*;
import uicc.access.*;
import javacard.framework.*;

public class MyToolkitApplet extends javacard.framework.Applet implements ToolkitInterface,uicc.toolkit.ToolkitConstants
{
    public static final byte MY_INSTRUCTION         = (byte)0x46;
    public static final byte SERVER_OPERATION       = (byte)0x0F;
    public static final byte CMD_QUALIFIER          = (byte)0x80;
    public static final byte EXIT_REQUESTED_BY_USER = (byte)0x10;

    private byte[] menuEntry =    {(byte)'0',(byte)'3',(byte)'.',(byte)'1',(byte)'9',(byte)' ',(byte)'A',(byte)'p',(byte)'l',(byte)'e',(byte)'t'};
    
    private byte[] menuTitle=     {(byte)'M',(byte)'y',(byte)'M',(byte)'n',(byte)'u'};
    private byte[] item1 =        {(byte)'I',(byte)'T',(byte)'E',(byte)'1' };
    private byte[] item2 =        {(byte)'I',(byte)'2' };
    private byte[] item3 =        {(byte)'I',(byte)'3' };
    private byte[] item4 =        {(byte)'I',(byte)'4' };
    private Object[] ItemList =   { item1,item2,item3,item4 };
    private byte[] textDText =    {(byte)'H',(byte)'o',(byte)'w',(byte)'r',(byte)'d',(byte)'2'};
    private byte[] textGInput =    {(byte)'Y',(byte)'u',(byte)'a',(byte)'m',(byte)'?'};

    private byte[]  baGSMAID =    {(byte)0xA0,(byte)0x00,(byte)0x09,(byte)0x01};
    private ToolkitRegistry reg;
    private byte buffer[] = new byte[10];
    private byte itemId;
    private byte result;


    // Allocate 500 bytes for the reception buffer
    private static final short BUFFER_SIZE = (short)0x01F4;    
    // TLVs overhead in the Terminal Response in case of Receice Data: 
    //      Command details size: 5 bytes
    //      Device identities size : 4 bytes
    //      Result size : 3 bytes (in success case)
    //      Channel data length size : 3 bytes
    //      Channel data TL size : 3 bytes
    private static final short MAX_TLV_OVERHEAD = (short)0x0012;
    // Maximum response handler capacity to be used for the receive data 
    private static short myRspHdlrCapacity;
    
    // Port number : integer
    private static byte[] portNumber = {(byte)0x00,(byte)0x23};
    // Channel Identifier given by the ME after the OPEN CHANNEL proactive command
    private static byte channelID = 0x00;
    // Byte array where data from the ME buffer is stored
    private static byte[] channelData = new byte [BUFFER_SIZE];
    // IP Address of the Server 
    private static byte[] IPAddress = {(byte)163,(byte)187,(byte)203,(byte)1};

    /**
    * Constructor of the applet
    */
    public MyToolkitApplet(byte bArray[],short bOffset,byte bLength) {
        
        register();
        
        // register to the SIM Toolkit Framework
        reg = ToolkitRegistrySystem.getEntry();

        // Define the applet Menu Entry and register to the EVENT_MENU_SELECTION
        itemId = reg.initMenuEntry(menuEntry,(short)0x0000,(short)menuEntry.length,PRO_CMD_disPLAY_TEXT,false,(byte) 0x00,(short) 0x0000);
        
        reg.enableMenuEntry((byte)itemId);
    }

    /**
    * Method called by the JCRE at the installation of the applet
    */
    public static void install(byte bArray[],byte bLength) {
        MyToolkitApplet MyApplet = new MyToolkitApplet(bArray,bOffset,bLength);       
    }

    /**
    * Method called by the GSM Framework
    */
    public Shareable getShareableInterfaceObject(AID aid,byte p) {
         if (aid == null && p == (byte)1) {
            return this ; 
         }
         return null;
    }

    /**
    * Method called by the SIM Toolkit Framework
    */
    public void processtoolkit(short event) {

        // get the handler references
        EnvelopeHandler          envHdlr = EnvelopeHandlerSystem.getTheHandler();
        ProactiveHandler         proHdlr = ProactiveHandlerSystem.getTheHandler();
        ProactiveResponseHandler rspHdlr = ProactiveResponseHandlerSystem.getTheHandler();;
        boolean repeat;

        switch(event) {
            case EVENT_STATUS_COMMAND:
                reg.enableMenuEntry((byte)itemId);  

            
            case EVENT_PROFILE_DOWNLOAD:
                reg.enableMenuEntry((byte)itemId);               
            break;
            case EVENT_MENU_SELECTION:
                // Prepare the Select Item proactive command
                proHdlr.init(PRO_CMD_SELECT_ITEM,DEV_ID_TERMINAL);
                // Append the Menu Title
                proHdlr.appendTLV((byte) (TAG_ALPHA_IDENTIFIER | TAG_SET_CR),menuTitle,(short)menuTitle.length);
                // add all the Item
                for (short i=(short) 0x0000; i<(short) 0x0004; i++) {
                    proHdlr.appendTLV((byte) (TAG_ITEM | TAG_SET_CR),(byte) (i+1),(byte[])ItemList[i],(short) 0x0000,(short)((byte[])ItemList[i]).length);
                }
                // ask the SIM Toolkit Framework to send the proactive command and check the result
                if((result = proHdlr.send()) == RES_CMD_PERF){

                    // SelectItem response handling
                    switch (rspHdlr.getItemIdentifier()) {
                        case 1: 
                            // displayText High Priority 
                            proHdlr.init(PRO_CMD_disPLAY_TEXT,CMD_QUALIFIER,DEV_ID_disPLAY);
                            proHdlr.appendTLV((byte)(TAG_TEXT_STRING| TAG_SET_CR),(byte)0x04,textDText,(short)textDText.length);
                            proHdlr.send();
                        break;

                        case 2: // Ask the user to enter data and display it
                            do {
                                repeat = false;
                                try {
                                    // GetInput asking the users name
                                    proHdlr.initGetInput((byte)0x01,textGInput,(short)textGInput.length,(short)0x0001,(short)0x0002);
                                    proHdlr.send();
                                    // display the entered text
                                    rspHdlr.copyTextString(textDText,(short)0x0000);
                                    proHdlr.initdisplayText((byte)0x00,(short) textDText.length);
                                    proHdlr.send();
                                }
                                catch (ToolkitException MyException) {
                                    if (MyException.getReason() == ToolkitException.UNAVAILABLE_ELEMENT ){
                                        if (rspHdlr.getGeneralResult() != EXIT_REQUESTED_BY_USER)
                                            repeat = true;
                                        break;
                                    }
                                }
                            }
                            while(repeat);
                        break;
                    }
                }
            break;
            
        }
    }

    /**
    * Method called by the JCRE,once selected
    */
    public void process(APDU apdu) {
        // Handle the Select AID apdu
        if(selectingApplet())
            return;

        switch(apdu.getBuffer()[1]) {
            // specific APDU for this applet to configure the MenuTitle from SelectItem
            case (byte)MY_INSTRUCTION:
                if(apdu.setIncomingAndReceive() > (short)0) {
                    Util.arraycopy(apdu.getBuffer(),(short)0x0005,(short)0x0006);
                }
            break;

            default:
                ISOException.throwIt(ISO7816.SW_INS_NOT_SUPPORTED);
        }
    }
}

据我所知,方法initMenuEntry和enableMenuEntry触发发送SET_UP_MENU主动命令。 但是,当我插入SIM卡时,没有SET_UP_MENU命令。 将SIM卡插入手机后,还有其他人如何发送SET_UP_MENU主动命令吗? 请帮助我...

解决方法

  1. 为什么要在initMenuEntry中使用PRO_CMD_DISPLAY_TEXT?正确的方法是

    itemId = reg.initMenuEntry(menuEntry,(short)0x0000,(short)menuEntry.length, (字节)0 ,假,(字节)0x00,(短)0x0000);

  2. 您无需在此处和此处使用enableMenuEntry。 initMenuEntry就足够了。

  3. 由于第2页,它已过时,但如果要使用它,则必须订阅EVENT_PROFILE_DOWNLOAD(reg.setEvent(EVENT_PROFILE_DOWNLOAD);)。并将轮询间隔设置为30秒(reg.requestPollInterval((short)30);),以确保EVENT_STATUS_COMMAND可用。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。