6

我是 UICC 和 Secure Elements 的新手,我尝试使用本教程 (http://code.google.com/p/seek-for- android/wiki/UsingSmartCardAPI) 来连接 Secure Elements 来做一个简单的 android 应用程序。当我运行应用程序时,它会抛出 java.lang.SecurityException: Connection denied !!!。请帮忙。谢谢

代码

public void onCreate(Bundle savedInstanceState) { 
          final String LOG_TAG = "HelloSmartcard";     
          super.onCreate(savedInstanceState);     
          LinearLayout layout = new LinearLayout(this); 
          layout.setLayoutParams(new LayoutParams( 
                  LayoutParams.WRAP_CONTENT, 
                  LayoutParams.WRAP_CONTENT)); 

          Button button = new Button(this); 
          button.setLayoutParams(new LayoutParams( 
                  LayoutParams.WRAP_CONTENT, 
                  LayoutParams.WRAP_CONTENT));     
          button.setText("Click Me"); 
          button.setOnClickListener(new OnClickListener() { 
            public void onClick(View v) { 
                try { 
                      Log.d(LOG_TAG, "Retrieve available readers..."); 
                      Reader[] readers = seService.getReaders(); 
                      if (readers.length < 1) 
                         return;     
                      Log.d(LOG_TAG, "Create Session from the first reader..."); 
                      Session session = readers[0].openSession();     
                      Log.d(LOG_TAG, 
                      "Create logical channel within the session..."); 
                      Channel channel = session.openLogicalChannel(new 
byte[] { 
                      (byte) 0xD2, 0x76, 0x00, 0x01, 0x18, 0x00, 0x02, 
                      (byte) 0xFF, 0x49, 0x50, 0x25, (byte) 0x89, 
                      (byte) 0xC0, 0x01, (byte) 0x9B, 0x01 });     
                      //Log.d(LOG_TAG, "Send HelloWorld APDU command"); 
                      byte[] respApdu = channel.transmit(new byte[] { 
                      (byte) 0x90, 0x10, 0x00, 0x00, 0x00 });     
                      channel.close();     
                      // Parse response APDU and show text but remove SW1 SW2 first 
                      byte[] helloStr = new byte[respApdu.length - 2]; 
                      System.arraycopy(respApdu, 0, helloStr, 0, 
respApdu.length - 2); 
                      Toast.makeText(MainActivity.this, new 
String(helloStr),   Toast.LENGTH_LONG).show(); 
                   } catch (Exception e) { 
                      Log.e(LOG_TAG, "Error occured:", e); 
                      return; 
                   } 
            } 
          }); 

          layout.addView(button); 
          setContentView(layout); 
          try { 
                Log.i(LOG_TAG, "creating SEService object"); 
                seService = new SEService(this, this); 
              } catch (SecurityException e) { 
                Log.e(LOG_TAG, "Binding not allowed, uses-permission org.simalliance.openmobileapi.SMARTCARD?"); 
              } catch (Exception e) { 
                Log.e(LOG_TAG, "Exception: " + e.getMessage()); 
              } 

    } 

日志猫

05-22 08:11:49.669: E/HelloSmartcard(6691): Error occured:
05-22 08:11:49.669: E/HelloSmartcard(6691): java.lang.SecurityException: Connection refused !!!
05-22 08:11:49.669: E/HelloSmartcard(6691):     at org.simalliance.openmobileapi.SEService.checkForException(SEService.java:611)
05-22 08:11:49.669: E/HelloSmartcard(6691):     at org.simalliance.openmobileapi.SEService.openLogicalChannel(SEService.java:479)
05-22 08:11:49.669: E/HelloSmartcard(6691):     at org.simalliance.openmobileapi.Session.openLogicalChannel(Session.java:143)
05-22 08:11:49.669: E/HelloSmartcard(6691):     at com.gieseckedevrient.android.hellosmartcard.MainActivity$1.onClick(MainActivity.java:50)
05-22 08:11:49.669: E/HelloSmartcard(6691):     at android.view.View.performClick(View.java:2485)
05-22 08:11:49.669: E/HelloSmartcard(6691):     at android.view.View$PerformClick.run(View.java:9080)
05-22 08:11:49.669: E/HelloSmartcard(6691):     at android.os.Handler.handleCallback(Handler.java:587)
05-22 08:11:49.669: E/HelloSmartcard(6691):     at android.os.Handler.dispatchMessage(Handler.java:92)
05-22 08:11:49.669: E/HelloSmartcard(6691):     at android.os.Looper.loop(Looper.java:130)
05-22 08:11:49.669: E/HelloSmartcard(6691):     at android.app.ActivityThread.main(ActivityThread.java:3768)
05-22 08:11:49.669: E/HelloSmartcard(6691):     at java.lang.reflect.Method.invokeNative(Native Method)
05-22 08:11:49.669: E/HelloSmartcard(6691):     at java.lang.reflect.Method.invoke(Method.java:507)
05-22 08:11:49.669: E/HelloSmartcard(6691):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:878)
05-22 08:11:49.669: E/HelloSmartcard(6691):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:636)
05-22 08:11:49.669: E/HelloSmartcard(6691):     at dalvik.system.NativeStart.main(Native Method)

显现

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.gieseckedevrient.android.hellosmartcard"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="10" />
    <uses-permission android:name="org.simalliance.openmobileapi.SMARTCARD"/>
    <uses-permission android:name="android.permission.INTERNET" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
         <uses-library android:name="org.simalliance.openmobileapi" android:required="false" />
    </application>

</manifest>
4

1 回答 1

4

//在AndroidManifest.xml文件中定义权限

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="org.simalliance.openmobileapi.SMARTCARD"/>
于 2012-05-22T08:25:48.547 回答