~bluetooth/bluez/import-mer-patches

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
129
130
131
132
133
134
135
136
137
138
139
#!/usr/bin/python

from __future__ import absolute_import, print_function, unicode_literals

import sys
import dbus
import time
from optparse import OptionParser, make_option

bus = dbus.SystemBus()

manager = dbus.Interface(bus.get_object("org.bluez", "/"), "org.bluez.Manager")

option_list = [
		make_option("-i", "--device", action="store",
				type="string", dest="dev_id"),
		]
parser = OptionParser(option_list=option_list)

(options, args) = parser.parse_args()

if options.dev_id:
	adapter_path = manager.FindAdapter(options.dev_id)
else:
	adapter_path = manager.DefaultAdapter()

adapter = dbus.Interface(bus.get_object("org.bluez", adapter_path),
							"org.bluez.Adapter")

if (len(args) < 1):
	print("Usage: %s <command>" % (sys.argv[0]))
	print("")
	print("  address")
	print("  list")
	print("  name [name]")
	print("  powered [on/off]")
	print("  pairable [on/off]")
	print("  pairabletimeout [timeout]")
	print("  discoverable [on/off]")
	print("  discoverabletimeout [timeout]")
	print("  discovering")
	sys.exit(1)

if (args[0] == "address"):
	properties = adapter.GetProperties()
	print(properties["Address"])
	sys.exit(0)

if (args[0] == "name"):
	if (len(args) < 2):
		properties = adapter.GetProperties()
		print(properties["Name"])
	else:
		adapter.SetProperty("Name", args[1])
	sys.exit(0)

if (args[0] == "list"):
	if (len(args) < 2):
		properties = manager.GetProperties()
		for adapter_path in properties["Adapters"]:
			adapter = dbus.Interface(bus.get_object("org.bluez", adapter_path),
								"org.bluez.Adapter")
			prop = adapter.GetProperties()
			print(" [ %s ]" % (adapter_path))
			for (key, value) in prop.items():
				if (key == "Class"):
					print("    %s = 0x%06x" % (key, value))
				else:
					print("    %s = %s" % (key, value))
			print()
	sys.exit(0)

if (args[0] == "powered"):
	if (len(args) < 2):
		properties = adapter.GetProperties()
		print(properties["Powered"])
	else:
		if (args[1] == "on"):
			value = dbus.Boolean(1)
		elif (args[1] == "off"):
			value = dbus.Boolean(0)
		else:
			value = dbus.Boolean(args[1])
		adapter.SetProperty("Powered", value)
	sys.exit(0)

if (args[0] == "pairable"):
	if (len(args) < 2):
		properties = adapter.GetProperties()
		print(properties["Pairable"])
	else:
		if (args[1] == "on"):
			value = dbus.Boolean(1)
		elif (args[1] == "off"):
			value = dbus.Boolean(0)
		else:
			value = dbus.Boolean(args[1])
		adapter.SetProperty("Pairable", value)
	sys.exit(0)

if (args[0] == "pairabletimeout"):
	if (len(args) < 2):
		properties = adapter.GetProperties()
		print(properties["PairableTimeout"])
	else:
		timeout = dbus.UInt32(args[1])
		adapter.SetProperty("PairableTimeout", timeout)
	sys.exit(0)

if (args[0] == "discoverable"):
	if (len(args) < 2):
		properties = adapter.GetProperties()
		print(properties["Discoverable"])
	else:
		if (args[1] == "on"):
			value = dbus.Boolean(1)
		elif (args[1] == "off"):
			value = dbus.Boolean(0)
		else:
			value = dbus.Boolean(args[1])
		adapter.SetProperty("Discoverable", value)
	sys.exit(0)

if (args[0] == "discoverabletimeout"):
	if (len(args) < 2):
		properties = adapter.GetProperties()
		print(properties["DiscoverableTimeout"])
	else:
		timeout = dbus.UInt32(args[1])
		adapter.SetProperty("DiscoverableTimeout", timeout)
	sys.exit(0)

if (args[0] == "discovering"):
	properties = adapter.GetProperties()
	print(properties["Discovering"])
	sys.exit(0)

print("Unknown command")
sys.exit(1)