最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

nfc - HostApduService on Android 16 - Stack Overflow

programmeradmin0浏览0评论

This is a small example for share here from a larger project where the use of HostApduService is necessary. I want to verify that my app works correctly with Android 16, but I’m encountering issues.

  • ApduService
class MyHostApduService: HostApduService() {
    init {
        println("MyHostApduService ---> $this")
    }

    override fun processCommandApdu(bytes: ByteArray?, bundle: Bundle?): ByteArray {
        println("MyHostApduService processCommandApdu ${bytes?.toHexString()}, $bundle")
        return byteArrayOf(0xA5.toByte())
    }

    override fun onDeactivated(reason: Int) {
        println("MyHostApduService onDeactivated $reason")
    }
}

fun ByteArray.toHexString(): String {
    return joinToString("") { "%02X".format(it) }
}
  • AndroidManifest.xml
<service
    android:name=".MyHostApduService"
    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
<?xml version="1.0" encoding="utf-8"?>
<host-apdu-service xmlns:android=";
    android:description="@string/app_name">

    <aid-group android:description="@string/app_name" android:category="other">
        <aid-filter android:name="A0ABC001234554321BBCC2211AAA0555" />
    </aid-group>
</host-apdu-service>


On Android 9, 12, 14, and 15, every time an NFC reader is near the phone, a new service instance starts, and it always works as expected.

// First attempt
MyHostApduService ---> com.example.android.MyHostApduService@a4638dc
MyHostApduService processCommandApdu 00A4040010A0ABC001234554321BBCC2211AAA0555, null
MyHostApduService onDeactivated 0
// Second attempt
MyHostApduService ---> com.example.android.MyHostApduService@c15c0ba
MyHostApduService processCommandApdu 00A4040010A0ABC001234554321BBCC2211AAA0555, null
MyHostApduService onDeactivated 0
// Third attempt
MyHostApduService ---> com.example.android.MyHostApduService@86c06c8
MyHostApduService processCommandApdu 00A4040010A0ABC001234554321BBCC2211AAA0555, null
MyHostApduService onDeactivated 0

However, on Android 16 Beta 2, the instance remains the same and runs as soon as the app is launched, even without an NFC reader nearby. When a reader is brought close, it works fine the first time, but doesn’t work on subsequent attempts.

MyHostApduService ---> com.example.android.MyHostApduService@ddcb1b
// First attempt
MyHostApduService processCommandApdu 00A4040010A0ABC001234554321BBCC2211AAA0555, null
MyHostApduService onDeactivated 0
// Second attempt
MyHostApduService processCommandApdu 00A4040010A0ABC001234554321BBCC2211AAA0555, null
MyHostApduService onDeactivated 0
// Third attempt
MyHostApduService processCommandApdu 00A4040010A0ABC001234554321BBCC2211AAA0555, null
MyHostApduService onDeactivated 0

I’ve checked the Android 16 documentation, but I don’t see any changes to HostApduService that would explain this behavior.

Am I doing something wrong?

发布评论

评论列表(0)

  1. 暂无评论