Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python
- # Script to simplify querying and setting the Trusted state for a remote Bluetooth device
- # Adapted from bluez-test-device, mainly by stripping stuff out
- # Does not use GObject, so is suitable for non-GUI platforms
- import sys
- import dbus
- import dbus.mainloop.glib
- from optparse import OptionParser, make_option
- dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
- bus = dbus.SystemBus()
- manager = dbus.Interface(bus.get_object("org.bluez", "/"), "org.bluez.Manager")
- # Allow an option to select from more than one Bluetooth adaptor on the host system
- 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:
- try:
- adapter_path = manager.FindAdapter(options.dev_id)
- except dbus.DBusException:
- print("FindAdapter() has thrown an exception!")
- print("Check your host device-ID and run this script again")
- sys.exit(1)
- else:
- try:
- adapter_path = manager.DefaultAdapter()
- except dbus.DBusException:
- print("DefaultAdapter() has thrown an exception!")
- print("This sometimes happens the first time it is called - try running this script again")
- sys.exit(1)
- adapter = dbus.Interface(bus.get_object("org.bluez", adapter_path), "org.bluez.Adapter")
- if (len(args) < 1):
- print("Usage: %s <address> [yes/no]" % (sys.argv[0]))
- print("e.g. %s 00:19:0E:0C:0C:CD yes" % (sys.argv[0]))
- print("")
- sys.exit(1)
- else:
- path = adapter.FindDevice(args[0])
- device = dbus.Interface(bus.get_object("org.bluez", path), "org.bluez.Device")
- if (len(args) < 2): # no "yes/no" parameter - query the current trust state for this device
- properties = device.GetProperties()
- trust_state = properties["Trusted"]
- if (trust_state == 1):
- print("Device %s is trusted" % args[0])
- else:
- print("Device %s is NOT trusted" % args[0])
- else:
- if (args[1] == "yes"):
- value = dbus.Boolean(1)
- elif (args[1] == "no"):
- value = dbus.Boolean(0)
- else:
- value = dbus.Boolean(args[1])
- device.SetProperty("Trusted", value)
- sys.exit(0)
- sys.exit(1)
Advertisement
Add Comment
Please, Sign In to add comment