How to send a message from HostApduService to action?

I would like to pass a string to an Activity easily. Something like a callback would be necessary, because when a line needs to be passed, Activity has to do something.

public class MyHostApduService extends HostApduService { @Override public byte[] processCommandApdu(byte[] apdu, Bundle extras) { if (selectAidApdu(apdu)) { Log.i("HCEDEMO", "Application selected"); return getWelcomeMessage(); } else { if (exchangeDataApdu(apdu)) { Log.i("HCEDEMO", "Data exchanged"); // int _len_ = apdu[4]; String _data_ = new String(apdu).substring(5, apdu.length - 1); // TODO: Send _data_ to an activity... return new byte[] { (byte)0x90, (byte)0x00 }; } else { Log.i("HCEDEMO", "Received: " + new String(apdu)); return getNextMessage(); } } } } 
+3
source share
2 answers

Your question is very broad, and the answer depends on some aspects of the design of your application that you did not disclose to us:

You want to get started and pass some data to it

You can start working with the intent and pass the parameter as optional (see this answer ):

 Intent intent = new Intent(this, YourActivity.class); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); intent.putExtra("hcedata", _data_) this.startActivity(intent); 

You want to transfer data to a running instance of your activity

In this case, you can, for example, register BroadcastReceiver from your activity:

 public class YourActivity extends Activity { final BroadcastReceiver hceNotificationsReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { if () { String hcedata = intent.getStringExtra("hcedata"); // TODO: do something with the received data } } }); @Override protected void onStart() { super.onStart(); final IntentFilter hceNotificationsFilter = new IntentFilter(); hceNotificationsFilter.addAction("your.hce.app.action.NOTIFY_HCE_DATA"); registerReceiver(hceNotificationsReceiver, hceNotificationsFilter); } @Override protected void onStop() { super.onStop(); unregisterReceiver(hceNotificationsReceiver); } 

You can send an intent to this broadcast receiver to your service (note that you may need to limit broadcast transmission with the permission of the recipient to prevent data leakage for unauthorized receivers):

 Intent intent = new Intent("your.hce.app.action.NOTIFY_HCE_DATA"); intent.putExtra("hcedata", _data_) this.sendBroadcast(intent); 

You want to transfer data to action and get a response back to your service

As above, you can use the broadcast receiver in your service to receive a notification from your activity. Please note that you cannot bind to your HCE service, so you cannot directly refer to methods from it.

+5
source

You must use the Intent mechanism.

If you need to start an action, add to your service:

 Intent intent = new Intent(context, MyActivity.class); intent.putExtra("extra_key", "values you want to pass"); startActivity(intent); 

and in action:

 String value = getIntent().getStringExtra("extra_key"); 
0
source

All Articles