From c429e84e537bde477c83a57e00ed596e881268ef Mon Sep 17 00:00:00 2001 From: Alexander Bantyev Date: Wed, 5 Aug 2020 15:56:47 +0300 Subject: [PATCH] Add bluetooth battery port autodetection --- modules/hardware.nix | 2 ++ .../i3blocks/scripts/bluetooth_battery.py | 17 +++++++++++++---- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/modules/hardware.nix b/modules/hardware.nix index 22d9c43..f9da867 100644 --- a/modules/hardware.nix +++ b/modules/hardware.nix @@ -107,6 +107,7 @@ with deviceSpecific; { grub.device = "nodev"; grub.efiInstallAsRemovable = true; # NVRAM is unreliable }); + kernelPackages = pkgs.linuxPackages_latest; consoleLogLevel = 3; extraModprobeConfig = "options ec_sys write_support=1"; kernel.sysctl."vm.swappiness" = 0; @@ -141,6 +142,7 @@ with deviceSpecific; { support32Bit = true; extraConfig = '' load-module module-native-protocol-tcp auth-ip-acl=127.0.0.1 + load-module module-bluetooth-policy auto_switch=2 ''; }; } diff --git a/modules/workspace/i3blocks/scripts/bluetooth_battery.py b/modules/workspace/i3blocks/scripts/bluetooth_battery.py index efb34b0..2fba0dc 100755 --- a/modules/workspace/i3blocks/scripts/bluetooth_battery.py +++ b/modules/workspace/i3blocks/scripts/bluetooth_battery.py @@ -2,8 +2,6 @@ """ A python script to get battery level from Bluetooth headsets - -Shamelessly stolen from https://github.com/TheWeirdDev/Bluetooth_Headset_Battery_Level """ # License: GPL-3.0 @@ -63,17 +61,28 @@ def getATCommand(sock, line, device): return True +def find_rfcomm_port(device): + uuid="0000111e-0000-1000-8000-00805f9b34fb" + proto = bluetooth.find_service(address=device, uuid=uuid) + if len(proto) == 0: + print("Couldn't find the RFCOMM port number") + return 4 + else: + for j in range(len(proto)): + if 'protocol' in proto[j] and proto[j]['protocol'] == 'RFCOMM': + port = proto[j]['port'] + return port def main(): if (len(sys.argv) < 2): print("Usage: bl_battery.py [.PORT] ...") - print(" Port number is optional (default = 4)") + print(" Port number is optional") exit() else: for device in sys.argv[1:]: i = device.find('.') if i == -1: - port = 10 + port = find_rfcomm_port(device) else: port = int(device[i+1:]) device = device[:i]