eagerbeagler

bluez-trust-device

Jan 29th, 2013
426
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.13 KB | None | 0 0
  1. #!/usr/bin/python
  2.  
  3. # Script to simplify querying and setting the Trusted state for a remote Bluetooth device
  4. # Adapted from bluez-test-device, mainly by stripping stuff out
  5. # Does not use GObject, so is suitable for non-GUI platforms
  6.  
  7. import sys
  8. import dbus
  9. import dbus.mainloop.glib
  10. from optparse import OptionParser, make_option
  11.  
  12. dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
  13. bus = dbus.SystemBus()
  14. manager = dbus.Interface(bus.get_object("org.bluez", "/"), "org.bluez.Manager")
  15.  
  16. # Allow an option to select from more than one Bluetooth adaptor on the host system
  17. option_list = [
  18.         make_option("-i", "--device", action="store", type="string", dest="dev_id"),
  19.         ]
  20. parser = OptionParser(option_list=option_list)
  21.  
  22. (options, args) = parser.parse_args()
  23.  
  24. if options.dev_id:
  25.     try:
  26.         adapter_path = manager.FindAdapter(options.dev_id)
  27.     except dbus.DBusException:
  28.         print("FindAdapter() has thrown an exception!")
  29.         print("Check your host device-ID and run this script again")
  30.         sys.exit(1)
  31. else:
  32.     try:
  33.         adapter_path = manager.DefaultAdapter()
  34.     except dbus.DBusException:
  35.         print("DefaultAdapter() has thrown an exception!")
  36.         print("This sometimes happens the first time it is called - try running this script again")
  37.         sys.exit(1)
  38.  
  39. adapter = dbus.Interface(bus.get_object("org.bluez", adapter_path), "org.bluez.Adapter")
  40.  
  41. if (len(args) < 1):
  42.     print("Usage: %s <address> [yes/no]" % (sys.argv[0]))
  43.     print("e.g. %s 00:19:0E:0C:0C:CD yes" % (sys.argv[0]))
  44.     print("")
  45.     sys.exit(1)
  46.  
  47. else:
  48.     path = adapter.FindDevice(args[0])
  49.     device = dbus.Interface(bus.get_object("org.bluez", path), "org.bluez.Device")
  50.     if (len(args) < 2): # no "yes/no" parameter - query the current trust state for this device
  51.             properties = device.GetProperties()
  52.             trust_state = properties["Trusted"]
  53.             if (trust_state == 1):
  54.                 print("Device %s is trusted" % args[0])
  55.             else:
  56.                 print("Device %s is NOT trusted" % args[0])
  57.     else:
  58.         if (args[1] == "yes"):
  59.             value = dbus.Boolean(1)
  60.         elif (args[1] == "no"):
  61.             value = dbus.Boolean(0)
  62.         else:
  63.             value = dbus.Boolean(args[1])
  64.         device.SetProperty("Trusted", value)
  65.     sys.exit(0)
  66.  
  67. sys.exit(1)
Advertisement
Add Comment
Please, Sign In to add comment