简体   繁体   English

以编程方式检查设备是否具有 NFC 阅读器

[英]Check programmatically if device has NFC reader

Is there a way to check at run time whether a device has an NFC reader?有没有办法在运行时检查设备是否有 NFC 阅读器? My app uses NFC to perform a task, but if no reader is present, it can perform the same task by using a button.我的应用程序使用 NFC 来执行任务,但如果没有阅读器,它可以通过使用按钮来执行相同的任务。

Hope This works for you希望这对你有用

NfcManager manager = (NfcManager) context.getSystemService(Context.NFC_SERVICE);
NfcAdapter adapter = manager.getDefaultAdapter();
if (adapter != null && adapter.isEnabled()) {

    //Yes NFC available 
}else if(adapter != null && !adapter.isEnabled()){

   //NFC is not enabled.Need to enable by the user.
}else{
   //NFC is not supported
}

The simplest way to check if an Android device has NFC functionality is to check for the system feature PackageManager.FEATURE_NFC ("android.hardware.nfc"):检查 Android 设备是否具有 NFC 功能的最简单方法是检查系统功能PackageManager.FEATURE_NFC ("android.hardware.nfc"):

PackageManager pm = context.getPackageManager();
if (pm.hasSystemFeature(PackageManager.FEATURE_NFC)) {
    // device has NFC functionality
}

However, there exist devices (at least one of Sony's first Android NFC smartphones has this issue) that do not properly report the FEATURE_NFC .但是,存在无法正确报告FEATURE_NFC设备(至少索尼首批 Android NFC 智能手机之一存在此问题)。 (That's those devices that do not allow you to install apps that require NFC functionality through Play Store does such a check for apps that require NFC.) (那些不允许您通过 Play 商店安装需要 NFC 功能的应用程序的设备会检查需要 NFC 的应用程序。)

Therefore, the more reliable solution is the one described by Sainath Patwary karnate .因此,更可靠的解决方案是Sainath Patwary karnate描述的解决方案。 To check if a device has NFC functionality (or rather if a device has a running NFC service), you can use:要检查设备是否具有 NFC 功能(或者设备是否具有正在运行的 NFC 服务),您可以使用:

NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(context);
if (nfcAdapter != null) {
    // device has NFC functionality
}

If you also want to check if the user enabled NFC on their device, you may use the NfcAdapter 's isEnabled() method.如果您还想检查用户是否在其设备上启用了 NFC,您可以使用NfcAdapterisEnabled()方法。 But be warned that it's not always as easy as described by Sainath Patwary karnate .但请注意,它并不总是像Sainath Patwary karnate所描述的那样容易。 Particularly on Android 4.0.*, the isEnabled() method sometimes throws undocumented exceptions when the NFC service had crashed before, so you might want to catch those exceptions.特别是在 Android 4.0.* 上, isEnabled()方法有时会在 NFC 服务之前崩溃时抛出未记录的异常,因此您可能想要捕获这些异常。 Moreover, on Android >= 2.3.4 and < 4.1 (I could not reproduce the problem on later versions but that does not mean it is not there!), the first call to isEnabled() after the NFC service had been stopped or crashed always returned false , so it is advisable to always ignore the result of the first call of isEnabled() .此外,在 Android >= 2.3.4 和 < 4.1(我无法在更高版本上重现该问题,但这并不意味着它不存在!),在 NFC 服务停止或崩溃后第一次调用isEnabled()始终返回false ,因此建议始终忽略第一次调用isEnabled()

if (nfcAdapter != null) {
    try {
        nfcAdapter.isEnabled();
    } catch (Exception e) {}
    bool isEnabled = false;
    try {
        isEnabled = nfcAdapter.isEnabled();
    } catch (Exception e) {}
    if (isEnabled) {
        // NFC functionality is available and enabled
    }
}

Here's my function that I use for detecting NFC presence.这是我用于检测 NFC 存在的函数。

public static boolean deviceHasNfc() {
    // Is NFC adapter present (whether enabled or not)
    NfcManager nfcMgr = (NfcManager) context.getSystemService(Context.NFC_SERVICE);
    if (manager != null) {
        NfcAdapter adapter = manager.getDefaultAdapter();
        return adapter != null;
    }
    return false;
}

As stated in @Sainath's answer you can also detect if the NFC is enabled using adapter.isEnabled()如@Sainath 的回答所述,您还可以使用adapter.isEnabled()检测是否启用了 NFC

For those of you doing Kotlin here is a quick enabled check extension following the rules posted above对于那些使用 Kotlin 的人来说,这里是一个快速启用的检查扩展,遵循上面发布的规则

fun Context.isNfcEnabled(): Boolean {
    val nfcAdapter = NfcAdapter.getDefaultAdapter(this)
    if (nfcAdapter != null) {
        return try {
            nfcAdapter.isEnabled
        } catch (exp: Exception) {
            // Double try this as there are times it will fail first time
            try {
                nfcAdapter.isEnabled
            } catch (exp: Exception) {
                false
            }
        }
    }
    return false
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM