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
public class BluetoothManager { public static boolean isBluetoothSupported() { return BluetoothAdapter.getDefaultAdapter() != null ? true : false; } public static boolean isBluetoothEnabled() { BluetoothAdapter bluetoothAdapter = BluetoothAdapter .getDefaultAdapter(); if (bluetoothAdapter != null) { return bluetoothAdapter.isEnabled(); } return false; } 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 { private static final int REQUEST_CODE_BLUETOOTH_ON = 1313; 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(); } } private void turnOnBluetooth() {
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