如何通过活动与HostApduService通信 [英] How to communicate with HostApduService from an Activity

查看:103
本文介绍了如何通过活动与HostApduService通信的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经问过这个问题

I have asked this question here but it was marked as duplicate - however I didn't find any solution helpful mentioned in comments. Here, I am asking again with more details ...

我正在 HCE上制作示例应用程序(PoC),并按照Android用户指南使用 HostApduService .我已经创建了两个应用程序
1)ReaderApp-充当读卡器 2)HCEApp-模拟卡

I am doing a sample app (PoC) on HCE and using HostApduService as per Android user guide. I have created two apps
1) ReaderApp - acting as card reader 2) HCEApp - emulating a card

在HCEApp中,我创建了一个扩展HostApduService的类"MyService"

In HCEApp, I have created a class 'MyService' extending HostApduService

public class MyService extends HostApduService {

private int messageCounter;
private final String TAG = "MyService";

Intent mIntent;

@Override
public void onCreate() {
    super.onCreate();
    Log.i(TAG, "onCreate");

    mIntent = new Intent(this, MyActivity.class);
    mIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(mIntent);
}

/**
 * returned bytes will be sent as response. This method runs in Main thread
 * so return ASAP.
 */

@Override
public byte[] processCommandApdu(byte[] apdu, Bundle extras) {
    if (selectAidApdu(apdu)) {
        Log.i(TAG, "Application selected");
        return getWelcomeMessage();
    } else {
        Log.i(TAG, "Received: " + new String(apdu));
        return getNextMessage();
    }
}

private byte[] getWelcomeMessage() {
    return "Hello Desktop!".getBytes();
}

private byte[] getNextMessage() {
    return ("Message from android: " + messageCounter++).getBytes();
}

private boolean selectAidApdu(byte[] apdu) {

    if (apdu != null) {
        for (byte b : apdu) {
            System.out.printf("0x%02X", b);
        }
    }

    return apdu.length >= 2 && apdu[0] == (byte) 0
            && apdu[1] == (byte) 0xa4;
}

@Override
public void onDeactivated(int reason) {
    Log.i(TAG, "Deactivated: " + reason);
}

@Override
public boolean onUnbind(Intent intent) {
    return super.onUnbind(intent);
}

}

正如您在 onCreate()中看到的那样,我正在启动 MyActivity ,它为用户提供了一些输入信息,需要将其发送回 MyService >.

As you can see in onCreate(), I am launching MyActivity provides user to enter some information and needs to be sent back to MyService.

我认为我不能使用绑定,因为在主机ApduService中声明了 onBind(),如下所示

I think I can not use binding as 'onBind()' is declared final in HostApduService as below

@Override
public final IBinder onBind(Intent intent) {
    return mMessenger.getBinder();
}

请让我知道我是否正确理解了它.感谢任何帮助.

Please let me know if I am understading it correctly. Appreciate any help.

谢谢
iuq

Thanks
iuq

推荐答案

我不知道是否可以使用onBind,但是最近我与BroadcastReceiver一起使用,必须从该服务启动服务.根据文档,您不能从广播接收器中bind服务,您只能start.稍后我需要从我的BroadcastReceiver向服务发送一些数据,并且由于绑定器技术对我不可用,因此我不得不寻找一种与服务进行通信的不同方式,就像您不使用该服务的情况一样有一个参考.

Whether you can use onBind or not I do not know, but I recently worked with a BroadcastReceiver from which I had to start a Service. You cannot bind a Service from a BroadcastReceiver according to docs, you can only start it. I needed to send some data to the Service from my BroadcastReceiver at some later point, and since the binder techniques was not available to me, I had to find a different way to communicate with the Service, much like your case where you don't have a reference to it.

我做了一些研究,但是找不到任何解决方案,但是后来我想起可以通过startService(intent)调用传递意图数据.我改用onCreate开始我的Service工作,因为onCreate在创建Service时仅被调用一次.

I did some research but could not find any solution, but then I remembered that you can pass intent data with the startService(intent) call. I start my Service work in onCreate instead, as onCreate is only called once when the Service is created.

在您的活动中

public void sendDataToService(){
    Intent intent = new Intent(context, MyService.class);
    intent.putExtra("message", SOME_DATA);
    context.startService(intent);
}

为您服务

@Override
public int onStartCommand(Intent intent, int flags, int startId) {

    // Check if intent has extras
    if(intent.getExtras() != null){

        // Get message
        int message = intent.getExtras().getInt("message");
    }

    return START_NOT_STICKY;
}

这可能有点像hack,因为"startService"听起来好像不应该用于发送消息,并且不确定这是否正是您所需要的,但是它对我有用,所以我希望它为您工作.干杯

This may be some sort what of a hack since "startService" does not sound like it should be used to send messages, and am not sure if this is exactly what you need, but it worked for me, so I hope it works for you. Cheers

顺便说一句.我用它来告诉LocationService特定活动不再需要位置更新.

BTW. I use it to tell a LocationService that a particular activity no longer want location updates.

这篇关于如何通过活动与HostApduService通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆