user2661518
user2661518

Reputation: 2745

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.

Upvotes: 25

Views: 84461

Answers (10)

wuseman
wuseman

Reputation: 1676

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

Upvotes: 29

Rasoul Miri
Rasoul Miri

Reputation: 12222

The best way to use settings put, svc, and actions.

It works for all Android versions and all devices.

val isOn =true

val command = "adb shell settings put global bluetooth_on ${
    if (isOn) {
        "1"
    } else {
        "0"
    }
} && adb shell svc bluetooth ${
    if (isOn) {
        "enable"
    } else {
        "disable"
    }
} && adb shell am start -a android.bluetooth.adapter.action.${
    if (isOn) {
        "REQUEST_ENABLE"
    } else {
        "REQUEST_DISABLE"
    }
} && adb shell am broadcast -a android.intent.action.BLUETOOTH_ENABLE --ez state $isOn"

Upvotes: 1

Safa
Safa

Reputation: 51

This might be a bit of a hacky solution but once you try to enable or disable bluetooth, you can use the right key action followed by the enter key action to select the allow button which is usually on the very right.

This should work even if your device is not rooted.

To enable:

adb shell am start -a android.bluetooth.adapter.action.REQUEST_ENABLE
adb shell input keyevent 22 (right)
adb shell input keyevent 22 (right)
adb shell input keyevent 66 (enter)

To disable:

adb shell am start -a android.bluetooth.adapter.action.REQUEST_DISABLE
adb shell input keyevent 22 (right)
adb shell input keyevent 22 (right)
adb shell input keyevent 66 (enter)

Upvotes: 5

VinPat
VinPat

Reputation: 51

For folks who are wondering from where did the numbers 6 and 8 came from. It came from this aidl file: https://android.googlesource.com/platform/system/bt/+/master/binder/android/bluetooth/IBluetoothManager.aidl Start counting from the first function listed in this aidl and you will see the enable function is the 6th one and the disable is the 8th one in the list.

Upvotes: 5

Hari Nandha
Hari Nandha

Reputation: 274

To enable

svc bluetooth enable

To disable

svc bluetooth disable

Upvotes: 14

Alex Vong
Alex Vong

Reputation: 513

As awm129 has pointed out, solutions using service call aren't device-independent. While I am unable to completely eliminate the use of service call, the following solution should be more device-independent:

To disable:

pm disable com.android.bluetooth

To enable:

pm enable com.android.bluetooth
service call bluetooth_manager 6

I hope someone will eventually find a service call-free solution.

Upvotes: 3

Seff
Seff

Reputation: 1807

On Xiaomi Mi 4i / MIUI 9:

To enable:

adb shell service call bluetooth_manager 8

To disable:

adb shell service call bluetooth_manager 10

This can also run within Andorid like:

service call bluetooth_manager 10

Upvotes: 1

Aiden
Aiden

Reputation: 31

To enable:

adb shell service call bluetooth_manager 6

To disable:

adb shell service call bluetooth_manager 9

Tested and worked on a Samsung Galaxy S7.

Upvotes: 3

To run commands listed in previous comment, you need to be root:

adb root

Enable:

adb shell service call bluetooth_manager 6

Disable:

adb shell service call bluetooth_manager 8

Upvotes: 3

Ravi Ranjan
Ravi Ranjan

Reputation: 449

To enable:

adb shell service call bluetooth_manager 6

To disable:

adb shell service call bluetooth_manager 8

Upvotes: 20

Related Questions