~bluetooth/bluez/import-mer-patches

« back to all changes in this revision

Viewing changes to test/test-adapter

  • Committer: Simon Fels
  • Date: 2015-09-11 09:01:46 UTC
  • Revision ID: morphis@gravedo.de-20150911090146-4c0ln9s7ec3xf0nx
Import package bluez_4.101-0ubuntu25.1~overlay4 from stable phone overlay PPA

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/python3
 
2
 
 
3
import sys
 
4
import dbus
 
5
import time
 
6
from optparse import OptionParser, make_option
 
7
 
 
8
bus = dbus.SystemBus()
 
9
 
 
10
manager = dbus.Interface(bus.get_object("org.bluez", "/"), "org.bluez.Manager")
 
11
 
 
12
option_list = [
 
13
                make_option("-i", "--device", action="store",
 
14
                                type="string", dest="dev_id"),
 
15
                ]
 
16
parser = OptionParser(option_list=option_list)
 
17
 
 
18
(options, args) = parser.parse_args()
 
19
 
 
20
if options.dev_id:
 
21
        adapter_path = manager.FindAdapter(options.dev_id)
 
22
else:
 
23
        adapter_path = manager.DefaultAdapter()
 
24
 
 
25
adapter = dbus.Interface(bus.get_object("org.bluez", adapter_path),
 
26
                                                        "org.bluez.Adapter")
 
27
 
 
28
if (len(args) < 1):
 
29
        print("Usage: %s <command>" % (sys.argv[0]))
 
30
        print("")
 
31
        print("  address")
 
32
        print("  list")
 
33
        print("  name [name]")
 
34
        print("  powered [on/off]")
 
35
        print("  pairable [on/off]")
 
36
        print("  pairabletimeout [timeout]")
 
37
        print("  discoverable [on/off]")
 
38
        print("  discoverabletimeout [timeout]")
 
39
        print("  discovering")
 
40
        sys.exit(1)
 
41
 
 
42
if (args[0] == "address"):
 
43
        properties = adapter.GetProperties()
 
44
        print(properties["Address"])
 
45
        sys.exit(0)
 
46
 
 
47
if (args[0] == "name"):
 
48
        if (len(args) < 2):
 
49
                properties = adapter.GetProperties()
 
50
                print(properties["Name"])
 
51
        else:
 
52
                adapter.SetProperty("Name", args[1])
 
53
        sys.exit(0)
 
54
 
 
55
if (args[0] == "list"):
 
56
        if (len(args) < 2):
 
57
                properties = manager.GetProperties()
 
58
                for adapter_path in properties["Adapters"]:
 
59
                        adapter = dbus.Interface(bus.get_object("org.bluez", adapter_path),
 
60
                                                                "org.bluez.Adapter")
 
61
                        prop = adapter.GetProperties()
 
62
                        print(" [ %s ]" % (adapter_path))
 
63
                        for (key, value) in prop.items():
 
64
                                if (key == "Class"):
 
65
                                        print("    %s = 0x%06x" % (key, value))
 
66
                                else:
 
67
                                        print("    %s = %s" % (key, value))
 
68
                        print()
 
69
        sys.exit(0)
 
70
 
 
71
if (args[0] == "powered"):
 
72
        if (len(args) < 2):
 
73
                properties = adapter.GetProperties()
 
74
                print(properties["Powered"])
 
75
        else:
 
76
                if (args[1] == "on"):
 
77
                        value = dbus.Boolean(1)
 
78
                elif (args[1] == "off"):
 
79
                        value = dbus.Boolean(0)
 
80
                else:
 
81
                        value = dbus.Boolean(args[1])
 
82
                adapter.SetProperty("Powered", value)
 
83
        sys.exit(0)
 
84
 
 
85
if (args[0] == "pairable"):
 
86
        if (len(args) < 2):
 
87
                properties = adapter.GetProperties()
 
88
                print(properties["Pairable"])
 
89
        else:
 
90
                if (args[1] == "on"):
 
91
                        value = dbus.Boolean(1)
 
92
                elif (args[1] == "off"):
 
93
                        value = dbus.Boolean(0)
 
94
                else:
 
95
                        value = dbus.Boolean(args[1])
 
96
                adapter.SetProperty("Pairable", value)
 
97
        sys.exit(0)
 
98
 
 
99
if (args[0] == "pairabletimeout"):
 
100
        if (len(args) < 2):
 
101
                properties = adapter.GetProperties()
 
102
                print(properties["PairableTimeout"])
 
103
        else:
 
104
                timeout = dbus.UInt32(args[1])
 
105
                adapter.SetProperty("PairableTimeout", timeout)
 
106
        sys.exit(0)
 
107
 
 
108
if (args[0] == "discoverable"):
 
109
        if (len(args) < 2):
 
110
                properties = adapter.GetProperties()
 
111
                print(properties["Discoverable"])
 
112
        else:
 
113
                if (args[1] == "on"):
 
114
                        value = dbus.Boolean(1)
 
115
                elif (args[1] == "off"):
 
116
                        value = dbus.Boolean(0)
 
117
                else:
 
118
                        value = dbus.Boolean(args[1])
 
119
                adapter.SetProperty("Discoverable", value)
 
120
        sys.exit(0)
 
121
 
 
122
if (args[0] == "discoverabletimeout"):
 
123
        if (len(args) < 2):
 
124
                properties = adapter.GetProperties()
 
125
                print(properties["DiscoverableTimeout"])
 
126
        else:
 
127
                timeout = dbus.UInt32(args[1])
 
128
                adapter.SetProperty("DiscoverableTimeout", timeout)
 
129
        sys.exit(0)
 
130
 
 
131
if (args[0] == "discovering"):
 
132
        properties = adapter.GetProperties()
 
133
        print(properties["Discovering"])
 
134
        sys.exit(0)
 
135
 
 
136
print("Unknown command")
 
137
sys.exit(1)