Android: Changing NFC settings (on/off) programmatically

前端 未结 5 1213
鱼传尺愫
鱼传尺愫 2020-12-01 05:27

I trying to change NFC settings (on/off) programmatically on Android 2.3.3.

On the phone, under the \"Wireless & network settings\",
you can choose to set wh

5条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-01 06:03

    if you want to do it programmatically, apperently this Q holds the answer:

    How can I enable NFC reader via API?

    Edit

    it didn't hold the answer, but it held the key to the answer, on which I based my code I answered with in the Q.

    I will paste it here as well in case anyone's interested.

    I got it working through reflection

    This code works on API 15, haven't checked it against other verions yet

    public boolean changeNfcEnabled(Context context, boolean enabled) {
        // Turn NFC on/off
        final boolean desiredState = enabled;
        mNfcAdapter = NfcAdapter.getDefaultAdapter(context);
    
        if (mNfcAdapter == null) {
            // NFC is not supported
            return false;
        }
    
        new Thread("toggleNFC") {
            public void run() {
                Log.d(TAG, "Setting NFC enabled state to: " + desiredState);
                boolean success = false;
                Class NfcManagerClass;
                Method setNfcEnabled, setNfcDisabled;
                boolean Nfc;
                if (desiredState) {
                    try {
                        NfcManagerClass = Class.forName(mNfcAdapter.getClass().getName());
                        setNfcEnabled   = NfcManagerClass.getDeclaredMethod("enable");
                        setNfcEnabled.setAccessible(true);
                        Nfc             = (Boolean) setNfcEnabled.invoke(mNfcAdapter);
                        success         = Nfc;
                    } catch (ClassNotFoundException e) {
                    } catch (NoSuchMethodException e) {
                    } catch (IllegalArgumentException e) {
                    } catch (IllegalAccessException e) {
                    } catch (InvocationTargetException e) {
                    }
                } else {
                    try {
                        NfcManagerClass = Class.forName(mNfcAdapter.getClass().getName());
                        setNfcDisabled  = NfcManagerClass.getDeclaredMethod("disable");
                        setNfcDisabled.setAccessible(true);
                        Nfc             = (Boolean) setNfcDisabled.invoke(mNfcAdapter);
                        success         = Nfc;
                    } catch (ClassNotFoundException e) {
                    } catch (NoSuchMethodException e) {
                    } catch (IllegalArgumentException e) {
                    } catch (IllegalAccessException e) {
                    } catch (InvocationTargetException e) {
                    }
                }
                if (success) {
                    Log.d(TAG, "Successfully changed NFC enabled state to "+ desiredState);
                } else {
                    Log.w(TAG, "Error setting NFC enabled state to "+ desiredState);
                }
            }
        }.start();
        return false;
    }//end method
    

    This requires 2 permissions though, put them in the manifest:

     
        
        
    

    The NFC button's state switches accordingly when the code is used, so there are no issues when doing it manually in the seetings menu.


    To clarify: This code doesn't work on normal devices. There are ways around, but at least it requires root.

提交回复
热议问题