Question

I try to activate or deactivate the Android Beam feature programmatically on ICS but I can't find any api for this. Is it possible ?

And I would know if Android Beam feature is enabled before initiate a push operation. Is it possible ?

Was it helpful?

Solution

In the phone's Settings you can enable and disable the Android Beam feature (Wireless Networks -> More... -> Android Beam). Normal apps do not have the necessary permission to turn this on or off (and there is no API). You can, however, send and Intent from your app to open this Settings screen directly, using new Intent(Settings.ACTION_WIRELESS_SETTINGS).

On Android 4.1 JB, a new API call was added, NfcAdapter.isNdefPushEnabled(), to check whether Android Beam is turned on or off.

BTW: Even if Android Beam is disabled, your device will still be able to receive Beam messages, as long as NFC is turned on.

OTHER TIPS

You can specifically choose which Settings screen to bring up based on the Android version and the current state of things. Here's how I did it:

import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Intent;
import android.nfc.NfcAdapter;
import android.os.Build;
import android.os.Bundle;
import android.provider.Settings;

@TargetApi(14)
// aka Android 4.0 aka Ice Cream Sandwich
public class NfcNotEnabledActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        final Intent intent = new Intent();
        if (Build.VERSION.SDK_INT >= 16) {
            /*
             * ACTION_NFC_SETTINGS was added in 4.1 aka Jelly Bean MR1 as a
             * separate thing from ACTION_NFCSHARING_SETTINGS. It is now
             * possible to have NFC enabled, but not "Android Beam", which is
             * needed for NDEF. Therefore, we detect the current state of NFC,
             * and steer the user accordingly.
             */
            if (NfcAdapter.getDefaultAdapter(this).isEnabled())
                intent.setAction(Settings.ACTION_NFCSHARING_SETTINGS);
            else
                intent.setAction(Settings.ACTION_NFC_SETTINGS);
        } else if (Build.VERSION.SDK_INT >= 14) {
            // this API was added in 4.0 aka Ice Cream Sandwich
            intent.setAction(Settings.ACTION_NFCSHARING_SETTINGS);
        } else {
            // no NFC support, so nothing to do here
            finish();
            return;
        }
        startActivity(intent);
        finish();
    }
}

(I hereby put this code into the public domain, no license terms or attribution needed)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top