我怎样才能实现通过API NFC阅读器? [英] How can I enable NFC reader via API?

查看:212
本文介绍了我怎样才能实现通过API NFC阅读器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有什么办法可以使用​​API​​使Android的NFC阅读器?

There is any way I can enable Android NFC reader using API?

推荐答案

因此​​很明显,有没有办法从API使NFC,尽管谷歌这样做,在他们的来源$ C ​​$ C(见下文)。

So apparently there is no way to enable the NFC from the API, even though Google does so within their source code (see below).

如果你从API一条线, NfcAdapter.isEnabled()

If you look at a line from the API for NfcAdapter.isEnabled():

返回真,如果这个NFC适配器有   任何使能的功能。

Return true if this NFC Adapter has any features enabled.

应用程序可以使用这个作为一个辅助   表明该用户应将   对NFC的设置。

Application may use this as a helper to suggest that the user should turn on NFC in Settings.

如果该方法返回false,NFC   硬件保证不会产生   或回应任何NFC交易。

If this method returns false, the NFC hardware is guaranteed not to generate or respond to any NFC transactions.

它看起来像有没有办法在API中做到这一点。令人失望。最好的办法是一个对话框,告知他们需要启用它的设置,也许推出设置意向用户。

It looks like there is no way to do it within the API. Bummer. Your best bet is a dialog to inform the user they need to enable it in the settings, and perhaps launch a settings intent.

编辑:以下是从源头抓起,但看起来他们没有让用户实现API(我很困惑这个)中的方法

The following is from the source, but it looks like they didn't allow the user to implement the methods in the API (I'm confused about this).

我发现这个从<一个href="http://www.java2s.com/Open-Source/Android/android-platform-apps/Settings/com/android/settings/nfc/NfcEnabler.java.htm"相对=nofollow> Android源$ C ​​$ C 帮助启用和禁用适配器。

I found this from the android source code to help enable and disable the adapter.

相关来源:

public boolean onPreferenceChange(Preference preference,
        Object value) {
    // Turn NFC on/off

    final boolean desiredState = (Boolean) value;
    mCheckbox.setEnabled(false);

    // Start async update of the NFC adapter state, as the API is
    // unfortunately blocking...
    new Thread("toggleNFC") {
        public void run() {
            Log.d(TAG, "Setting NFC enabled state to: "
                    + desiredState);
            boolean success = false;
            if (desiredState) {
                success = mNfcAdapter.enable();
            } else {
                success = mNfcAdapter.disable();
            }
            if (success) {
                Log.d(TAG,
                        "Successfully changed NFC enabled state to "
                                + desiredState);
                mHandler.post(new Runnable() {
                    public void run() {
                        handleNfcStateChanged(desiredState);
                    }
                });
            } else {
                Log.w(TAG, "Error setting NFC enabled state to "
                        + desiredState);
                mHandler.post(new Runnable() {
                    public void run() {
                        mCheckbox.setEnabled(true);
                        mCheckbox
                                .setSummary(R.string.nfc_toggle_error);
                    }
                });
            }
        }
    }.start();
    return false;
}

这篇关于我怎样才能实现通过API NFC阅读器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆