通过命令行启用或禁用安卓蓝牙

24

我正在尝试使用命令行在安卓设备上启用或禁用蓝牙。

我可以使用以下命令启用它:

adb shell am start -a android.bluetooth.adapter.action.REQUEST_ENABLE

但是它会提示用户“允许”或“拒绝”。

我也看到有选项可以先启动BLE设置,如下:

adb shell am start -a android.settings.BLUETOOTH_SETTINGS

然后通过以下命令启用或禁用:adb shell input keyevent **

但这不会是设备无关的。


你能找到如何使用adb处理提示吗? - brij
10个回答

26

更新时间: 2023-03-10 - Android 12/13

通过cmd启用蓝牙

adb shell cmd bluetooth_manager enable

禁用蓝牙 cmd

adb shell cmd bluetooth_manager disable

更新:2019-06-22: - Android 11

通过settings打印当前蓝牙状态

adb shell settings get global bluetooth_on 
adb shell settings list global |grep ^bluetooth_on

通过设置启用蓝牙

adb shell settings put global bluetooth_disabled_profiles 1 

禁用蓝牙 设置

adb shell settings put global bluetooth_disabled_profiles 0

通过content启用蓝牙

adb shell content insert \
  --uri content://settings/global \
  --bind name:s:bluetooth_disabled_profiles \
  --bind value:s:1 --user 0 

禁用蓝牙 内容

adb shell content insert \
  --uri content://settings/global \
  --bind name:s:bluetooth_disabled_profiles \
  --bind value:s:0 --user 0 

Android 11/12/13以及早期版本

启用蓝牙

adb shell settings put global bluetooth_on 1

禁用蓝牙

adb shell settings put global bluetooth_on 0

通过activity manager启用蓝牙

adb shell am broadcast \
  -a android.intent.action.BLUETOOTH_ENABLE --ez state true

通过活动管理器禁用蓝牙

adb shell am broadcast \
  -a android.intent.action.BLUETOOTH_ENABLE --ez state false

通过 keyevents 启用/禁用蓝牙

adb shell am start \
  -a android.settings.BLUETOOTH_SETTINGS  \
  adb shell input keyevent 19
  adb shell input keyevent 23

1
在华为P30 Pro上使用Android P,即使您的新解决方案也无法正常工作。当我使用adb shell settings put global bluetooth_disabled_profiles 1时,它会禁用蓝牙(与您所说的相反),而adb shell settings put global bluetooth_disabled_profiles 0则无效,既不启用也不禁用它。 - Robyer
1
看看他们是否更改了名称,因此可能是bluetooth_on而不是bluetooth_disabled_profiles。如果您键入“adb shell settings list global | grep -i bluetooth”,则应该会得到一些替代方案,如果上述方法不起作用,则可以尝试更改名称和值,以上在华为P9、三星S8和华为P8上使用Android 8.0和Android 9.0都可以正常工作。谢谢。 - wuseman
作为参考,adb shell settings put global bluetooth_on 1 在 Samsung S7 上可行。 - TripeHound
1
对我来说,2019年编辑的所有这些方法(包括禁用和启用)只会短暂地禁用蓝牙。它看起来更像是重置。三星A50,Android 10(内核4.14)。 - Antek
adb shell settings put global bluetooth_on 0 在安卓12.1上对我有效 - 但有没有办法使其非持久化,以便每次启动后行为相同?当执行 settings put global bluetooth_on 0 后,它会保留该值,下次重启时,我注意到 boot_completed 永远不会设置为1。除非我擦除 /data 分区并重新启动,然后重新执行 settings put global bluetooth_on 0 - mohdyusuf

18

启用:

adb shell service call bluetooth_manager 6

禁用:

adb shell service call bluetooth_manager 8

这两个命令对我都没有起作用。我正在使用三星S4设备和它的Android 4.2版本。 - Codelearner
我们正在尝试使用adb命令在开始自动化测试之前启用和禁用蓝牙 启用adb shell service call bluetooth_manager 6禁用adb shell service call bluetooth_manager 9我们使用以下命令获取蓝牙状态adb shell settings get global bluetooth_on是否有其他方法可以使用APPIUM java启用和禁用蓝牙,因为这些命令在某些设备上有效,但在所有设备上都无效。 有解决方案吗?相同的命令适用于Android 5(棒棒糖) - alk1985
2
使用静态方法代码,如此处建议的那样,将无法在所有设备上正常工作。这些代码可以并且会根据供应商和OEM的修改而在每个设备上发生变化。 - awm129
有没有文件说明6和8状态的含义? - AIB
这个命令适用于安卓7.1.1上的Moto E4手机,但需要root权限才能生效: adb shell su -c "service call bluetooth_manager 8" - Eugen

12

启用

svc bluetooth enable

禁用

svc bluetooth disable

谢谢,这适用于Android 12 Moto G22。 - odgatelmand
谢谢,这对我在Android 12上有效。 - undefined

5
这可能是一个不太正式的解决方案,但是一旦你尝试启用或禁用蓝牙,你可以使用右箭头键动作,然后使用回车键动作选择通常位于最右边的"允许"按钮。
即使您的设备没有root,这也应该起作用。
要启用:
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)

禁用:

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)

4

3

启用:

adb shell service call bluetooth_manager 6

禁用方法:

adb shell service call bluetooth_manager 9

已在三星Galaxy S7上进行测试并工作正常。


3

要运行上面评论中列出的命令,您需要成为root用户:

adb root

启用蓝牙:

adb shell service call bluetooth_manager 6

禁用蓝牙:

adb shell service call bluetooth_manager 8


当我运行此命令时,我会得到一个奇怪的输出(类似于xxd生成的输出),其中包含以下消息:尝试调用虚拟方法“java.lang.String android.content.AttributionSource.getPackageName()”时,在空对象引用上。 。 。位于com.android.server.BluetoothManagerService.enableNoAutoConnect(BluetoothManagerService.java:1136)。。。在android.bluetooth.IBluetoothManager $ Stub.onTransact(IBluetoothManager.java:294). 。 。在android.os.Binder.execTransactInterval(Binder.java:1184)中执行Binder.execTransact(Binder.java:1143)... - Matthew

2
如awm129所指出的,使用“service call”解决方案并不是设备无关的。虽然我无法完全消除“service call”的使用,但以下解决方案应该更具设备兼容性:
禁用方法:
pm disable com.android.bluetooth

启用:

pm enable com.android.bluetooth
service call bluetooth_manager 6

我希望最终有人能找到一种不需要进行“服务调用”的解决方案。原始回答:Original Answer

1

在小米4i / MIUI 9上:

启用蓝牙:

adb shell service call bluetooth_manager 8

禁用蓝牙:

adb shell service call bluetooth_manager 10

这也可以在Android中运行,如下所示:

service call bluetooth_manager 10


Poco F1 / MIUI 10.2 / Pie:5 启用,8 禁用。 - Seff

1

使用settings putsvcactions的最佳方法。

它适用于所有Android版本所有设备

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"

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接