打印出的所有日志都很好,但事情不起作用。请帮忙。
的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:true
(nfc!= null):true
(comp!= null):true
emulationRetVal:true
由于emulationRetVal
为true
,因此ApduService应该处于活动状态并与CardEmulation
事物绑定。但是,它不起作用。
我该怎样做才能让HCE继续前进?
答案 0 :(得分:0)
您没有显示apduservice.xml
,即发生AID注册的文件。或者,您应该从应用程序中手动呼叫CardEmulation::registerAidsForService
。
确保执行https://developer.android.com/guide/topics/connectivity/nfc/hce.html
中列出的所有步骤