· 2 years ago · Apr 26, 2023, 10:10 AM
1#
2# dela
3#
4import sys
5sys.path.append('/storage/downloads/bt2/pydbus-0.6.0')
6
7import pgi
8pgi.install_as_gi()
9from gi.repository import GLib
10
11import pydbus
12import time
13
14discovery_time = 20
15adapter_path = '/org/bluez/hci0'
16
17pyd_bus = pydbus.SystemBus()
18mainloop = GLib.MainLoop()
19
20bluez = pyd_bus.get("org.bluez", "/")
21
22# connect to the DBus api for the Bluetooth adapter
23adapter = pyd_bus.get('org.bluez', adapter_path)
24
25bluez.onInterfacesAdded = lambda *args: print("InterfacesAdded: ", *args)
26bluez.onInterfacesRemoved = lambda *args: print("InterfacesRemoved: ", *args)
27adapter.onPropertiesChanged = lambda *args: print("PropertiesChanged: ", *args)
28
29def end_discovery():
30 """Handler for end of discovery"""
31 mainloop.quit()
32 adapter.StopDiscovery()
33
34# Run discovery
35adapter.StartDiscovery()
36#GLib.timeout_add_seconds(discovery_time, end_discovery)
37print('Scanning for devices...')
38#mainloop.run()
39
40for i in range(0, discovery_time):
41 time.sleep(1)
42 print('-------------------------------------------------------')
43
44 manager = pyd_bus.get('org.bluez', '/')
45 managed_objects = manager.GetManagedObjects()
46 for device_path in managed_objects.keys():
47 #print("device_path [ %s ]" % (device_path))
48
49 uuids = managed_objects[device_path].get('org.bluez.Device1', {}).get('UUIDs', [])
50 for uuid in uuids:
51 # HID (Human Interface Device), For classic BR/EDR keyboards and mice, 0x1124
52 # HID Over GATT, For LE keyboards and mice, 0x1812
53 if uuid.startswith('00001124') or uuid.startswith('00001812'):
54 print('')
55 device_name = managed_objects[device_path].get('org.bluez.Device1', {}).get('Name')
56 device_address = managed_objects[device_path].get('org.bluez.Device1', {}).get('Address')
57 device_paired = managed_objects[device_path].get('org.bluez.Device1', {}).get('Paired')
58 device_trusted = managed_objects[device_path].get('org.bluez.Device1', {}).get('Trusted')
59
60 if device_name not in 'B21':
61 continue
62
63 print('remote control:\n Name: {}\n Address: {}\n Paired: {}\n Trusted: {}'.
64 format(device_name, device_address, device_paired, device_trusted))
65
66 if not device_paired:
67 print('pair device {}'.format(device_path))
68 #adapter.Trusted = True
69 #adapter.Pair()
70 try:
71 dev = pyd_bus.get('org.bluez', device_path)
72 dev.Trusted = True
73 dev.Pair()
74 except:
75 pass
76
77 #print("[ %s ]" % (path))
78 # print all keys
79 if 0:
80 for interface in managed_objects[path].keys():
81 properties = interfaces[interface]
82 for key in properties.keys():
83 print(" %s = %s" % (key, properties[key]))
84########################################
85
86print('-------------------------------------------------------')
87