1

I'm implementing an app which is using the NFC chip. When the phone is set to the airplane mode NFC is turned off and can be turned on manually via the preferences. I want to turn it on programmatically, so I can go on using the NFC chip for my purposes.

I found this which seems not to work on my galaxy nexus (API 14). The author mentioned he only tested it on API 15.

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

https://stackoverflow.com/a/11286431/1018288

I always get the Log.w(TAG, "Error setting NFC enabled state to "+ desiredState); message when executing. Any ideas?

Community
  • 1
  • 1
greg121
  • 914
  • 1
  • 11
  • 19
  • 4
    Considering that implementation uses the Secure Settings permission (which users might consider dodgy), I'd try to detect whether NFC is on in your app and tell the user if it isn't, then give them the option to open the NFC settings themselves. It's generally bad practice to programmatically change settings like that. – Alex Curran Jul 04 '12 at 16:47
  • See also http://stackoverflow.com/q/11292435/1202968. The Secure Settings permission is not available to normal apps, only system apps can get it. – NFC guy Jul 04 '12 at 20:41
  • @Espiandev: well I don't really care whether it is bad practice or not :-) nfcguy: thank you for that but I already saw your comment there. that's why I was wondering if the mentioned code is really working.. – greg121 Jul 05 '12 at 11:29

0 Answers0