我怎样才能实现通过API NFC阅读器?阅读器、API、NFC

2023-09-04 23:12:42 作者:风筝、在阴天搁浅。

有没有什么办法可以使用​​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).

我发现这个从 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;
}