2017-05-17 3 views
1

を受け取っていません。ログはすべて出力されますが、正常に動作していません。助けてください。カードエミュレーションはOKだと思われますが、バイト[

のAndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.abc.hcetest"> 

    <uses-permission android:name="android.permission.NFC" /> 

    <application 
     android:allowBackup="true" 
     android:icon="@mipmap/ic_launcher" 
     android:label="@string/app_name" 
     android:roundIcon="@mipmap/ic_launcher_round" 
     android:supportsRtl="true" 
     android:theme="@style/AppTheme"> 
     <activity android:name=".MainActivity"> 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 


     <service 
      android:name=".services.ApduService" 
      android:exported="true" 
      android:permission="android.permission.BIND_NFC_SERVICE"> 
      <intent-filter> 
       <action android:name="android.nfc.cardemulation.action.HOST_APDU_SERVICE" /> 

       <category android:name="android.intent.category.DEFAULT" /> 
      </intent-filter> 

      <meta-data 
       android:name="android.nfc.cardemulation.host_apdu_service" 
       android:resource="@xml/apduservice" /> 
     </service> 
    </application> 

</manifest> 

MainActivity

public class MainActivity extends AppCompatActivity { 
    private static final String LOGTAG = MainActivity.class.toString(); 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     Thread thread = new Thread(){ 
      public void run(){ 
       try{sleep(1000l);}catch(Exception e){} 
       setPreferredNFCService(true); 
      } 
     }; 
     thread.start(); 
    } 


    private void setPreferredNFCService(boolean isSet) { 
     Log.d(LOGTAG, "setPreferredNFCService isSet: " + isSet); 
     NfcAdapter nfc = NfcAdapter.getDefaultAdapter(this); 

     Log.d(LOGTAG, "(nfc != null): " + (nfc != null)); 

     if (nfc == null) 
      return; //HCEAppError.NFC_NOT_SUPPORT; 

     CardEmulation emulation = CardEmulation.getInstance(nfc); 
     if(emulation != null) { 
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { 
       try{ 
        if (isSet) { 
         ComponentName comp = new ComponentName(this, ApduService.class); 
         Log.d(LOGTAG, "(comp != null): " + (comp != null)); 
         final boolean emulationRetVal = emulation.setPreferredService(this, comp); 

         Log.d(LOGTAG, "emulationRetVal: " + emulationRetVal); 
        } else { 
         emulation.unsetPreferredService(this); 
        } 
       } 
       catch(Exception e){ 
        e.printStackTrace(); 
       } 
      } 
      else{ 
       Log.e(LOGTAG, "unsupported"); 
      } 
     } 
     else{ 
      Log.e(LOGTAG, "card emulation failed"); 
     } 
    } 
} 

ApduService

public class ApduService extends HostApduService { 
    final static String LOGTAG = ApduService.class.getName(); 

    @Override 
    public byte[] processCommandApdu(byte[] commandApdu, Bundle extras) { 
     if(commandApdu != null){ 
      Log.d(LOGTAG, "processCommandApdu " + commandApdu.length); 
     } 
     else{ 
      Log.d(LOGTAG, "processCommandApdu " + commandApdu); 
     } 
     return null; 
    } 

    private boolean canStartTrans() { 
     Log.d(LOGTAG, "in can start trans"); 
     return true; 
    } 

    @Override 
    public void onDeactivated(int reason) { 
     Log.d(LOGTAG, "onDeactivated: " + reason); 
    } 
} 

プリントアウトは、次のとおりです。

setPreferredNFCService ISSET:真

(NFC = nullを!):真

(COMP = nullを!):真

emulationRetVal:真

emulationRetValは、ApduService trueあるので、アクティブであり、CardEmulationものに結びついているはずです。ただし、動作しません。

HCEを取得するにはどうすればよいですか?

答えて

0

あなたはapduservice.xmlを表示していません。これはAID登録が行われるファイルです。または、アプリケーション内から手動でCardEmulation::registerAidsForServiceに電話する必要があります。

https://developer.android.com/guide/topics/connectivity/nfc/hce.html

に記載されているすべての手順を必ず実行してください。
関連する問題