Java Code Examples for android.nfc.NfcAdapter#enableForegroundDispatch()

The following examples show how to use android.nfc.NfcAdapter#enableForegroundDispatch() . 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: PassportConActivity.java    From polling-station-app with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * Setup the recognition of nfc tags when the activity is opened (foreground)
 *
 * @param activity The corresponding {@link Activity} requesting the foreground dispatch.
 * @param adapter The {@link NfcAdapter} used for the foreground dispatch.
 */
public static void setupForegroundDispatch(final Activity activity, NfcAdapter adapter) {
    final Intent intent = new Intent(activity.getApplicationContext(), activity.getClass());
    intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);

    final PendingIntent pendingIntent = PendingIntent.getActivity(activity.getApplicationContext(), 0, intent, 0);

    IntentFilter[] filters = new IntentFilter[1];
    String[][] techList = new String[][]{};

    // Filter for nfc tag discovery
    filters[0] = new IntentFilter();
    filters[0].addAction(NfcAdapter.ACTION_TAG_DISCOVERED);
    filters[0].addCategory(Intent.CATEGORY_DEFAULT);

    adapter.enableForegroundDispatch(activity, pendingIntent, filters, techList);
}
 
Example 2
Source File: MainFlowActivity.java    From flow-android with MIT License 6 votes vote down vote up
/**
 * @param activity The activity that's requesting dispatch
 * @param adapter NfcAdapter for the current context
 */
private void enableForegroundDispatch(Activity activity, NfcAdapter adapter) {
    final Intent intent = new Intent(activity.getApplicationContext(), activity.getClass());
    intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);

    final PendingIntent pendingIntent = PendingIntent.getActivity(activity.getApplicationContext(), 0, intent, 0);

    IntentFilter[] filters = new IntentFilter[1];
    String[][] techList = new String[][]{};

    // Notice that this is the same filter as in our manifest.
    filters[0] = new IntentFilter();
    filters[0].addAction(NfcAdapter.ACTION_NDEF_DISCOVERED);
    filters[0].addCategory(Intent.CATEGORY_DEFAULT);
    filters[0].addDataScheme("https");
    filters[0].addDataAuthority(Constants.FLOW_DOMAIN, null);
    filters[0].addDataPath(".*", PatternMatcher.PATTERN_SIMPLE_GLOB);

    adapter.enableForegroundDispatch(activity, pendingIntent, filters, techList);
}
 
Example 3
Source File: MainActivity.java    From QuickLyric with GNU General Public License v3.0 6 votes vote down vote up
@Override
protected void onResume() {
    super.onResume();
    NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(this);
    if (nfcAdapter != null) {
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, ((Object) this).getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
        IntentFilter ndef = new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED);
        try {
            ndef.addDataType("application/lyrics");
        } catch (IntentFilter.MalformedMimeTypeException e) {
            return;
        }
        IntentFilter[] intentFiltersArray = new IntentFilter[]{ndef,};
        try {
            nfcAdapter.enableForegroundDispatch(this, pendingIntent, intentFiltersArray, null);
        } catch (Exception ignored) {
        }
    }
    updatePrefsChanges();
    AppBarLayout appBarLayout = findViewById(id.appbar);
    appBarLayout.removeOnOffsetChangedListener(this);
    appBarLayout.addOnOffsetChangedListener(this);
}
 
Example 4
Source File: Abbott.java    From FreeStyleLibre-NFC-Reader with GNU General Public License v3.0 6 votes vote down vote up
/**
 * @param activity The corresponding {@link Activity} requesting the foreground dispatch.
 * @param adapter The {@link NfcAdapter} used for the foreground dispatch.
 */
public static void setupForegroundDispatch(final Activity activity, NfcAdapter adapter) {
    final Intent intent = new Intent(activity.getApplicationContext(), activity.getClass());
    intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);

    final PendingIntent pendingIntent = PendingIntent.getActivity(activity.getApplicationContext(), 0, intent, 0);

    IntentFilter[] filters = new IntentFilter[1];
    String[][] techList = new String[][]{};

    // Notice that this is the same filter as in our manifest.
    filters[0] = new IntentFilter();
    filters[0].addAction(NfcAdapter.ACTION_NDEF_DISCOVERED);
    filters[0].addCategory(Intent.CATEGORY_DEFAULT);

    adapter.enableForegroundDispatch(activity, pendingIntent, filters, techList);
}
 
Example 5
Source File: ActivityAddFriend.java    From ploggy with GNU General Public License v3.0 6 votes vote down vote up
private void setupForegroundDispatch() {
    NfcAdapter nfcAdapter = getNfcAdapter();
    if (nfcAdapter != null) {
        IntentFilter intentFilter = new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED);
        try {
            intentFilter.addDataType(getNfcMimeType());
        } catch (IntentFilter.MalformedMimeTypeException e) {
            Log.addEntry(LOG_TAG, e.getMessage());
            return;
        }
        nfcAdapter.enableForegroundDispatch(
               this,
               PendingIntent.getActivity(
                       this,
                       0,
                       new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0),
               new IntentFilter[] {intentFilter},
               null);
    }
}
 
Example 6
Source File: TagDispatcher.java    From GreenBits with GNU General Public License v3.0 5 votes vote down vote up
private void enableForegroundDispatch(NfcAdapter adapter, Intent intent) {
    if(adapter.isEnabled()) {
        PendingIntent tagIntent = PendingIntent.getActivity(activity, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
        IntentFilter tag = new IntentFilter(NfcAdapter.ACTION_TECH_DISCOVERED);

        adapter.enableForegroundDispatch(activity, tagIntent, new IntentFilter[]{tag},
                                         new String[][]{new String[]{IsoDep.class.getName()}});
    }
}
 
Example 7
Source File: PGPClipperResultShowActivity.java    From PGPClipper with Apache License 2.0 5 votes vote down vote up
private void enableTagReading(NfcAdapter adapter) {
    try {
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);

        adapter.enableForegroundDispatch(this, pendingIntent, null, null);
    } catch (Exception e) {
        // ignore
    }
}
 
Example 8
Source File: PGPClipperQuickReplyActivity.java    From PGPClipper with Apache License 2.0 5 votes vote down vote up
private void enableTagReading(NfcAdapter adapter) {
    try {
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);

        adapter.enableForegroundDispatch(this, pendingIntent, null, null);
    } catch (Exception e) {
        // ignore
    }
}
 
Example 9
Source File: TagDispatcher.java    From nordpol with MIT License 5 votes vote down vote up
private void enableForegroundDispatch(NfcAdapter adapter, Intent intent) {
    if(adapter.isEnabled()) {
        PendingIntent tagIntent = PendingIntent.getActivity(activity, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
        IntentFilter tag = new IntentFilter(NfcAdapter.ACTION_TECH_DISCOVERED);
        adapter.enableForegroundDispatch(activity, tagIntent, new IntentFilter[]{tag},
                                         new String[][]{new String[]{IsoDep.class.getName()}});
    }
}
 
Example 10
Source File: TagDispatcher.java    From WalletCordova with GNU Lesser General Public License v2.1 5 votes vote down vote up
private void enableForegroundDispatch(NfcAdapter adapter, Intent intent) {
    if(adapter.isEnabled()) {
        PendingIntent tagIntent = PendingIntent.getActivity(activity, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
        IntentFilter tag = new IntentFilter(NfcAdapter.ACTION_TECH_DISCOVERED);

        adapter.enableForegroundDispatch(activity, tagIntent, new IntentFilter[]{tag},
                                         new String[][]{new String[]{IsoDep.class.getName()}});
    }
}
 
Example 11
Source File: NFCAuthenticationSetupActivity.java    From PGPClipper with Apache License 2.0 4 votes vote down vote up
private void enableTagReading(NfcAdapter adapter) {
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);

    adapter.enableForegroundDispatch(this, pendingIntent, null, null);
}