Hi,
I am writing some code for android using python. I want to control the visibility of bluetooth ( i.e on or off of the bluetooth visibility) using some python script. Can you please help me ?

Dani AI

Generated

Quick, practical notes to control Bluetooth discoverability from Python on Android (follow-ups to , and ). Two reliable paths are common: 1) run a script on-device with SL4A/QPython for quick automation, or 2) build an APK (python-for-android / Kivy) and call Android APIs via Pyjnius for a proper app. Both approaches must use the system dialogs/intents (user consent) and the right permissions for the Android version.

SL4A (on-device) — build an Intent and start it from the Android facade instead of trying to fiddle with low-level adapter calls. Example (requests discoverability for 300s):

from android import Android
droid = Android()

intent = droid.makeIntent(
    "android.bluetooth.adapter.action.REQUEST_DISCOVERABLE",
    None, None,
    {"android.bluetooth.adapter.extra.DISCOVERABLE_DURATION": 300}
).result

droid.startActivityIntent(intent)

This uses SL4A's Intent helpers so the system shows the discoverable prompt. See SL4A intent APIs and the Android discoverability intent docs. (developer.qpython.org)

Packaged app (Kivy / pyjnius) — use pyjnius to call the Java APIs from Python and start the same intent from the Activity:

from jnius import autoclass
PythonActivity = autoclass('org.renpy.android.PythonActivity')
Intent = autoclass('android.content.Intent')
BluetoothAdapter = autoclass('android.bluetooth.BluetoothAdapter')

activity = PythonActivity.mActivity
intent = Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE)
intent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300)
activity.startActivity(intent)

Pyjnius + python-for-android is the robust route when you need full control and packaging. (pyjnius.readthedocs.io)

Caveats and troubleshooting: Android enforces user consent — discoverability is a system dialog (default 120s, max 300s by intent extra). Newer Androids changed Bluetooth permissions (BLUETOOTH_SCAN / BLUETOOTH_CONNECT / BLUETOOTH_ADVERTISE) and directly enabling/disabling programmatically is restricted on the latest SDKs (enable() behavior changed). Add the correct <uses-permission> lines when packaging and check Logcat for permission-denied messages. Test on a real device because emulators often lack full Bluetooth support. (developer.android.com)

Recommended Answers

All 2 Replies

All the Info you need, is accessible through the API documentation: menu > help > API help > bluetooth facade.

from android import Android

droid = Android()

check = droid.checkBluetoothState().result
if check != 1:
    droid.toggleBluetoothState(1,0)
    print "Bluetooth turned ON!"
    droid.bluetoothMakeDiscoverable(300)
    print "Bluetooth is Discoverable for 300sec now"

else:
    droid.toggleBluetoothState(0,0)
    print "Bluetooth turned OFF!"
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.