How to turn on Bluetooth when a button is pressed

In my application, I need to turn on the bluetooth of my device when I press a button. How can i achieve this? An example will be really helpful. Also, what permissions do I require to include in my mainfest.xml for the same?

+6
source share
3 answers

Below are snippets of code from the Android documentation for Bluetooth

In the manifest file for permissions:

<manifest ... > <uses-permission android:name="android.permission.BLUETOOTH" /> ... </manifest> 

Source code for turning on bluetooth

 BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); if (mBluetoothAdapter == null) { // Device does not support Bluetooth } if (!mBluetoothAdapter.isEnabled()) { Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT); } 

If enabling Bluetooth succeeds, your Activity will receive the RESULT_OK result RESULT_OK in the onActivityResult() . If Bluetooth was not turned on due to an error (or the user answered “No”), the result will be RESULT_CANCELED .

+6
source

Turn on Bluetooth in Android, it is not difficult. But you should pay attention to some details. There are three ways to enable Bluetooth in Android.

1.Force to open bluetooth.

To get the default bluetooth adapter, i.e. use BluetoothAdapter.getDefaultAdapter() ; You need this permission:

 <uses-permission android:name="android.permission.BLUETOOTH" /> 

To force open Bluetooth, i.e. use BluetoothAdapter.enable() ; You need this permission:

 <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" /> 

Here is a sample code

 /** * Bluetooth Manager * * @author ifeegoo www.ifeegoo.com * */ public class BluetoothManager { /** * Whether current Android device support Bluetooth. * * @return true:Support Bluetooth false:not support Bluetooth */ public static boolean isBluetoothSupported() { return BluetoothAdapter.getDefaultAdapter() != null ? true : false; } /** * Whether current Android device Bluetooth is enabled. * * @return true:Bluetooth is enabled false:Bluetooth not enabled */ public static boolean isBluetoothEnabled() { BluetoothAdapter bluetoothAdapter = BluetoothAdapter .getDefaultAdapter(); if (bluetoothAdapter != null) { return bluetoothAdapter.isEnabled(); } return false; } /** * Force to turn on Bluetooth on Android device. * * @return true:force to turn on Bluetooth success * false:force to turn on Bluetooth failure */ public static boolean turnOnBluetooth() { BluetoothAdapter bluetoothAdapter = BluetoothAdapter .getDefaultAdapter(); if (bluetoothAdapter != null) { return bluetoothAdapter.enable(); } return false; } } 

The method above to enable Bluetooth will not tell the user whether Bluetooth mode is enabled or not. When you call the enable () method, you will not turn on Bluetooth 100%. Due to the reason of some security applications, refuse to do this and so on. You should pay attention to the return value of the enable () method.

The official api of the enable () method tells us that:

Bluetooth should not be turned on without the express consent of the user. If you want to enable Bluetooth to create a wireless connection, you must use the ACTION_REQUEST_ENABLE Intent, which will result in a dialog box asking for user permission to enable Bluetooth. The enable () method is provided only for applications that include a user interface for changing system settings, such as the "power manager" application.

Therefore, you do not need to enable Bluetooth using the enable () method unless you tell the user what you will do.

2.Use System Alert to remind users that you turn on Bluetooth with startActivityForResult.

 this.startActivityForResult(requestBluetoothOn, REQUEST_CODE_BLUETOOTH_ON) need this permission: <uses-permission android:name="android.permission.BLUETOOTH" /> public class MainActivity extends Activity { /** * Custom integer value code for request to turn on Bluetooth,it equal *requestCode in onActivityResult. */ private static final int REQUEST_CODE_BLUETOOTH_ON = 1313; /** * Bluetooth device discovery time,second。 */ private static final int BLUETOOTH_DISCOVERABLE_DURATION = 250; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); this.setContentView(R.layout.activity_main); if ((BluetoothManager.isBluetoothSupported()) && (!BluetoothManager.isBluetoothEnabled())) { this.turnOnBluetooth(); } } /** * Use system alert to remind user that the app will turn on Bluetooth */ private void turnOnBluetooth() { // request to turn on Bluetooth Intent requestBluetoothOn = new Intent( BluetoothAdapter.ACTION_REQUEST_ENABLE); // Set the Bluetooth discoverable. requestBluetoothOn .setAction(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE); // Set the Bluetooth discoverable time. requestBluetoothOn.putExtra( BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, BLUETOOTH_DISCOVERABLE_DURATION); // request to turn on Bluetooth this.startActivityForResult(requestBluetoothOn, REQUEST_CODE_BLUETOOTH_ON); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == REQUEST_CODE_BLUETOOTH_ON) { switch (resultCode) { // When the user press OK button. case Activity.RESULT_OK: { // TODO } break; // When the user press cancel button or back button. case Activity.RESULT_CANCELED: { // TODO } break; default: break; } } } } 

This is the best way to enable Bluetooth on Android.

3.Support users in Bluetooth settings to turn on Bluetooth by yourself.

 // Guide users to system Bluetooth settings to turn on Bluetooth by himselves. this.startActivity(new Intent(Settings.ACTION_BLUETOOTH_SETTINGS)); 

In my application. Considering the logical code and user interface, I use the following steps to enable Bluetooth:

1.use enable () enable Bluetooth. But I will tell users that I will turn on Bluetooth, if they allow me to do this, I will call enable () to turn on Bluetooth. This is better than triggering a system alert because we can control the content to remind the user. You should pay attention to the fact that it is not 100% to enable Bluetooth with the enable () function, some security applications or for other reasons refuse this. But we can evaluate whether we turned on Bluetooth success or not by turning on () the return value of the method.

2. If the enable () method returns false, we use startActivityForResult to remind users that we will enable Bluetooth.

3. If step 2 does not work for some reason, you can direct users to the Bluetooth settings to turn on Bluetooth on their own.

Additional information on enabling and disabling Bluetooth in Android: http://www.ifeegoo.com/android-turn-on-and-turn-off-bluetooth.html

+2
source

This solution will help you Android Bluetooth . But to get detailed information about using Bluetooth with Android along with manifest.xml you need to see this Android Bluetooth .

For permission you need.

 <manifest ... > <uses-permission android:name="android.permission.BLUETOOTH" /> ... </manifest> 
0
source

Source: https://habr.com/ru/post/891800/


All Articles