Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
# bt-stat
Polybar bluetooth module.

Polybar bluetooth module.

Displays connect device, the module is clickable to connect/disconnect devices.

## Installation/usage

1. Make sure bluetoothctl and Python >= 3.3 is installed
2. Download python script and modify the (list of) MAC addresses for bluetooth devices to fit your own stuff.
2. Download python script and modify the (list of) MAC addresses for bluetooth devices to fit your own stuff.
3. Create polybar config

```
[module/bluetooth]
type = custom/script
Expand All @@ -13,4 +18,6 @@ format-prefix = " "
format-underline = #5f6cd7
format-prefix-foreground = ${colors.foreground-alt}
exec = python3 ~/.config/polybar/bt-stat.py
```
click-left = python3 .config/polybar/bt-stat.py click
```

72 changes: 56 additions & 16 deletions bt-stat.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import subprocess
from shutil import which
import sys


class BTdevice():
Expand All @@ -13,19 +14,58 @@ def getProperties(self):
return self.name, self.mac


bt_devices = {BTdevice(name='XM3', mac='CC:98:8B:4A:07:31')}

if which("bluetoothctl") is not None:
bt_connected = "no"
for dev in bt_devices:
name, mac = dev.getProperties()
cmd_bt_connected = 'bluetoothctl info ' + mac + ' | grep -i connected | awk \'{print $2}\''
t = subprocess.Popen([cmd_bt_connected], stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell=True)
bt_connected = t.stdout.read().decode('utf-8').strip('\n')
if bt_connected == "yes":
print(name)
break
if bt_connected == "no":
print("None")
else:
print("bluetoothctl missing")
# Helper function to handle sending command and handle the result
def msghandler(args):
(message, _) = subprocess.Popen(args,stdout=subprocess.PIPE).communicate()
return message.decode('utf-8')

# List of devices, earlier device will be check first.
# So for performance reason place more common devices first,
# allowing the execution to terminated earlier.

bt_devices = [BTdevice(name='XM3', mac='CC:98:8B:4A:07:31')]

def main(argv):
if len(argv) == 0:
# Print connected device
if which("bluetoothctl") is not None:
bt_connected = "no"
for dev in bt_devices:
name, mac = dev.getProperties()
cmd_bt_connected = 'bluetoothctl info ' + mac + ' | grep -i connected | awk \'{print $2}\''
t = subprocess.Popen([cmd_bt_connected], stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell=True)
bt_connected = t.stdout.read().decode('utf-8').strip('\n')
if bt_connected == "":
print("{} is not connected".format(name))
exit()
if bt_connected == "yes":
print(name)
break
if bt_connected == "no":
print("None")
else:
print("bluetoothctl missing")

else:
# Click detected, handle input

# If connected, we want to disconnect and if successful we might
# as well turn off the bluetooth controller since we won't use it.
message = msghandler(['bluetoothctl', 'disconnect'])
if "Successful disconnected" in message:
msghandler(['bluetoothctl', 'power', 'off'])
exit()

# If disconnected, we want to make sure the controller is turned on
# and then try to connect the device.
# This is a bit slow but it does the job.
msghandler(['bluetoothctl', 'power', 'on'])
for dev in bt_devices:
(_, mac) = dev.getProperties()
message = msghandler(['bluetoothctl', 'connect', mac])
if "Connection successful" in message:
exit()


if __name__ == "__main__":
main(sys.argv[1:])