从ADB或启动时启动蓝牙?

tho*_*ogh 5 android bluetooth

是否可以在没有用户干预的情况下从ADB启动蓝牙?我试过了:

am start -a android.bluetooth.adapter.action.REQUEST_ENABLE
Run Code Online (Sandbox Code Playgroud)

但这需要用户按OK.和:

service call bluetooth 3
Run Code Online (Sandbox Code Playgroud)

什么都不做 在init.rc中启用蓝牙服务也不起作用.

service bluetoothd /system/bin/bluetoothd -n
    class main
    socket bluetooth stream 660 bluetooth bluetooth
    socket dbus_bluetooth stream 660 bluetooth bluetooth
    # init.rc does not yet support applying capabilities, so run as root and
    # let bluetoothd drop uid to bluetooth with the right linux capabilities
    group bluetooth net_bt_admin misc
    enabled
Run Code Online (Sandbox Code Playgroud)

而且我更喜欢亚行的命令.(如果有人想知道我需要它用于FCC测试.)

all*_*rog 3

如果它适合您,应用程序可以轻松更改蓝牙状态。代码非常简单,我相信你很熟悉:

BluetoothAdapter.getDefaultAdapter().enable()
Run Code Online (Sandbox Code Playgroud)

这可能是一个“无头”应用程序,其服务仅侦听特定意图。您可以安装它,然后广播激活器意图。

如果您希望应用程序不出现在应用程序“抽屉”中(仅在“设置”->“应用程序”中),则从文件中删除启动器和主要意图过滤器AndroidManifest.xml。也就是说,删除这些:

<intent-filter>
    <action android:name="android.intent.action.MAIN" />
    <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
Run Code Online (Sandbox Code Playgroud)

从这时起,您可以按照您在清单文件中定义的意图来启动应用程序/服务。例如,您可以使用操作为服务创建并注册一个意图过滤器com.company.service.bluetooth.ON,并使用 adb 命令启动它:

am startservice -a com.company.service.bluetooth.ON
Run Code Online (Sandbox Code Playgroud)

如果手机没有root,似乎没有其他方法可以做到这一点。如果扎根,service call bluetooth 3应该可以工作。

本教程描述了一个可行的解决方案:如何从 adb 启动 Android 应用程序 - 并切换蓝牙。他们使用这个应用程序:蓝牙开/关切换应用程序。