2

我想在活动和HostApduService. 我没有成功地在两者之间进行双向通信,声明了OnBindHostApduService我们final只是限于使用通知来进行它们之间的通信。迄今为止,我刚刚成功建立了从 HostApduService 到 Activity 的通信。

我的活动课

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);
     }

我的服务类:

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 = "";
}

}

4

0 回答 0