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

The following examples show how to use android.nfc.NfcAdapter#isEnabled() . 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: NetWorkInfo.java    From MobileInfo with Apache License 2.0 6 votes vote down vote up
private static boolean hasNfc(Context context) {
    boolean bRet = false;
    if (context == null) {
        return false;
    }
    NfcManager manager = (NfcManager) context.getSystemService(Context.NFC_SERVICE);
    if (manager == null) {
        return false;
    }
    NfcAdapter adapter = manager.getDefaultAdapter();
    if (adapter != null && adapter.isEnabled()) {
        // adapter存在,能启用
        bRet = true;
    }
    return bRet;
}
 
Example 2
Source File: MainActivity.java    From LecteurOPUS with GNU General Public License v3.0 6 votes vote down vote up
private void enableReaderMode() {
    Log.i(TAG, "Enabling reader mode");
    Activity activity = this;
    NfcAdapter nfc = NfcAdapter.getDefaultAdapter(activity);
    if (!nfc.isEnabled()) {
        AlertDialog alertDialog = new AlertDialog.Builder(MainActivity.this).create();
        alertDialog.setTitle(getString(R.string.disable_title));
        alertDialog.setMessage(getString(R.string.nfc_disable));
        alertDialog.setButton(AlertDialog.BUTTON_NEUTRAL, "OK",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                        startActivity(new Intent(android.provider.Settings.ACTION_WIRELESS_SETTINGS));

                    }
                });
        alertDialog.show();
    }
    else if (nfc != null) {
        nfc.enableReaderMode(activity, mCardReader, FLAG_MIFARE | FLAG_OPUS, null);
    }
}
 
Example 3
Source File: RNHceModule.java    From react-native-nfc-hce with Apache License 2.0 6 votes vote down vote up
private WritableMap supportNFC() {
    NfcManager manager = (NfcManager) this.reactContext.getSystemService(this.reactContext.NFC_SERVICE);
    NfcAdapter adapter = manager.getDefaultAdapter();
    WritableMap map = Arguments.createMap();
    if (adapter != null) {
        map.putBoolean("support", true);
        if (adapter.isEnabled()) {
            map.putBoolean("enabled", true);
        } else {
            map.putBoolean("enabled", false);
        }
    } else {
        map.putBoolean("support", false);
        map.putBoolean("enabled", false);
    }

    return map;
}
 
Example 4
Source File: NfcDeviceFragment.java    From ESeal with Apache License 2.0 6 votes vote down vote up
private void enableNfcReaderMode() {
    NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(getActivity());
    if (nfcAdapter != null) {
        if (nfcAdapter.isEnabled()) {
            nfcAdapter.enableReaderMode(getActivity(), mNfcUtility, NfcUtility.NFC_TAG_FLAGS, null);
            showSnackbar(getString(R.string.text_hint_close_to_nfc_tag));
        } else {
            Snackbar snackbar = Snackbar.make(foldingCellLock, getString(R.string.error_nfc_not_open), Snackbar.LENGTH_SHORT)
                    .setAction(getString(R.string.text_hint_open_nfc), new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            Intent intent = new Intent(Settings.ACTION_NFC_SETTINGS);
                            startActivity(intent);
                        }
                    });
            ((TextView) (snackbar.getView().findViewById(R.id.snackbar_text))).setTextColor(ContextCompat.getColor(getContext(), R.color.blue_grey_100));
            snackbar.show();
        }
    }
}
 
Example 5
Source File: DeviceOperationActivity.java    From ESeal with Apache License 2.0 6 votes vote down vote up
private void enableNfcReaderMode() {
    NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(this);
    if (nfcAdapter != null) {
        if (nfcAdapter.isEnabled()) {
            nfcAdapter.enableReaderMode(this, mNfcUtility, NfcUtility.NFC_TAG_FLAGS, null);
            showSnackbar(getString(R.string.text_hint_close_to_nfc_tag));
        } else {
            Snackbar snackbar = Snackbar.make(cardSetting, getString(R.string.error_nfc_not_open), Snackbar.LENGTH_SHORT)
                    .setAction(getString(R.string.text_hint_open_nfc), new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            Intent intent = new Intent(Settings.ACTION_NFC_SETTINGS);
                            startActivity(intent);
                        }
                    });
            ((TextView) (snackbar.getView().findViewById(R.id.snackbar_text))).setTextColor(ContextCompat.getColor(DeviceOperationActivity.this, R.color.blue_grey_100));
            snackbar.show();
        }
    }
}
 
Example 6
Source File: RepoDetailsActivity.java    From fdroidclient with GNU General Public License v3.0 6 votes vote down vote up
@TargetApi(16)
private void prepareNfcMenuItems(Menu menu) {
    NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(this);
    MenuItem menuItem = menu.findItem(R.id.menu_enable_nfc);

    if (nfcAdapter == null) {
        menuItem.setVisible(false);
        return;
    }

    boolean needsEnableNfcMenuItem;
    if (Build.VERSION.SDK_INT < 16) {
        needsEnableNfcMenuItem = !nfcAdapter.isEnabled();
    } else {
        needsEnableNfcMenuItem = !nfcAdapter.isNdefPushEnabled();
    }

    menuItem.setVisible(needsEnableNfcMenuItem);
}
 
Example 7
Source File: MainPage.java    From NFCard with GNU General Public License v3.0 6 votes vote down vote up
public static CharSequence getContent(Activity activity) {

		final NfcAdapter nfc = NfcAdapter.getDefaultAdapter(activity);
		final int resid;

		if (nfc == null)
			resid = R.string.info_nfc_notsupport;
		else if (!nfc.isEnabled())
			resid = R.string.info_nfc_disabled;
		else
			resid = R.string.info_nfc_nocard;

		String tip = ThisApplication.getStringResource(resid);

		return new SpanFormatter(new Handler(activity)).toSpanned(tip);
	}
 
Example 8
Source File: MainPage.java    From nfcard with GNU General Public License v3.0 6 votes vote down vote up
public static CharSequence getContent(Activity activity) {

		final NfcAdapter nfc = NfcAdapter.getDefaultAdapter(activity);
		final int resid;

		if (nfc == null)
			resid = R.string.info_nfc_notsupport;
		else if (!nfc.isEnabled())
			resid = R.string.info_nfc_disabled;
		else
			resid = R.string.info_nfc_nocard;

		String tip = ThisApplication.getStringResource(resid);

		return new SpanFormatter(new Handler(activity)).toSpanned(tip);
	}
 
Example 9
Source File: LyricsViewFragment.java    From QuickLyric with GNU General Public License v3.0 6 votes vote down vote up
@TargetApi(16)
private void beamLyrics(final Lyrics lyrics, Activity activity) {
    NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(activity);
    if (nfcAdapter != null && nfcAdapter.isEnabled()) {
        if (lyrics.getText() != null) {
            nfcAdapter.setNdefPushMessageCallback(event -> {
                try {
                    byte[] payload = lyrics.toBytes(); // whatever data you want to send
                    NdefRecord record = new NdefRecord(NdefRecord.TNF_MIME_MEDIA, "application/lyrics".getBytes(), new byte[0], payload);
                    return new NdefMessage(new NdefRecord[]{
                            record, // your data
                            NdefRecord.createApplicationRecord("com.geecko.QuickLyric"), // the "application record"
                    });
                } catch (IOException e) {
                    return null;
                }
            }, activity);
        }
    }
}
 
Example 10
Source File: NfcNotEnabledActivity.java    From fdroidclient with GNU General Public License v3.0 5 votes vote down vote up
@TargetApi(16)
private void doOnJellybean(Intent intent) {
    NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(this);
    if (nfcAdapter == null) {
        return;
    }
    if (nfcAdapter.isEnabled()) {
        intent.setAction(Settings.ACTION_NFCSHARING_SETTINGS);
    } else {
        intent.setAction(Settings.ACTION_NFC_SETTINGS);
    }
}
 
Example 11
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 12
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 13
Source File: NFCTagPreferenceX.java    From PhoneProfilesPlus with Apache License 2.0 5 votes vote down vote up
void writeToNFCTag(long id, String tag) {

        NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(prefContext);
        if (!nfcAdapter.isEnabled()) {
            AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(prefContext);
            dialogBuilder.setTitle(R.string.nfc_tag_pref_dlg_menu_writeToNfcTag);
            dialogBuilder.setMessage(R.string.nfc_tag_pref_dlg_writeToNfcTag_nfcNotEnabled);
            dialogBuilder.setPositiveButton(android.R.string.ok, null);
            AlertDialog dialog = dialogBuilder.create();

//            dialog.setOnShowListener(new DialogInterface.OnShowListener() {
//                @Override
//                public void onShow(DialogInterface dialog) {
//                    Button positive = ((AlertDialog)dialog).getButton(DialogInterface.BUTTON_POSITIVE);
//                    if (positive != null) positive.setAllCaps(false);
//                    Button negative = ((AlertDialog)dialog).getButton(DialogInterface.BUTTON_NEGATIVE);
//                    if (negative != null) negative.setAllCaps(false);
//                }
//            });

            if (fragment.getActivity() != null)
                if (!fragment.getActivity().isFinishing())
                    dialog.show();
            return;
        }

        Intent nfcTagIntent = new Intent(prefContext.getApplicationContext(), NFCTagWriteActivity.class);
        nfcTagIntent.putExtra(NFCTagWriteActivity.EXTRA_TAG_NAME, tag);
        nfcTagIntent.putExtra(NFCTagWriteActivity.EXTRA_TAG_DB_ID, id);
        ((Activity) prefContext).startActivityForResult(nfcTagIntent, RESULT_NFC_TAG_WRITE);
    }
 
Example 14
Source File: PayActivity.java    From HandstandPay with GNU General Public License v2.0 5 votes vote down vote up
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();

    if (id == R.id.set_as_default) {
        DefaultPaymentAppUtil.ensureSetAsDefaultPaymentApp(PayActivity.this);
        return true;
    } else if (id == R.id.play_animation) {
        LocalBroadcastManager.getInstance(PayActivity.this).sendBroadcast(new Intent(HandstandApduService.PAYMENT_SENT));
        return true;
    } else if (id == R.id.edit_magstripe) {
        startEditMagstripeActivity();
        return true;
    } else if (id == R.id.nfc_settings) {
        NfcAdapter mNfcAdapter = NfcAdapter.getDefaultAdapter(PayActivity.this);

        //Tell the user whether NFC is enabled
        String message = "NFC is enabled: " + mNfcAdapter.isEnabled();
        Toast.makeText(PayActivity.this, message, Toast.LENGTH_SHORT).show();

        //Show the settings regardless of whether it is enabled or not
        Intent intent = new Intent(Settings.ACTION_NFC_SETTINGS);
        startActivity(intent);
    }

    return super.onOptionsItemSelected(item);
}
 
Example 15
Source File: HostConnectionProfile.java    From DeviceConnect-Android with MIT License 5 votes vote down vote up
@Override
public boolean onRequest(final Intent request, final Intent response) {
    NfcAdapter adapter = NfcAdapter.getDefaultAdapter(getContext());
    if (adapter != null) {
        if (adapter.isEnabled()) {
            response.putExtra(PARAM_ENABLE, true);
        } else {
            response.putExtra(PARAM_ENABLE, false);
        }
    } else {
        response.putExtra(PARAM_ENABLE, false);
    }
    setResult(response, IntentDConnectMessage.RESULT_OK);
    return true;
}
 
Example 16
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 17
Source File: DeviceStatusUtil.java    From under-the-hood with Apache License 2.0 5 votes vote down vote up
/**
 * Current NFC hardware state. Needs correct permission to work.
 *
 * @param context
 * @return state
 */
//@RequiresPermission(Manifest.permission.NFC)
public static Status getNfcState(Context context) {
    NfcManager nfcManager = (NfcManager) context.getSystemService(Context.NFC_SERVICE);
    NfcAdapter adapter = nfcManager.getDefaultAdapter();
    if (adapter == null) {
        return Status.UNSUPPORTED;
    } else if (ContextCompat.checkSelfPermission(context, Manifest.permission.NFC) != PackageManager.PERMISSION_GRANTED) {
        return Status.NEEDS_PERMISSION;
    } else if (adapter.isEnabled()) {
        return Status.ENABLED;
    } else {
        return Status.DISABLED;
    }
}
 
Example 18
Source File: NfcIconData.java    From Status with Apache License 2.0 5 votes vote down vote up
@Override
public void register() {
    super.register();

    NfcAdapter adapter = manager.getDefaultAdapter();
    if (adapter != null && adapter.isEnabled())
        onIconUpdate(0);
}
 
Example 19
Source File: NfcHandler.java    From PowerSwitch_Android with GNU General Public License v3.0 2 votes vote down vote up
/**
 * Checks if NFC is supported by the device
 *
 * @param context any suitable context
 * @return true if NFC is supported, false otherwise
 */
public static boolean isNfcEnabled(Context context) {
    NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(context);
    return nfcAdapter.isEnabled();
}
 
Example 20
Source File: SettingsInfo.java    From batteryhub with Apache License 2.0 2 votes vote down vote up
/**
 * Checks if NFC is enabled on the device
 *
 * @return True if NFC is enabled
 */
public static boolean isNfcEnabled(final Context context) {
    NfcAdapter adapter = NfcAdapter.getDefaultAdapter(context);
    return adapter != null && adapter.isEnabled();
}