• Stars
    star
    764
  • Rank 59,449 (Top 2 %)
  • Language
    Python
  • License
    GNU General Publi...
  • Created about 5 years ago
  • Updated 7 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A python script to get battery level from Bluetooth headsets

🔋🎧 Bluetooth Headset Battery Level

This is a python script to fetch the battery charge level of some Bluetooth headsets.

You need python 3.6 or newer to run the script.

Table of Contents

▶️ How to run

There are four options:

Option 1: Install from PyPI

Please ensure you have the BlueZ and python libraries and header files if you are using Ubuntu/Debian based distros:

sudo apt install libbluetooth-dev python3-dev

If you are using OpenSUSE, you will need to install bluez, bluez-devel, python3-devel, python3-pybluez

Then, install with pip:

pip3 install bluetooth_battery

bluetooth_battery BT_MAC_ADDRESS_1 ...

You can input addresses for as many devices as you want separated by space.

the dependency pybluez should be installed automatically, but if not, you may need to install it manually


Option 2: Download this repository

cd Bluetooth_Headset_Battery_Level
chmod +x bluetooth_battery.py

./bluetooth_battery.py BT_MAC_ADDRESS_1 ...

make sure you have python-pybluez or python3-pybluez or python3-bluez installed on your system.


Option 3: Docker

You must have docker installed on your system. Clone the repository using git clone command. Then run:

cd Bluetooth_Headset_Battery_Level
docker build -t bluetooth_battery_level .

Once the build is complete you can use the below command to run the program:

docker run --rm -ti --privileged --net=host bluetooth_battery_level "BT_MAC_ADDRESS"

Option 4: AUR

You must have AUR access enabled on your Arch or Manjaro machine. You can install this library using

yay -S python-bluetooth-battery

the dependency pybluez should be installed automatically, but if not, you may need to install it manually
This can be done with pip3 install git+https://github.com/pybluez/pybluez@master.


Library usage

To use this as a library, simply install it using pip or AUR (see above) or require it in your Pipfile.
You can then

from bluetooth_battery import BatteryStateQuerier, BatteryQueryError, BluetoothError
                                                   # only for error handling

and query the Battery State as follows:

# Autodetects SPP port
query = BatteryStateQuerier("11:22:33:44:55:66")
# or with given port
query = BatteryStateQuerier("11:22:33:44:55:66", "4")

result = int(query)  # returns integer between 0 and 100
# or
result = str(query)  # returns "0%".."100%"

As errors can occur in a wireless system, you probably want to handle them:

try:
    query = BatteryStateQuerier("11:22:33:44:55:66")  # Can raise BluetoothError when autodetecting port
    str(query)                                        # Can raise BluetoothError when device is down or port is wrong
                                                      # Can raise BatteryQueryError when the device is unsupported
except BluetoothError as e:
    # Handle device is offline
    ...
except BatteryQueryError as e:
    # Handle device is unsupported
    ...

GNOME Extension

There is also a GNOME extension for integrating this program with GNOME desktop environment: https://extensions.gnome.org/extension/3991/bluetooth-battery/

Budgie Applet

There is also a Budgie extension for integrating this program with Budgie desktop environment: https://github.com/GaLaXy102/budgie-bluetooth-battery-applet

Finding MAC address

There are a variety of utilities that can find the MAC address of your bluetooth device. Here is one, this command is in the bluez package, and the given argument gets a list of all devices it knows about, even if not currently available.

$ bluetoothctl devices
Device E8:AB:FA:XX:XX:XX iTeknic IK-BH002
Device D0:77:14:XX:XX:XX Barak's Moto X4
Device E8:AB:FA:XX:XX:XX iTeknic IK-BH002

The 1st and 3rd would be relevant here, as those are headsets.

This shows devices that are actually connected.

$ bluetoothctl info
Device E8:AB:FA:XX:XX:XX (public)
	Name: iTeknic IK-BH002
	...

So you can use

bluetooth_battery.py $(bluetoothctl info | awk '/^Device/ {print $2}')

to query the battery of all connected devices.

It didn't work?

You can set the port number manually by adding a dot at the end of mac address, like this: 00:00:00:00:00:00.3

Try port numbers 1 to 30 to find the one that works for your device. (wait a few seconds between each try)

If that didn't work, disconnect your device first, and then try again.

Still doesn't work?

Please consider that this script doesn't guarantee to support every bluetooth device.

You can open a new issue for discussion or check the existing ones for more information.

Tested on

  • ArchLinux (5.6.14)
  • Manjaro (5.14.10)
  • NixOS 20.09 (20.09.2386.ae1b121d9a6)
  • Debian GNU/Linux (bullseye 5.9)
  • Ubuntu/Linux (Focal Fossa 20.04.1)
  • openSUSE (Leap 15.3)

🤝 Thanks

Special thanks to:

@clst: For spreading the word!

@bhepple: For his research on fixing the important bug

@balsoft: For thinking outside the box (finding my big mistake)

@martin-beran: For making it easy to set the port number

@Bobo1239: For adding support for Samsung galaxy buds

@keystroke3: For adding multiple device support to the script

@jadia: For docker container support

@cheriimoya: For refactoring the code and providing NixOS packages

@MichalW: For making the GNOME shell extension

@GaLaXy102: For re-writing the code, making it cleaner and usable as a library

❤️ And everyone else that pointed out the issues or helped me with writing the code or testing it.

📜 License

This project is a free software licensed under GPL-3.0 or newer. For more information see LICENSE

More Repositories

1

lyrics-finder-gnome-ext

Finding lyrics has never been easier. Just play some music !
JavaScript
34
star
2

arch-wiki-bot

Fastest way to search through ArchWiki pages in telegram !
Python
27
star
3

somafm-radio-gnome-ext

SomaFm internet radio gnome extension
JavaScript
22
star
4

JeyBoard

A simple powerful virtual keyboard for any desktop operating system running java
Java
19
star
5

Authomata

Two-factor authentication GUI app for Linux
D
18
star
6

Vodga

Vodga is an OpenVPN client for Linux with a GUI (Gtk) and a CLI, written in Go
Go
16
star
7

nautilus-send-to-bluetooth

add "send to bluetooth device" option in nautilus right-click menu
Python
15
star
8

SnakeD

A simple snake game using D and ncurses.
D
13
star
9

Calcool

A cool calculator that works in your terminal
D
9
star
10

JTaskbarLib

The first library for using windows taskbar and window features in Java
Java
7
star
11

DontPanic

An experimental x86_64 OS kernel written in D
D
5
star
12

Emuze

Hobby emulator (for now only chip8) written in C++20
C++
5
star
13

dotfiles

My .dotfiles
Vim Script
4
star
14

BestoonGui

MultiPlatform GUI for bestoon project
Java
4
star
15

apk-preview

A graphical user interface to see APK file details & icon
Python
4
star
16

Censor-Boat

CensorBoat helps you to remove inappropriate or unneeded parts of a movie
Python
3
star
17

user-auth-django

A simple user authentication django app (Educational)
Python
3
star
18

MeShell

MeShell is our OS-Lab exercise project
C
2
star
19

calcool-rust

A CLI calculator and library using pratt parsing and rust
Rust
2
star
20

theweirddev.github.io

SCSS
2
star
21

BingeWatch

BingeWatch is a Linux Gtk app that helps you organize and watch your movies and TV shows
D
2
star
22

TheWeirdDev

Profile
1
star
23

FiDecrypt

E-Book DRM remover (decryptor)
TypeScript
1
star
24

GThemeInstaller

Easily install themes, icons and wallpapers from gnome-looks.org (DEPRECATED)
Ruby
1
star
25

task-txt

tui task management app for people who don't like task warrior
Rust
1
star
26

nautilus-correct-size

A simple nautilus script that shows file/folder sizes in power of 2 (1 MB = 1024 KB)
Shell
1
star
27

Exercise1

Code for exercise
Python
1
star
28

clear-notifs-gnome-ext

JavaScript
1
star
29

xpos

A handy X11 tool for showing the mouse position next to it's pointer
C
1
star
30

chromium-30nama-links

A simple chromium extension to change '30nama*.*/*' links to '30nama.us/*'
JavaScript
1
star
31

Chunky

Chunky is a lightweight http(s) proxy designed to circumvent censorship
Rust
1
star
32

SubtitleGet

Download movie subtitles with a simple python script
Python
1
star