2

I want to establish a communication between activity and HostApduService. I didn't success to have a bi-directional communication between the both, the OnBind of HostApduService is declared final, are we just limited to use notifications for the communication between them. To date, I just success to establish a communication from HostApduService to Activity.

My Activity class

public class MainActivity extends Activity {

 private static final String TAG = MainActivity.class.getSimpleName();

 private BroadcastReceiver receiver = new BroadcastReceiver() {

     @Override
     public void onReceive(Context context, Intent intent) {
         Bundle bundle = intent.getExtras();
         if (bundle != null) {
             boolean transactionFinished = bundle.getBoolean(HosService.COMMUNICATION_FINISHED);
             String hceServiceMessage = bundle.getString(HostService.APDU_LOG);

             Toast.makeText(MainActivity.this,
                     transactionFinished ? "HCE Transaction accomplished!" : "HCE Transaction in progress!",
                     Toast.LENGTH_LONG).show();

             TextView t = (TextView) MainActivity.this.findViewById(R.id.textView2);
             t.setText(hceServiceMessage);
             t.refreshDrawableState();
       }
     }

    };

     @Override
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.activity_main);

     }

     @Override
     protected void onResume() {
         super.onResume();
         registerReceiver(receiver, new IntentFilter(HostService.NOTIFICATION));
     }
     @Override
     protected void onPause() {
         super.onPause();
         unregisterReceiver(receiver);
     }

     @Override
     public boolean onCreateOptionsMenu(Menu menu) {
         // Inflate the menu; this adds items to the action bar if it is present.
 //        getMenuInflater().inflate(R.menu.main, menu);
         return true;
     }

     @Override
     public boolean onOptionsItemSelected(MenuItem item) {
         // Handle action bar item clicks here. The action bar will
         // automatically handle clicks on the Home/Up button, so long
         // as you specify a parent activity in AndroidManifest.xml.
         int id = item.getItemId();

         return super.onOptionsItemSelected(item);
     }

And my service class:

public class HostService extends HostApduService {

private static final String TAG = HostService.class.getSimpleName();
private static boolean secureSessionInProgress = false;
private static List<String> secureSessionApdu = new ArrayList<String>();
private static String transactionLog = "";
public static final String NOTIFICATION = "net.hce";
public static final String COMMUNICATION_FINISHED = "finished";
public static final String APDU_LOG = "apdu";

public HostService() {
}

@Override
public byte[] processCommandApdu(byte[] cmd_bytes, Bundle bundle) {
    byte[] resp_bytes = null;

   // ***********************************
   // Here I want to exchange data with activity
   // Intent intent = new Intent(NOTIFICATION);
   // intent.putExtra (APDU_LOG, finishedTransactionLog);
   // intent.putExtra(COMMUNICATION_FINISHED, communicationFinished);
   // sendBroadcast(intent);
   // ...
   // How to get info from activity??
   // ************************************ 
   transactionLog += "> ";return resp_bytes;
}

@Override
public void onDeactivated(int reason) {
    secureSessionInProgress = false;
    Log.d(TAG, "deactivated. reason=" + reason);

    transactionLog = "";
}

}

phuclv
  • 37,963
  • 15
  • 156
  • 475

0 Answers0