【问题标题】:android enable disable bluetooth via command lineandroid通过命令行启用禁用蓝牙
【发布时间】:2016-09-12 13:23:54
【问题描述】:

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

我可以使用

启用它

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 处理提示吗?

标签: shell terminal bluetooth command adb


【解决方案1】:

启用:

adb shell service call bluetooth_manager 6

禁用:

adb shell service call bluetooth_manager 8

【讨论】:

  • 这两个命令都不适合我。我正在使用三星 S4 设备及其 Android 4.2。
  • 我们正在尝试在使用 adb 命令开始自动化测试之前启用和禁用蓝牙 Enable adb shell service call bluetooth_manager 6 Disable adb shell service call bluetooth_manager 9 我们正在使用此命令获取蓝牙状态 adb shell settings get global bluetooth_on 是否有任何其他解决方法可以使用 APPIUM java 启用和禁用蓝牙,因为这些命令在某些设备中有效,但并非在所有设备中有效。有什么解决办法吗?在 Android 5 (Lollipop) 中工作的相同命令
  • 使用此处建议的静态方法代码永远不会在所有设备上工作。根据供应商和 OEM 的修改,这些代码可以并且确实会根据每个设备进行更改。
  • 有文件说明6和8状态是什么意思吗?
  • 这适用于 Android v7.1.1 上的 Moto E4 - 但仅适用于 root:adb shell su -c "service call bluetooth_manager 8"
【解决方案2】:

蓝牙状态:

adb shell settings get global bluetooth_on 

adb shell settings list global |grep ^bluetooth_on

启用蓝牙

adb shell settings put global bluetooth_on 1

禁用蓝牙

adb shell settings put global bluetooth_on 0

Via am - 使用 enable 而不是请求

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

通过按键事件

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

编辑 2019-06-22:

终于找到了一种在没有 root 的情况下在 Android 8.0 和更新版本上打开/关闭蓝牙的方法,“bluetooth_on”似乎不再适用于最新的 android 版本:

启用蓝牙 - 无需 root

  adb shell settings put global bluetooth_disabled_profiles 1 

禁用蓝牙 - 无需 root

  adb shell settings put global bluetooth_disabled_profiles 0

既然上述工作正常,那么内容当然也工作正常

启用蓝牙 - 无需 root

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

禁用蓝牙 - 无需 root:

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

【讨论】:

  • 在搭载 Android P 的华为 P30 Pro 上,即使您的新解决方案也不起作用。当我使用adb shell settings put global bluetooth_disabled_profiles 1 时,它会禁用 蓝牙(与您所说的相反)而adb shell settings put global bluetooth_disabled_profiles 0 什么都不做,它既不启用也不禁用它。
  • 查看他们是否更改了名称,因此它可能是 bluetooth_on 而不是 bluetooth_disabled_profiles。如果您键入“adb shell settings list global|grep -i bluetooth”,您应该获得一些替代方法P8 这就是我在 Android 8.0 和 Android 9.0 上尝试过的全部。问候
  • 作为参考,adb shell settings put global bluetooth_on 1 在三星 S7 上工作过
  • 对我来说,从 2019 年开始,所有这些方法(禁用和启用)都会在很短的时间内禁用蓝牙。它看起来像一个重置。三星 A50,Android 10(内核 4.14)
【解决方案3】:

启用

svc bluetooth enable

禁用

svc bluetooth disable

【讨论】:

    【解决方案4】:

    对于那些想知道数字 6 和 8 来自哪里的人。它来自这个aidl文件:https://android.googlesource.com/platform/system/bt/+/master/binder/android/bluetooth/IBluetoothManager.aidl 从本文档中列出的第一个函数开始计数,您会看到启用函数是列表中的第 6 个函数,而禁用函数是列表中的第 8 个函数。

    【讨论】:

      【解决方案5】:

      要运行之前评论中列出的命令,您需要是 root:

      adb root

      启用:

      adb shell service call bluetooth_manager 6

      禁用:

      adb shell service call bluetooth_manager 8

      【讨论】:

        【解决方案6】:

        启用:

        adb shell service call bluetooth_manager 6
        

        禁用:

        adb shell service call bluetooth_manager 9
        

        在三星 Galaxy S7 上测试和使用。

        【讨论】:

          【解决方案7】:

          正如 awm129 所指出的,使用 service call 的解决方案不是独立于设备的。虽然我无法完全消除 service call 的使用,但以下解决方案应该更加独立于设备:

          禁用:

          pm disable com.android.bluetooth
          

          启用:

          pm enable com.android.bluetooth
          service call bluetooth_manager 6
          

          我希望有人最终会找到service call-free 的解决方案。

          【讨论】:

            【解决方案8】:

            在小米米 4i / MIUI 9 上:

            启用:

            adb shell 服务调用 bluetooth_manager 8

            禁用:

            adb shell 服务调用 bluetooth_manager 10

            这也可以在 Andorid 中运行,例如:

            服务调用 bluetooth_manager 10

            【讨论】:

            • Poco F1 / MIUI 10.2 / Pie:5 启用,8 禁用
            【解决方案9】:

            这可能是一个有点笨拙的解决方案,但是一旦您尝试启用或禁用蓝牙,您可以使用右键操作,然后输入键操作来选择通常位于最右侧的允许按钮。

            即使您的设备没有植根,这也应该可以工作。

            启用:

            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)
            

            【讨论】:

              【解决方案10】:

              我没有足够的声望点来添加评论,所以我正在跟进 TripeHound 对 wuseman 回答的评论。

              我尝试了TripeHound的设置蓝牙全局设置的方式(

              adb shell settings put global bluetooth_on 1
              

              ) 在三星 Galaxy Tab A (SAMSUNG SM-T515 running Android 10, Kernel 4.4.177) 上,它似乎确实有效。但是,打开快速设置菜单时蓝牙图标不会显示更改,进入完整设置菜单时也不会显示更改。

              也就是说,如果我在平板电脑确实会以正确的状态重新启动后立即重新启动,那么我不确定是否有必要以某种方式重新读取全局设置以确保蓝牙无线电实际上已关闭。

              欢迎评论!

              【讨论】:

              • 谢谢,能否请您编辑一下以体现 SO 礼仪?
              猜你喜欢
              • 2017-12-04
              • 2014-07-12
              • 2016-05-08
              • 1970-01-01
              • 1970-01-01
              • 2016-05-07
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多