Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add bluetooth autoconnect ability #248

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
8 changes: 8 additions & 0 deletions resources/language/resource.language.en_gb/strings.po
Original file line number Diff line number Diff line change
Expand Up @@ -1016,6 +1016,14 @@ msgctxt "#32390"
msgid "Settings addon is not yet ready, please try again later."
msgstr ""

msgctxt "#32391"
msgid "Disable autoconnect"
msgstr ""

msgctxt "#32392"
msgid "Enable autoconnect"
msgstr ""

msgctxt "#32393"
msgid "** SAFE MODE! ** SAFE MODE! ** SAFE MODE! **"
msgstr ""
Expand Down
8 changes: 8 additions & 0 deletions resources/language/resource.language.fr_fr/strings.po
Original file line number Diff line number Diff line change
Expand Up @@ -1147,6 +1147,14 @@ msgctxt "#32390"
msgid "Settings addon is not yet ready, please try again later."
msgstr "Le greffon des paramètres n'est pas encore prêt, veuillez ré-essayer plus tard."

msgctxt "#32391"
msgid "Disable autoconnect"
msgstr "Désactiver l'autoconnexion"

msgctxt "#32392"
msgid "Enable autoconnect"
msgstr "Activer l'autoconnexion"

msgctxt "#32393"
msgid "** SAFE MODE! ** SAFE MODE! ** SAFE MODE! **"
msgstr "** MODE SANS ÉCHEC! ** MODE SANS ÉCHEC! ** MODE SANS ÉCHEC! **"
Expand Down
4 changes: 4 additions & 0 deletions resources/lib/dbus_bluez.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,10 @@ def device_get_connected(path):
return device_get_property(path, 'Connected')


def device_get_trusted(path):
return device_get_property(path, 'Trusted')


def device_connect(path):
return dbus_utils.run_method(BUS_NAME, path, INTERFACE_DEVICE, 'Connect')

Expand Down
47 changes: 43 additions & 4 deletions resources/lib/modules/bluetooth.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,22 @@ def disable_device_standby(self, listItem=None):
devices.remove(listItem.getProperty('entry'))
oe.write_setting('bluetooth', 'standby', ','.join(devices))

@log.log_function()
def enable_device_autoconnect(self, listItem=None):
devices = oe.read_setting('bluetooth', 'autoconnect')
devices = devices.split(',') if devices else []
if not listItem.getProperty('entry') in devices:
devices.append(listItem.getProperty('entry'))
oe.write_setting('bluetooth', 'autoconnect', ','.join(devices))

@log.log_function()
def disable_device_autoconnect(self, listItem=None):
devices = oe.read_setting('bluetooth', 'autoconnect')
devices = devices.split(',') if devices else []
if listItem.getProperty('entry') in devices:
devices.remove(listItem.getProperty('entry'))
oe.write_setting('bluetooth', 'autoconnect', ','.join(devices))

@log.log_function()
def pair_device(self, path):
try:
Expand Down Expand Up @@ -383,10 +399,7 @@ def open_context_menu(self, listItem):
'action': 'disconnect_device',
}
devices = oe.read_setting('bluetooth', 'standby')
if devices is not None:
devices = devices.split(',')
else:
devices = []
devices = devices.split(',') if devices else []
if listItem.getProperty('entry') in devices:
values[4] = {
'text': oe._(32389),
Expand All @@ -397,6 +410,20 @@ def open_context_menu(self, listItem):
'text': oe._(32388),
'action': 'enable_device_standby',
}

devices = oe.read_setting('bluetooth', 'autoconnect')
devices = devices.split(',') if devices else []
if listItem.getProperty('entry') in devices:
values[7] = {
'text': oe._(32391),
'action': 'disable_device_autoconnect',
}
else:
values[7] = {
'text': oe._(32392),
'action': 'enable_device_autoconnect',
}

elif listItem.getProperty('Paired') == '1':
values[1] = {
'text': oe._(32144),
Expand Down Expand Up @@ -454,6 +481,18 @@ def standby_devices(self):
if dbus_bluez.device_get_connected(device):
self.disconnect_device_by_path(device)

def autoconnect_devices(self):
if not self.dbusBluezAdapter:
return
devices = oe.read_setting('bluetooth', 'autoconnect')
if not devices:
return
for path in devices.split(','):
if not dbus_bluez.device_get_connected(path) and dbus_bluez.device_get_trusted(path):
try:
dbus_bluez.device_connect(path)
except DBusError as e:
pass

####################################################################
## Bluez Listener class
Expand Down
9 changes: 9 additions & 0 deletions resources/lib/oe.py
Original file line number Diff line number Diff line change
Expand Up @@ -637,6 +637,15 @@ def standby_devices():
except Exception as e:
dbg_log('oe::standby_devices', f'ERROR: ({repr(e)})')

def autoconnect_devices():
global dictModules
try:
if 'bluetooth' in dictModules:
dictModules['bluetooth'].autoconnect_devices()
except Exception as e:
dbg_log('oe::autoconnect_devices', f'ERROR: ({repr(e)})')


def load_config():
try:
global conf_lock
Expand Down
29 changes: 15 additions & 14 deletions service.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,23 +81,24 @@ def run(self):
oe.start_service()
service_thread = Service_Thread()
service_thread.start()
oe.autoconnect_devices()
while not self.abortRequested():
if self.waitForAbort(60):
if self.waitForAbort(30):
break
if not oe.read_setting('bluetooth', 'standby'):
continue
timeout = oe.read_setting('bluetooth', 'idle_timeout')
if not timeout:
continue
try:
timeout = int(timeout)
except:
continue
if timeout < 1:
continue
if xbmc.getGlobalIdleTime() / 60 >= timeout:
log.log(f'Idle timeout reached', log.DEBUG)
standby_devices = oe.read_setting('bluetooth', 'standby')
autoconnect_devices = oe.read_setting('bluetooth', 'autoconnect')
timeout = None
if standby_devices:
try:
timeout = int(oe.read_setting('bluetooth', 'idle_timeout'))
except TypeError:
pass
if timeout and xbmc.getGlobalIdleTime() / 60 >= timeout:
log.log('Idle timeout reached', log.DEBUG)
oe.standby_devices()
else:
log.log('Autoconnect triggered', log.DEBUG)
oe.autoconnect_devices()
if hasattr(oe, 'winOeMain') and hasattr(oe.winOeMain, 'visible'):
if oe.winOeMain.visible == True:
oe.winOeMain.close()
Expand Down