を受け取っていません。ログはすべて出力されますが、正常に動作していません。助けてください。カードエミュレーションは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を取得するにはどうすればよいですか?