@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); CardEmulation cardEmulation = CardEmulation.getInstance(NfcAdapter.getDefaultAdapter(this)); boolean defaultService = cardEmulation.isDefaultServiceForAid(new ComponentName(this, ExternalNFCHostApduService.class), ExternalNFCHostApduService.AID); if(!defaultService) { Log.d(TAG, "Expected default service for AID " + ExternalNFCHostApduService.AID); } Log.d(TAG, "Service AID is " + ExternalNFCHostApduService.AID); enableBroadcast(); showHelpfulDialog(); }
@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"); } }
@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())); }
@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); } }
@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); } }
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); } }
/** * Check if PaymentService is the default NFC payment app, and if not request user to set it. */ private void checkDefaultPaymentApp() { CardEmulation cardEmulationManager = CardEmulation.getInstance(NfcAdapter.getDefaultAdapter(this)); ComponentName paymentServiceComponent = new ComponentName(getApplicationContext(), PaymentService.class.getCanonicalName()); boolean isPaymentServiceDefault = cardEmulationManager.isDefaultServiceForCategory(paymentServiceComponent, CardEmulation.CATEGORY_PAYMENT); if (!isPaymentServiceDefault) { 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); } }
@Override public void onCreate() { super.onCreate(); Log.d(TAG, "Service created"); CardEmulation cardEmulation = CardEmulation.getInstance(NfcAdapter.getDefaultAdapter(this)); boolean defaultService = cardEmulation.isDefaultServiceForAid(new ComponentName(this, ExternalNFCHostApduService.class), AID); if(!defaultService) { throw new IllegalArgumentException("Expected default service for AID " + AID); } Log.d(TAG, "Service AID is " + AID); }