androidbluetoothadbandroid-sourceandroid-13

Is there any way to turn on bluetooth using cmd line (adb shell preferably)


I tried turning on bluetooth using cmd bluetooth_manager enable:
But i see its failing a check in BluetoothManagerService.java::checkIfCallerIsForegroundUser Since the caller is shell (UID 2000). So Framework is not allowing to turn on Bluetooth

====================================
Also tried using service call bluetooth_manager 3 (call for enable api)
This is failing on Null pointer reference call from BluetoothManager

====================================
Tried with adb shell am start -a android.bluetooth.adapter.action.REQUEST_ENABLE
This is asking for allow or deny permission, it can be automated but i am looking for any other options if there are


Solution

  • Enabling Bluetooth via adb shell without user interaction or root access is restricted by Android's security policies. Here's why your attempts fail and alternatives:

    Why Attempts Fail Below Reasons

    1. bluetooth_manager enable: Fails due to BluetoothManagerService enforcing checkIfCallerIsForegroundUser, blocking non-foreground users like adb shell.
    2. service call bluetooth_manager 3: Likely triggers a NullPointerException since required states or dependencies aren't initialized.
    3. adb shell am start: Shows a permission dialog that requires user action.

    Alternatives

    1. Automate the Dialog: Use adb or tools like uiautomator to simulate button presses (adb shell input keyevent KEYCODE_DPAD_CENTER).
    2. Root Access: If rooted, enable Bluetooth with:

    adb root

    adb shell service call bluetooth_manager 6

    1. Custom App: A system-signed app can use BluetoothAdapter.enable() without user interaction.
    2. Developer Options: Relax restrictions with "Disable Permission Monitoring" (only for development devices).

    Without root, automation or creating a privileged app is your best option.