Skip to content

Commit

Permalink
Cleaner connection method for bt-tether
Browse files Browse the repository at this point in the history
Signed-off-by: Jeroen Oudshoorn <[email protected]>
  • Loading branch information
jayofelony committed Dec 29, 2024
1 parent 3e5e2bf commit 3e5d802
Showing 1 changed file with 12 additions and 8 deletions.
20 changes: 12 additions & 8 deletions pwnagotchi/plugins/default/bt-tether.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,21 @@

class BTTether(plugins.Plugin):
__author__ = 'Jayofelony'
__version__ = '1.1'
__version__ = '1.2'
__license__ = 'GPL3'
__description__ = 'A new BT-Tether plugin'

def __init__(self):
self.ready = False
self.options = dict()
self.status = '-'
self._config = dict()

def on_loaded(self):
logging.info("[BT-Tether] plugin loaded.")

def on_config_changed(self, config):
self._config = config
ip = config['main']['plugins']['bt-tether']['ip']
phone_name = config['main']['plugins']['bt-tether']['phone-name'] + ' Network'
if config['main']['plugins']['bt-tether']['phone'].lower() == 'android':
Expand All @@ -33,8 +35,7 @@ def on_config_changed(self, config):
return
try:
subprocess.run(['nmcli', 'connection', 'modify', f'{phone_name}', 'ipv4.addresses', f'{address}', 'ipv4.gateway',f'{gateway}', 'ipv4.route-metric', '100'], check=True)
subprocess.run(['nmcli', 'connection', 'reload'], check=True)
subprocess.run(['systemctl', 'restart', 'NetworkManager'], check=True)
subprocess.run(['nmcli', 'connection', 'up', f'{phone_name}'], check=True)
except Exception as e:
logging.error(f"[BT-Tether] Failed to connect to device: {e}")
self.ready = True
Expand All @@ -49,23 +50,26 @@ def on_ui_setup(self, ui):
label_font=fonts.Bold, text_font=fonts.Medium))

def on_ui_update(self, ui):
phone_name = self._config['main']['plugins']['bt-tether']['phone-name'] + ' Network'
if (subprocess.run(['bluetoothctl', 'info'], capture_output=True, text=True)).stdout.find('Connected: yes') != -1:
self.status = 'C'
else:
self.status = '-'
try:
mac = self.options['mac']
subprocess.run(['nmcli', 'device', 'connect', f'{mac}'], check=True)
subprocess.run(['nmcli', 'connection', 'up', f'{phone_name}'], check=True)
except Exception as e:
logging.error(f"[BT-Tether] Failed to connect to device: {e}")
ui.set('bluetooth', self.status)

def on_unload(self, ui):
phone_name = self._config['main']['plugins']['bt-tether']['phone-name'] + ' Network'
with ui._lock:
ui.remove_element('bluetooth')
try:
mac = self.options['mac']
subprocess.run(['nmcli', 'device', 'disconnect', f'{mac}'], check=True)
logging.info(f"[BT-Tether] Disconnected from device with MAC: {mac}")
if (subprocess.run(['bluetoothctl', 'info'], capture_output=True, text=True)).stdout.find('Connected: yes') != -1:
subprocess.run(['nmcli', 'connection', 'down', f'{phone_name}'], check=True)
logging.info(f"[BT-Tether] Disconnected from device with name: {phone_name}")
else:
logging.info(f"[BT-Tether] Device with name {phone_name} is not connected, not disconnecting")
except Exception as e:
logging.error(f"[BT-Tether] Failed to disconnect from device: {e}")

0 comments on commit 3e5d802

Please sign in to comment.