mirror of
https://git.kernel.org/pub/scm/bluetooth/bluez.git
synced 2024-12-12 05:24:19 +08:00
225 lines
5.7 KiB
Python
Executable File
225 lines
5.7 KiB
Python
Executable File
#!/usr/bin/python
|
|
|
|
from __future__ import absolute_import, print_function, unicode_literals
|
|
|
|
from gi.repository import GObject
|
|
|
|
import sys
|
|
import dbus
|
|
import dbus.mainloop.glib
|
|
import re
|
|
from optparse import OptionParser, make_option
|
|
|
|
dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
|
|
bus = dbus.SystemBus()
|
|
mainloop = GObject.MainLoop()
|
|
|
|
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(" list")
|
|
print(" services <address>")
|
|
print(" create <address>")
|
|
print(" remove <address|path>")
|
|
print(" connect <address>")
|
|
print(" disconnect <address>")
|
|
print(" discover <address> [pattern]")
|
|
print(" class <address>")
|
|
print(" name <address>")
|
|
print(" alias <address> [alias]")
|
|
print(" trusted <address> [yes/no]")
|
|
print(" blocked <address> [yes/no]")
|
|
sys.exit(1)
|
|
|
|
if (args[0] == "list"):
|
|
om = dbus.Interface(bus.get_object("org.bluez", "/"),
|
|
"org.freedesktop.DBus.ObjectManager")
|
|
objects = om.GetManagedObjects()
|
|
|
|
for path, interfaces in objects.iteritems():
|
|
if "org.bluez.Device" not in interfaces:
|
|
continue
|
|
properties = interfaces["org.bluez.Device"]
|
|
print("%s %s" % (properties["Address"], properties["Alias"]))
|
|
|
|
sys.exit(0)
|
|
|
|
def create_device_reply(device):
|
|
print("New device (%s)" % device)
|
|
mainloop.quit()
|
|
sys.exit(0)
|
|
|
|
def create_device_error(error):
|
|
print("Creating device failed: %s" % error)
|
|
mainloop.quit()
|
|
sys.exit(1)
|
|
|
|
if (args[0] == "create"):
|
|
if (len(args) < 2):
|
|
print("Need address parameter")
|
|
else:
|
|
adapter.CreateDevice(args[1],
|
|
reply_handler=create_device_reply,
|
|
error_handler=create_device_error)
|
|
mainloop.run()
|
|
|
|
if (args[0] == "remove"):
|
|
if (len(args) < 2):
|
|
print("Need address or object path parameter")
|
|
else:
|
|
try:
|
|
path = adapter.FindDevice(args[1])
|
|
except:
|
|
path = args[1]
|
|
adapter.RemoveDevice(path)
|
|
sys.exit(0)
|
|
|
|
if (args[0] == "connect"):
|
|
if (len(args) < 2):
|
|
print("Need address parameter")
|
|
else:
|
|
path = adapter.FindDevice(args[1])
|
|
device = dbus.Interface(bus.get_object("org.bluez", path),
|
|
"org.bluez.Device")
|
|
device.Connect()
|
|
sys.exit(0)
|
|
|
|
if (args[0] == "disconnect"):
|
|
if (len(args) < 2):
|
|
print("Need address parameter")
|
|
else:
|
|
path = adapter.FindDevice(args[1])
|
|
device = dbus.Interface(bus.get_object("org.bluez", path),
|
|
"org.bluez.Device")
|
|
device.Disconnect()
|
|
sys.exit(0)
|
|
|
|
if (args[0] == "discover"):
|
|
if (len(args) < 2):
|
|
print("Need address parameter")
|
|
else:
|
|
path = adapter.FindDevice(args[1])
|
|
device = dbus.Interface(bus.get_object("org.bluez", path),
|
|
"org.bluez.Device")
|
|
if (len(args) < 3):
|
|
pattern = ""
|
|
else:
|
|
pattern = args[2]
|
|
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 (args[0] == "class"):
|
|
if (len(args) < 2):
|
|
print("Need address parameter")
|
|
else:
|
|
path = adapter.FindDevice(args[1])
|
|
device = dbus.Interface(bus.get_object("org.bluez", path),
|
|
"org.freedesktop.DBus.Properties")
|
|
cls = device.Get("org.bluez.Device", "Class")
|
|
print("0x%06x" % cls)
|
|
sys.exit(0)
|
|
|
|
if (args[0] == "name"):
|
|
if (len(args) < 2):
|
|
print("Need address parameter")
|
|
else:
|
|
path = adapter.FindDevice(args[1])
|
|
device = dbus.Interface(bus.get_object("org.bluez", path),
|
|
"org.freedesktop.DBus.Properties")
|
|
name = device.Get("org.bluez.Device", "Name")
|
|
print(name)
|
|
sys.exit(0)
|
|
|
|
if (args[0] == "alias"):
|
|
if (len(args) < 2):
|
|
print("Need address parameter")
|
|
else:
|
|
path = adapter.FindDevice(args[1])
|
|
device = dbus.Interface(bus.get_object("org.bluez", path),
|
|
"org.freedesktop.DBus.Properties")
|
|
if (len(args) < 3):
|
|
alias = device.Get("org.bluez.Device", "Alias")
|
|
print(alias)
|
|
else:
|
|
device.Set("org.bluez.Device", "Alias", args[2])
|
|
sys.exit(0)
|
|
|
|
if (args[0] == "trusted"):
|
|
if (len(args) < 2):
|
|
print("Need address parameter")
|
|
else:
|
|
path = adapter.FindDevice(args[1])
|
|
device = dbus.Interface(bus.get_object("org.bluez", path),
|
|
"org.freedesktop.DBus.Properties")
|
|
if (len(args) < 3):
|
|
trusted = device.Get("org.bluez.Device", "Trusted")
|
|
print(trusted)
|
|
else:
|
|
if (args[2] == "yes"):
|
|
value = dbus.Boolean(1)
|
|
elif (args[2] == "no"):
|
|
value = dbus.Boolean(0)
|
|
else:
|
|
value = dbus.Boolean(args[2])
|
|
device.Set("org.bluez.Device", "Trusted", value)
|
|
sys.exit(0)
|
|
|
|
if (args[0] == "blocked"):
|
|
if (len(args) < 2):
|
|
print("Need address parameter")
|
|
else:
|
|
path = adapter.FindDevice(args[1])
|
|
device = dbus.Interface(bus.get_object("org.bluez", path),
|
|
"org.freedesktop.DBus.Properties")
|
|
if (len(args) < 3):
|
|
blocked = device.Get("org.bluez.Device", "Blocked")
|
|
print(blocked)
|
|
else:
|
|
if (args[2] == "yes"):
|
|
value = dbus.Boolean(1)
|
|
elif (args[2] == "no"):
|
|
value = dbus.Boolean(0)
|
|
else:
|
|
value = dbus.Boolean(args[2])
|
|
device.Set("org.bluez.Device", "Blocked", value)
|
|
sys.exit(0)
|
|
|
|
if (args[0] == "services"):
|
|
if (len(args) < 2):
|
|
print("Need address parameter")
|
|
else:
|
|
path = adapter.FindDevice(args[1])
|
|
device = dbus.Interface(bus.get_object("org.bluez", path),
|
|
"org.freedesktop.DBus.Properties")
|
|
services = device.Get("org.bluez.Device", "Services")
|
|
for path in services:
|
|
print(path)
|
|
sys.exit(0)
|
|
|
|
print("Unknown command")
|
|
sys.exit(1)
|