我正在开发一个Android应用程序,该应用程序必须通过NFC与自定义设备进行交互。设备扮演读取器角色时,Android应用程序充当仿真卡(HCE)。
当手机锁定(屏幕显示)时,一切正常,但是当手机解锁时,HostApduService不会被调用。
AndroidManifest.xml
...
<uses-permission android:name="android.permission.NFC" />
<uses-feature
android:name="android.hardware.nfc.hce"
android:required="false" />
...
<service
android:name=".communication.nfc.NfcService"
android:exported="true"
android:permission="android.permission.BIND_NFC_SERVICE">
<intent-filter>
<action android:name="android.nfc.cardemulation.action.HOST_APDU_SERVICE" />
</intent-filter>
<meta-data
android:name="android.nfc.cardemulation.host_apdu_service"
android:resource="@xml/apduservice" />
</service>
apduservice.xml
<host-apdu-service xmlns:android="http://schemas.android.com/apk/res/android"
android:description="@string/nfcServiceDescription"
android:requireDeviceUnlock="false">
<aid-group
android:category="other"
android:description="@string/nfcServiceApertura">
<aid-filter android:name="F70FC6AAAAAAA" />
</aid-group>
</host-apdu-service>
服务
class NfcService : HostApduService() {
override fun processCommandApdu(commandApdu: ByteArray, extras: Bundle?): ByteArray {
val text = "processCommandApdu: " + commandApdu.asHexString()
log(text)
}
override fun onDeactivated(reason: Int) {
val text = "onDeactivated: $reason"
log(text)
}
private fun log(mensaje: String) {
Log.i(TAG, mensaje)
}
companion object {
const val TAG = "APDU"
}
}
锁定并打开手机屏幕时会呼叫 NfcService.processCommandApdu
,但解锁手机时不会呼叫
view1.rightAnchor.constraint(equalTo: view2.leftAnchor).isActive = true
。我希望它在两种状态(锁定和未锁定)下都能正常工作