7

I'm trying to determine the preferred way for programmatically enabling bluetooth on Android. I've found that either of the following techniques works (at least on Android 4.0.4...):

public class MyActivity extends Activity {
    public static final int MY_BLUETOOTH_ENABLE_REQUEST_ID = 6;
    ...
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ...
        Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
        startActivityForResult(enableBtIntent, MY_BLUETOOTH_ENABLE_REQUEST_ID);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == MY_BLUETOOTH_ENABLE_REQUEST_ID) {
            if (resultCode == RESULT_OK) {
                // Request granted - bluetooth is turning on...
            }
            if (resultCode == RESULT_CANCELED) {
                // Request denied by user, or an error was encountered while 
                // attempting to enable bluetooth
            }
        }
    }

or...

BluetoothAdapter.getDefaultAdapter().enable();

The former asks the user for permission prior to enabling while the latter just silently enables bluetooth (but requires the "android.permission.BLUETOOTH_ADMIN" permission). Is one or the other old/obsolete and/or is one technique only available on some devices? or is it just a matter of personal preference as to which I use?

Troy
  • 21,172
  • 20
  • 74
  • 103
  • I remember reading somewhere that you shouldn't turn on things without telling the user. Google also doesn't like it. – noidraug Mar 21 '14 at 15:18
  • 1
    That's true, but both techniques are provided by Google and, therefore, should be Google-approved... and both tell the user, it's just a matter of WHEN they tell the user (at install vs. later when it actually turns bluetooth on). I know one may be preferable from a customer-friendliness standpoint, but I'd also like to know if both techniques are available everywhere or if one or the other is restricted to certain versions, or if one was once the normal way, but is now frowned upon, etc. – Troy Mar 21 '14 at 15:31
  • Hey Troy! Have you find the answer? I was going to use this in my application but I saw that warning. So what should I do? I have already added those permissions in manifest file, is there anything else that I need to do? Or for ex. if I upload my app into play store, would that refuse it? Thanks! – Hilal Jun 08 '19 at 02:21

3 Answers3

5

It is clearly mentioned in Android Doc

Bluetooth should never be enabled without direct user consent. If you want to turn on Bluetooth in order to create a wireless connection, you should use the ACTION_REQUEST_ENABLE Intent, which will raise a dialog that requests user permission to turn on Bluetooth. The enable() method is provided only for applications that include a user interface for changing system settings, such as a "power manager" app.

Both of these techniques would work. You have to choose based on your purpose and requirement. Hope it answers your questions.

theangrylama
  • 216
  • 3
  • 11
1

I think this can be helpful...

https://stackoverflow.com/a/20142972/1386533

You also needs to add following permissions into the manifest file as well.

android.permission.BLUETOOTH,

android.permission.BLUETOOTH_ADMIN

Community
  • 1
  • 1
Rakesh Gondaliya
  • 1,050
  • 3
  • 25
  • 42
  • Thanks for the reply, but I don't need instructions for enabling bluetooth (I provided instructions in my question). The question was regarding preferability between the 2 techniques and the availability of each technique among Android versions. – Troy Mar 22 '14 at 13:22
1

This works for me... BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); mBluetoothAdapter.enable();

parvez rafi
  • 464
  • 4
  • 20