Ron*_*nie 57
NfcManager manager = (NfcManager) context.getSystemService(Context.NFC_SERVICE);
NfcAdapter adapter = manager.getDefaultAdapter();
if (adapter != null && adapter.isEnabled()) {
    // adapter exists and is enabled.
}
您无法以编程方式启用NFC.用户必须通过设置或硬件按钮手动完成.
使用PackageManager和hasSystemFeature("android.hardware.nfc")匹配<uses-feature android:name="android.hardware.nfc"  android:required="false" />清单中应该具有的元素.
从2.3.3开始,您还可以使用NfcAdapter.getDefaultAdapter()获取适配器(如果可用)并调用其isEnabled()方法来检查NFC当前是否已打开.
我可能在这里有点晚了,但我已经实现了一个'完整'的例子,检测到了
我还添加了一个相应的Beam 示例,它使用了
nfcAdapter.isNdefPushEnabled()
在后来的Android版本中引入的方法来检测梁状态,如2)和3).
这可以简单地使用以下代码完成:
NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(this);
if (nfcAdapter == null) {
    // NFC is not available for device
} else if (!nfcAdapter.isEnabled()) {
    // NFC is available for device but not enabled
} else {
    // NFC is enabled
}
请记住,即使在使用您的应用程序时,用户也可以关闭 NFC。
来源:https : //developer.android.com/guide/topics/connectivity/nfc/nfc#manifest
虽然您自己无法以编程方式启用 NFC,但您可以通过使用按钮打开 NFC 设置来要求用户启用它,如下所示:
Intent intent
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
    intent = new Intent(Settings.ACTION_NFC_SETTINGS);
} else {
    Intent intent = new Intent(Settings.ACTION_WIRELESS_SETTINGS);
}
startActivity(intent);