- Android BluetoothAdapter enable()
- Introduction
- Syntax
- Example
- Related
- android enable disable bluetooth via command line
- 10 Answers 10
- Enable bluetooth via cmd
- Disable bluetooth cmd
- Print current bluetooth status via settings
- Enable Bluetooth via settings
- Disable bluetooth settings
- Enable bluetooth via content
- Disable bluetooth content
- Enable Bluetooth
- Disable Bluetooth
- Enable bluetooth via activity manager
- Disable bluetooth via activity manager
- Enable/Disable bluetooth via keyevents
- Set up Bluetooth
- Kotlin
- Java
- Kotlin
- Java
Android BluetoothAdapter enable()
Android BluetoothAdapter enable() Turn on the local Bluetooth adapter—do not use without explicit user action to turn on Bluetooth.
Introduction
Turn on the local Bluetooth adapter—do not use without explicit user action to turn on Bluetooth.
This powers on the underlying Bluetooth hardware, and starts all Bluetooth system services.
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.
This is an asynchronous call: it will return immediately, and clients should listen for #ACTION_STATE_CHANGED to be notified of subsequent adapter state changes.
If this call returns true, then the adapter state will immediately transition from #STATE_OFF to #STATE_TURNING_ON, and some time later transition to either #STATE_OFF or #STATE_ON.
If this call returns false then there was an immediate problem that will prevent the adapter from being turned on — such as Airplane mode, or the adapter is already turned on.
Syntax
The method enable() from BluetoothAdapter is declared as:
@RequiresPermission(Manifest.permission.BLUETOOTH_ADMIN) public boolean enable()
The method enable() returns true to indicate adapter startup has begun, or false on immediate error
Example
The following code shows how to use Java BluetoothAdapter enable()
import android.app.Activity; import android.bluetooth.BluetoothAdapter; import android.os.Bundle; import android.widget.Toast; public class BluetoothToogleActivity extends Activity < @Override/* w w w . d e m o 2 s . c om */ public void onCreate(Bundle savedInstanceState) < super.onCreate(savedInstanceState); BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); if (mBluetoothAdapter != null) < if (!mBluetoothAdapter.isEnabled()) < mBluetoothAdapter.enable(); Toast.makeText(getApplicationContext(), getResources().getString(R.string.bluetooth_on), Toast.LENGTH_LONG).show(); > else < mBluetoothAdapter.disable(); Toast.makeText(getApplicationContext(), getResources().getString(R.string.bluetooth_off), Toast.LENGTH_LONG).show(); >> finish(); > >
import android.app.Activity; import android.bluetooth.BluetoothAdapter; import android.os.Bundle; import android.widget.TextView; import android.widget.Toast; public class MyActivity extends Activity < /**// w w w .d e mo 2 s. c o m * Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) < super.onCreate(savedInstanceState); setContentView(R.layout.main); BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); TextView textView1 = (TextView) findViewById(R.id.text1); String status = ""; if (bluetoothAdapter != null) < if (!bluetoothAdapter.isEnabled()) < bluetoothAdapter.enable(); > String myDeviceAddress = bluetoothAdapter.getAddress(); String myDeviceName = bluetoothAdapter.getName(); status = myDeviceName + ": " + myDeviceAddress; > textView1.setText(textView1.getText() + "\n" + status); Toast.makeText(this, status, Toast.LENGTH_LONG).show(); > >
import android.Manifest; import android.bluetooth.BluetoothAdapter; import android.content.Intent; import android.content.pm.PackageManager; import android.support.v4.app.ActivityCompat; import android.support.v4.app.ShareCompat; import android.support.v4.content.ContextCompat; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.widget.Toast; import static android.os.Build.VERSION_CODES.M; public class MainActivity extends AppCompatActivity < @Override// w w w . d e m o2 s. c om protected void onCreate(Bundle savedInstanceState) < super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); if (mBluetoothAdapter == null) < Toast.makeText(this, "Bluetooth is not available", Toast.LENGTH_SHORT).show(); > else if (mBluetoothAdapter.isEnabled()) < Toast.makeText(this, "Bluetooth is enabled", Toast.LENGTH_SHORT).show(); > else < int permissionCheck = ContextCompat.checkSelfPermission(this, Manifest.permission.BLUETOOTH_ADMIN); if (permissionCheck == PackageManager.PERMISSION_GRANTED) < if (mBluetoothAdapter.enable()) Toast.makeText(this, "Enabled Bluetooth successfully.", Toast.LENGTH_SHORT).show(); > else < Toast.makeText(this, "Failed to enable Bluetooth.", Toast.LENGTH_SHORT).show(); > > else < Intent intentBTEnabled = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); int REQUEST_ENABLE_BT = 101; startActivityForResult(intentBTEnabled, REQUEST_ENABLE_BT); > > > >
Related
demo2s.com | Email: | Demo Source and Support. All rights reserved.
android enable disable bluetooth via command line
I am trying to enable disable bluetooth on android device using command line. I can enable it using adb shell am start -a android.bluetooth.adapter.action.REQUEST_ENABLE But it prompts user to ‘allow’ or ‘deny’. I also see there is option to launch ble settings first like adb shell am start -a android.settings.BLUETOOTH_SETTINGS and then enable disable adb shell input keyevent ** But it won’t be device independent.
10 Answers 10
Updated: 2023-03-10 — Android 12/13
Enable bluetooth via cmd
adb shell cmd bluetooth_manager enable
Disable bluetooth cmd
adb shell cmd bluetooth_manager disable
Updated: 2019-06-22: — Android 11
Print current bluetooth status via settings
adb shell settings get global bluetooth_on adb shell settings list global |grep ^bluetooth_on
Enable Bluetooth via settings
adb shell settings put global bluetooth_disabled_profiles 1
Disable bluetooth settings
adb shell settings put global bluetooth_disabled_profiles 0
Enable bluetooth via content
adb shell content insert \ --uri content://settings/global \ --bind name:s:bluetooth_disabled_profiles \ --bind value:s:1 --user 0
Disable bluetooth content
adb shell content insert \ --uri content://settings/global \ --bind name:s:bluetooth_disabled_profiles \ --bind value:s:0 --user 0
Android 11/12/13 and older versions
Enable Bluetooth
adb shell settings put global bluetooth_on 1
Disable Bluetooth
adb shell settings put global bluetooth_on 0
Enable bluetooth via activity manager
adb shell am broadcast \ -a android.intent.action.BLUETOOTH_ENABLE --ez state true
Disable bluetooth via activity manager
adb shell am broadcast \ -a android.intent.action.BLUETOOTH_ENABLE --ez state false
Enable/Disable bluetooth via keyevents
adb shell am start \ -a android.settings.BLUETOOTH_SETTINGS \ adb shell input keyevent 19 adb shell input keyevent 23
On Huawei P30 Pro with Android P even your new solution doesn’t work. When I use adb shell settings put global bluetooth_disabled_profiles 1 , it disables bluetooth (as opposite to what you say) and adb shell settings put global bluetooth_disabled_profiles 0 does nothing, it doesn’t enable nor disable it.
See if they have changed the name so it might be bluetooth_on instead of bluetooth_disabled_profiles. If you type ‘adb shell settings list global|grep -i bluetooth’ you should get some alternatives what you can try if above dosn’t work so just change name and value if needed, above works fine on Huawei P9, Samsung S8 and Huawei P8 that’s all I have tried on with Android 8.0 & Android 9.0. Regards
for me, all of these methods from 2019 edit (both disable and enable) disable bluetooth for just a very quick amount of time. It rather looks like a reset. Samsung A50, Android 10 (kernel 4.14 )
adb shell settings put global bluetooth_on 0 worked for me in android 12.1 — but is there anyway that I can make it non-persistent so that behavior is same after each boot? When settings put global bluetooth_on 0 — it retains the value and next time I reboot, I notice that boot_completed never gets set to 1. Unless I wipe /data partition and restart again and re-put settings put global bluetooth_on 0 .
adb shell service call bluetooth_manager 6
adb shell service call bluetooth_manager 8
We are trying to enable and disable Bluetooth before we start our automation test using adb commands Enable adb shell service call bluetooth_manager 6 Disable adb shell service call bluetooth_manager 9 We are getting the Bluetooth status using this command adb shell settings get global bluetooth_on Are there any other work around to enable and disable the bluetooth using APPIUM java because these commands working in some devices but not in all the device. Any solution for this? The same commands working in Android 5 (Lollipop)
using static method codes as suggested here will never work across all devices. These codes can and do change on a per device basis depending on vendor and OEM modifications.
Set up Bluetooth
Before your app can communicate over Bluetooth or Bluetooth Low Energy, you need to verify that Bluetooth is supported on the device, and if it is, ensure that it is enabled. Note that this check is only necessary if the android:required attribute in the manifest file entry is set to false .
If Bluetooth isn’t supported, then you should gracefully disable any Bluetooth features. If Bluetooth is supported, but disabled, then you can request that the user enable Bluetooth without leaving your app.
The first step is adding the Bluetooth permissions to your manifest file in order to use the following APIs.
Once the permissions are in place, Bluetooth setup is accomplished in two steps using the BluetoothAdapter :
- Get the BluetoothAdapter . The BluetoothAdapter is required for any and all Bluetooth activity. The BluetoothAdapter represents the device’s own Bluetooth adapter (the Bluetooth radio). To get a BluetoothAdapter , you first need to have a Context . Use this context to obtain an instance of the BluetoothManager system service. Calling BluetoothManager#getAdapter will give you a BluetoothAdapter object. If getAdapter() returns null, then the device doesn’t support Bluetooth. For example:
Kotlin
val bluetoothManager: BluetoothManager = getSystemService(BluetoothManager::class.java) val bluetoothAdapter: BluetoothAdapter? = bluetoothManager.getAdapter() if (bluetoothAdapter == null) < // Device doesn't support Bluetooth >
Java
BluetoothManager bluetoothManager = getSystemService(BluetoothManager.class); BluetoothAdapter bluetoothAdapter = bluetoothManager.getAdapter(); if (bluetoothAdapter == null) < // Device doesn't support Bluetooth >
Kotlin
if (bluetoothAdapter?.isEnabled == false)
Java
if (!bluetoothAdapter.isEnabled())
A dialog appears requesting user permission to enable Bluetooth, as shown in figure 1. If the user grants permission, the system begins to enable Bluetooth, and focus returns to your app once the process completes (or fails).
Figure 1. The enabling Bluetooth dialog.
The REQUEST_ENABLE_BT constant passed to startActivityForResult() is a locally-defined integer that must be greater than or equal to 0. The system passes this constant back to you in your onActivityResult() implementation as the requestCode parameter.
If enabling Bluetooth succeeds, your activity receives the RESULT_OK result code in the onActivityResult() callback. If Bluetooth was not enabled due to an error (or the user responded «Deny») then the result code is RESULT_CANCELED .
Optionally, your app can also listen for the ACTION_STATE_CHANGED broadcast intent, which the system broadcasts whenever the Bluetooth state changes. This broadcast contains the extra fields EXTRA_STATE and EXTRA_PREVIOUS_STATE , containing the new and old Bluetooth states, respectively. Possible values for these extra fields are STATE_TURNING_ON , STATE_ON , STATE_TURNING_OFF , and STATE_OFF . Listening for this broadcast can be useful if your app needs to detect runtime changes made to the Bluetooth state.
Tip: Enabling discoverability automatically enables Bluetooth. If you plan to consistently enable device discoverability before performing Bluetooth activity, you can skip step 2 in the earlier steps.
Once Bluetooth is enabled on the device, you can use both Bluetooth classic and Bluetooth Low Energy.
Content and code samples on this page are subject to the licenses described in the Content License. Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.
Last updated 2023-02-01 UTC.