Last active
January 2, 2023 09:26
-
-
Save Langerz82/6ccb21555cd19af753b479682772a94e to your computer and use it in GitHub Desktop.
python test emuelec-bluetooth
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import time | |
import logging | |
import sys | |
from bluetool import Bluetooth | |
logger = logging.getLogger(__name__) | |
if __name__ == "__main__": | |
bt = Bluetooth() | |
print('Scanning for available devices for 90 seconds, please wait...') | |
try: | |
bt.start_scanning(60) | |
time.sleep(10) | |
except (bluezutils.BluezUtilError) as error: | |
logger.error(str(error) + "\n") | |
sys.exit(1) | |
print('Getting pairable devices, please wait...') | |
devices = bt.get_devices_to_pair() | |
for device in devices: | |
mac = device['mac_address'].decode('utf-8') | |
name = device['name'].decode('utf-8') | |
icon = bt.get_device_property(mac,'Icon') | |
if not icon or (icon and icon == 'input-gaming'): | |
print('Found MAC: {}\tName: {}'.format(mac,name)) | |
print('Found controller {} Name: {}, trusting...'.format(mac,name)) | |
bt.trust(mac) | |
if bt.get_device_property(mac,'Trusted') == 1: | |
print('Trusted {}, quick pause, then pairing...'.format(name)) | |
time.sleep(5) | |
bt.pair(mac) | |
if bt.get_device_property(mac,'Paired') == 1: | |
print('Paired {}, quick pause, then connecting...'.format(name)) | |
time.sleep(5) | |
bt.connect(mac) | |
if bt.get_device_property(mac,'Connected') == 1: | |
print('Connected {}, exiting...'.format(name)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment