forked from bbirand/python-dbus-gatt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
td_devices.py
83 lines (64 loc) · 2.2 KB
/
td_devices.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#!/usr/bin/env python
#
# This file is part of python-tdbus. Python-tdbus is free software
# available under the terms of the MIT license. See the file "LICENSE" that
# was provided together with this source file for the licensing terms.
#
# Copyright (c) 2012 the python-tdbus authors. See the file "AUTHORS" for a
# complete list.
# This example shows how to access Avahi on the D-BUS.
from tdbus import SimpleDBusConnection, DBUS_BUS_SYSTEM, DBusHandler, signal_handler, DBusError
import logging
logging.basicConfig(level=logging.DEBUG)
# = 'org.freedesktop.Avahi'
#PATH_SERVER = '/'
#IFACE_SERVER = 'org.freedesktop.Avahi.Server'
conn = SimpleDBusConnection(DBUS_BUS_SYSTEM)
try:
bluez = conn.call_method('/', 'GetManagedObjects',
interface='org.freedesktop.DBus.ObjectManager',
destination='org.bluez')
except DBusError:
print "Bluez not available"
raise
var = bluez.get_args()[0]
for p, dev in var.iteritems():
if 'org.bluez.Adapter1' in dev:
print "Found adapter {}".format(p)
adapter = p
#
# Get device name
#
bluez = conn.call_method(adapter, 'Get',
interface='org.freedesktop.DBus.Properties',
format = 'ss',
args=('org.bluez.Adapter1', 'Powered'),
destination='org.bluez')
print bluez.get_args()[0]
# Returns:
# ('b', False)
# Bring up the interface
bluez = conn.call_method(adapter, 'Set',
interface='org.freedesktop.DBus.Properties',
format = 'ssv',
args=('org.bluez.Adapter1', 'Powered', ('b', True)),
destination='org.bluez')
import sys
sys.exit()
print 'Avahi is available at %s' % CONN_AVAHI
print 'Avahi version: %s' % result.get_args()[0]
print
print 'Browsing service types on domain: local'
print 'Press CTRL-c to exit'
print
result = conn.call_method('/', 'ServiceTypeBrowserNew', interface=IFACE_SERVER,
destination=CONN_AVAHI, format='iisu', args=(-1, 0, 'local', 0))
browser = result.get_args()[0]
print browser
class AvahiHandler(DBusHandler):
@signal_handler()
def ItemNew(self, message):
args = message.get_args()
print 'service %s exists on domain %s' % (args[2], args[3])
conn.add_handler(AvahiHandler())
conn.dispatch()