android.nfc.cardemulation.CardEmulation Java Examples

The following examples show how to use android.nfc.cardemulation.CardEmulation. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: MainActivity.java    From SwipeYours with GNU General Public License v3.0 6 votes vote down vote up
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);
    activityLog = (TextView) findViewById(R.id.activity_log);

    CardEmulation cardEmulationManager = CardEmulation.getInstance(NfcAdapter.getDefaultAdapter(this));
    ComponentName paymentServiceComponent =
            new ComponentName(getApplicationContext(), PaymentService.class.getCanonicalName());

    if (!cardEmulationManager.isDefaultServiceForCategory(paymentServiceComponent, CardEmulation.CATEGORY_PAYMENT)) {
        Intent intent = new Intent(CardEmulation.ACTION_CHANGE_DEFAULT);
        intent.putExtra(CardEmulation.EXTRA_CATEGORY, CardEmulation.CATEGORY_PAYMENT);
        intent.putExtra(CardEmulation.EXTRA_SERVICE_COMPONENT, paymentServiceComponent);
        startActivityForResult(intent, 0);
        log(TAG, "onCreate: Requested Android to make SwipeYours the default payment app");
    } else {
        log(TAG, "onCreate: SwipeYours is the default NFC payment app");
    }
}
 
Example #2
Source File: HostPaycardActivity.java    From NFC-EMV-Reader with GNU General Public License v3.0 5 votes vote down vote up
@Override
protected void onResume() {
    super.onResume();
    LogUtil.d(TAG, "\"" + TAG + "\": Activity resume");

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        if (mCardEmulation != null && mCardEmulation.categoryAllowsForegroundPreference(CardEmulation.CATEGORY_PAYMENT)) {
            mCardEmulation.setPreferredService(this, new ComponentName(this, PaymentHostApduService.class));
        }
    }
}
 
Example #3
Source File: HostPaycardActivity.java    From NFC-EMV-Reader with GNU General Public License v3.0 5 votes vote down vote up
@Override
protected void onPause() {
    super.onPause();
    LogUtil.d(TAG, "\"" + TAG + "\": Activity pause");

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        if (mCardEmulation != null && mCardEmulation.categoryAllowsForegroundPreference(CardEmulation.CATEGORY_PAYMENT)) {
            mCardEmulation.unsetPreferredService(this);
        }
    }
}
 
Example #4
Source File: PayActivity.java    From HandstandPay with GNU General Public License v2.0 5 votes vote down vote up
@Override
protected void onCreate(Bundle savedInstanceState) {
    this.context = getApplicationContext();
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_pay);
    ButterKnife.bind(this);
    cardEmulation = CardEmulation.getInstance(NfcAdapter.getDefaultAdapter(getApplicationContext()));

}
 
Example #5
Source File: PayActivity.java    From HandstandPay with GNU General Public License v2.0 5 votes vote down vote up
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public void setAsPreferredHceService() {
    boolean allowsForeground = cardEmulation.categoryAllowsForegroundPreference(CardEmulation.CATEGORY_PAYMENT);
    if (allowsForeground) {
        ComponentName hceComponentName = new ComponentName(context, HandstandApduService.class);
        cardEmulation.setPreferredService(PayActivity.this, hceComponentName);
    }
}
 
Example #6
Source File: PayActivity.java    From HandstandPay with GNU General Public License v2.0 5 votes vote down vote up
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public void unsetAsPreferredHceService() {
    boolean allowsForeground = cardEmulation.categoryAllowsForegroundPreference(CardEmulation.CATEGORY_PAYMENT);
    if (allowsForeground) {
        ComponentName hceComponentName = new ComponentName(context, HandstandApduService.class);
        cardEmulation.unsetPreferredService(PayActivity.this);
    }
}
 
Example #7
Source File: DefaultPaymentAppUtil.java    From HandstandPay with GNU General Public License v2.0 5 votes vote down vote up
public static void ensureSetAsDefaultPaymentApp(Activity context) {
  NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(context);
  CardEmulation cardEmulation = CardEmulation.getInstance(nfcAdapter);
  ComponentName componentName = new ComponentName(context, HandstandApduService.class);
  boolean isDefault = cardEmulation.isDefaultServiceForCategory(componentName, CardEmulation.CATEGORY_PAYMENT);

  if (!isDefault) {
    Intent intent = new Intent(CardEmulation.ACTION_CHANGE_DEFAULT);
    intent.putExtra(CardEmulation.EXTRA_CATEGORY, CardEmulation.CATEGORY_PAYMENT);
    intent.putExtra(CardEmulation.EXTRA_SERVICE_COMPONENT, componentName);
    context.startActivityForResult(intent, REQUEST_CODE_DEFAULT_PAYMENT_APP);
  }
}
 
Example #8
Source File: EchoHostApduService.java    From external-nfc-api with Apache License 2.0 5 votes vote down vote up
@Override
public void onCreate() {
	super.onCreate();
	Log.d(TAG, "HCE service created");
	
	CardEmulation cardEmulation = CardEmulation.getInstance(NfcAdapter.getDefaultAdapter(this));
	
	boolean defaultService = cardEmulation.isDefaultServiceForAid(new ComponentName(this, EchoHostApduService.class), AID);
	
	if(!defaultService) {
		throw new IllegalArgumentException("Expected default service for AID " + AID);
	}
	Log.d(TAG, "HCE service AID is " + AID);
}
 
Example #9
Source File: EchoHostAdpuServiceActivity.java    From external-nfc-api with Apache License 2.0 4 votes vote down vote up
@Override
protected void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);

	PreferenceManager.setDefaultValues(this, R.xml.preferences, false);

	setContentView(R.layout.activity_main);

	CardEmulation cardEmulation = CardEmulation.getInstance(NfcAdapter.getDefaultAdapter(this));

	boolean defaultService = cardEmulation.isDefaultServiceForAid(new ComponentName(this, EchoHostApduService.class), EchoHostApduService.AID);

	if (!defaultService) {
		Log.w(TAG, "Expected default service for AID " + EchoHostApduService.AID);
	}
	Log.d(TAG, "Service AID is " + EchoHostApduService.AID);

	enableBroadcast();

	showHelpfulDialog();

	Intent intent = getIntent();

	hostCardEmulationBroadcastReceiver.onReceive(this, getIntent());
}