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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
|
#!/usr/bin/python
import sys
import dbus
import re
bus = dbus.SystemBus()
manager = dbus.Interface(bus.get_object("org.bluez", "/"), "org.bluez.Manager")
adapter = dbus.Interface(bus.get_object("org.bluez", manager.DefaultAdapter()),
"org.bluez.Adapter")
if (len(sys.argv) < 2):
print "Usage: %s <command>" % (sys.argv[0])
print ""
print " list"
print " create <address>"
print " remove <address|path>"
print " discover <address> [pattern]"
print " class <address>"
print " name <address>"
print " alias <address> [alias]"
print " trusted <address> [yes/no]"
sys.exit(1)
if (sys.argv[1] == "list"):
list = adapter.ListDevices()
print list
sys.exit(0)
if (sys.argv[1] == "create"):
if (len(sys.argv) < 3):
print "Need address parameter"
else:
device = adapter.CreateDevice(sys.argv[2])
print device
sys.exit(0)
if (sys.argv[1] == "remove"):
if (len(sys.argv) < 3):
print "Need address or object path parameter"
else:
try:
path = adapter.FindDevice(sys.argv[2])
except:
path = sys.argv[2]
adapter.RemoveDevice(path)
sys.exit(0)
if (sys.argv[1] == "discover"):
if (len(sys.argv) < 3):
print "Need address parameter"
else:
path = adapter.FindDevice(sys.argv[2])
device = dbus.Interface(bus.get_object("org.bluez", path),
"org.bluez.Device")
if (len(sys.argv) < 4):
pattern = ""
else:
pattern = sys.argv[3]
services = device.DiscoverServices(pattern);
for key in services.keys():
p = re.compile(">.*?<")
xml = p.sub("><", services[key].replace("\n", ""))
print "[ 0x%5x ]" % (key)
print xml
print
sys.exit(0)
if (sys.argv[1] == "class"):
if (len(sys.argv) < 3):
print "Need address parameter"
else:
path = adapter.FindDevice(sys.argv[2])
device = dbus.Interface(bus.get_object("org.bluez", path),
"org.bluez.Device")
properties = device.GetProperties()
print "0x%06x" % (properties["Class"])
sys.exit(0)
if (sys.argv[1] == "name"):
if (len(sys.argv) < 3):
print "Need address parameter"
else:
path = adapter.FindDevice(sys.argv[2])
device = dbus.Interface(bus.get_object("org.bluez", path),
"org.bluez.Device")
properties = device.GetProperties()
print properties["Name"]
sys.exit(0)
if (sys.argv[1] == "alias"):
if (len(sys.argv) < 3):
print "Need address parameter"
else:
path = adapter.FindDevice(sys.argv[2])
device = dbus.Interface(bus.get_object("org.bluez", path),
"org.bluez.Device")
if (len(sys.argv) < 4):
properties = device.GetProperties()
print properties["Alias"]
else:
device.SetProperty("Alias", sys.argv[3])
sys.exit(0)
if (sys.argv[1] == "trusted"):
if (len(sys.argv) < 3):
print "Need address parameter"
else:
path = adapter.FindDevice(sys.argv[2])
device = dbus.Interface(bus.get_object("org.bluez", path),
"org.bluez.Device")
if (len(sys.argv) < 4):
properties = device.GetProperties()
print properties["Trusted"]
else:
if (sys.argv[3] == "yes"):
value = dbus.Boolean(1)
elif (sys.argv[3] == "no"):
value = dbus.Boolean(0)
else:
value = dbus.Boolean(sys.argv[3])
device.SetProperty("Trusted", value)
sys.exit(0)
print "Unknown command"
sys.exit(1)
|