简体   繁体   English

侦听 HCE(基于主机的卡模拟)事件

[英]Listen for HCE (Host-based Card Emulation) event

I have the need to launch my app every time the user begins an NFC transaction using Host-based Card Emulation (HCE).每次用户使用基于主机的卡模拟 (HCE) 开始 NFC 交易时,我都需要启动我的应用程序。

I don't need to manage the interaction.我不需要管理交互。 I don't need any kind of data from the reader or the NFC emulator.我不需要来自阅读器或 NFC 模拟器的任何类型的数据。 I have no control on the apps that use the service.我无法控制使用该服务的应用程序。 I just need to know if the phone came close to an NFC reader.我只需要知道手机是否靠近 NFC 阅读器。

Is there an easy way like listen for a generic system event or notification without interfering with other apps?有没有一种简单的方法,比如在不干扰其他应用程序的情况下侦听通用系统事件或通知?

No, Android does not send any notification like "hey, there is some app that has just been activated over HCE".不,Android 不会发送任何通知,例如“嘿,有一些应用程序刚刚通过 HCE 激活”。 Thus, you can't get such an event in your app.因此,您无法在您的应用程序中获得这样的事件。 And specifically, it's not possibly to monitor if any existing HCE app (that's not under your control) on your device has been activated through HCE.具体而言,不可能监控您设备上的任何现有 HCE 应用程序(不在您的控制之下)是否已通过 HCE 激活。

What you can do is to create your own HCE service (registered for your specific application AIDs).您可以做的是创建您自己的 HCE 服务(为您的特定应用程序 AID 注册)。 This HCE service could then launch an activity if it receives a transaction (also see How can I send message from HostApduService to an activity? ):如果此 HCE 服务收到交易,则它可以启动活动(另请参阅如何从 HostApduService 向活动发送消息? ):

public class MyHostApduService extends HostApduService {
    @Override
    public byte[] processCommandApdu(byte[] apdu, Bundle extras) {
        if ((apdu[1] == (byte)0xA4) && (apdu[2] == (byte)0x04)) {
            // SELECT by AID
            Intent intent = new Intent(this, MyActivity.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(intent);

            return new byte[]{ (byte)0x90, (byte)0x00 }
        } else {
            return new byte[]{ (byte)0x6D, (byte)0x00 }
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM