Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programmatically enabling bluetooth on Android

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?

like image 239
Troy Avatar asked Mar 21 '14 15:03

Troy


People also ask

How to enable Bluetooth in Android?

This example demonstrates How to enable Bluetooth in android. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main.xml. In the above code, we have taken a text view to show Bluetooth status.

How to show Bluetooth status in Android Studio?

Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main.xml. In the above code, we have taken a text view to show Bluetooth status. Let's try to run your application.

What is Bluetooth in Android?

The Android platform includes support for the Bluetooth network stack, which allows a device to wirelessly exchange data with other Bluetooth devices. The app framework provides access to the Bluetooth functionality through Bluetooth APIs.

What is the use of the bluetoothadapter?

The BluetoothAdapter is the entry-point for all Bluetooth interaction. Using this, you can discover other Bluetooth devices, query a list of bonded (paired) devices, instantiate a BluetoothDevice using a known MAC address, and create a BluetoothServerSocket to listen for communications from other devices. Represents a remote Bluetooth device.


2 Answers

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.

like image 93
theangrylama Avatar answered Oct 01 '22 12:10

theangrylama


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

like image 39
Rakesh Gondaliya Avatar answered Oct 01 '22 14:10

Rakesh Gondaliya