nfc - HostApduService on Android 16 - Stack Overflow

This is a small example for share here from a larger project where the use of HostApduService is necess

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?

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745045002a4608042.html

相关推荐

  • nfc - HostApduService on Android 16 - Stack Overflow

    This is a small example for share here from a larger project where the use of HostApduService is necess

    8小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信