| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
from __future__ import absolute_import, print_function, unicode_literals |
| 5 |
|
| 6 |
from optparse import OptionParser |
| 7 |
import sys |
| 8 |
import dbus |
| 9 |
import dbus.service |
| 10 |
import dbus.mainloop.glib |
| 11 |
try: |
| 12 |
from gi.repository import GObject |
| 13 |
except ImportError: |
| 14 |
import gobject as GObject |
| 15 |
import bluezutils |
| 16 |
|
| 17 |
BUS_NAME = 'org.bluez' |
| 18 |
AGENT_INTERFACE = 'org.bluez.Agent1' |
| 19 |
AGENT_PATH = "/test/agent" |
| 20 |
|
| 21 |
bus = None |
| 22 |
device_obj = None |
| 23 |
dev_path = None |
| 24 |
|
| 25 |
def ask(prompt): |
| 26 |
try: |
| 27 |
return raw_input(prompt) |
| 28 |
except: |
| 29 |
return input(prompt) |
| 30 |
|
| 31 |
def set_trusted(path): |
| 32 |
props = dbus.Interface(bus.get_object("org.bluez", path), |
| 33 |
"org.freedesktop.DBus.Properties") |
| 34 |
props.Set("org.bluez.Device1", "Trusted", True) |
| 35 |
|
| 36 |
def dev_connect(path): |
| 37 |
dev = dbus.Interface(bus.get_object("org.bluez", path), |
| 38 |
"org.bluez.Device1") |
| 39 |
dev.Connect() |
| 40 |
|
| 41 |
class Rejected(dbus.DBusException): |
| 42 |
_dbus_error_name = "org.bluez.Error.Rejected" |
| 43 |
|
| 44 |
class Agent(dbus.service.Object): |
| 45 |
exit_on_release = True |
| 46 |
|
| 47 |
def set_exit_on_release(self, exit_on_release): |
| 48 |
self.exit_on_release = exit_on_release |
| 49 |
|
| 50 |
@dbus.service.method(AGENT_INTERFACE, |
| 51 |
in_signature="", out_signature="") |
| 52 |
def Release(self): |
| 53 |
print("Release") |
| 54 |
if self.exit_on_release: |
| 55 |
mainloop.quit() |
| 56 |
|
| 57 |
@dbus.service.method(AGENT_INTERFACE, |
| 58 |
in_signature="os", out_signature="") |
| 59 |
def AuthorizeService(self, device, uuid): |
| 60 |
print("AuthorizeService (%s, %s)" % (device, uuid)) |
| 61 |
authorize = ask("Authorize connection (yes/no): ") |
| 62 |
if (authorize == "yes"): |
| 63 |
return |
| 64 |
raise Rejected("Connection rejected by user") |
| 65 |
|
| 66 |
@dbus.service.method(AGENT_INTERFACE, |
| 67 |
in_signature="o", out_signature="s") |
| 68 |
def RequestPinCode(self, device): |
| 69 |
print("RequestPinCode (%s)" % (device)) |
| 70 |
set_trusted(device) |
| 71 |
return ask("Enter PIN Code: ") |
| 72 |
|
| 73 |
@dbus.service.method(AGENT_INTERFACE, |
| 74 |
in_signature="o", out_signature="u") |
| 75 |
def RequestPasskey(self, device): |
| 76 |
print("RequestPasskey (%s)" % (device)) |
| 77 |
set_trusted(device) |
| 78 |
passkey = ask("Enter passkey: ") |
| 79 |
return dbus.UInt32(passkey) |
| 80 |
|
| 81 |
@dbus.service.method(AGENT_INTERFACE, |
| 82 |
in_signature="ouq", out_signature="") |
| 83 |
def DisplayPasskey(self, device, passkey, entered): |
| 84 |
print("DisplayPasskey (%s, %06u entered %u)" % |
| 85 |
(device, passkey, entered)) |
| 86 |
|
| 87 |
@dbus.service.method(AGENT_INTERFACE, |
| 88 |
in_signature="os", out_signature="") |
| 89 |
def DisplayPinCode(self, device, pincode): |
| 90 |
print("DisplayPinCode (%s, %s)" % (device, pincode)) |
| 91 |
|
| 92 |
@dbus.service.method(AGENT_INTERFACE, |
| 93 |
in_signature="ou", out_signature="") |
| 94 |
def RequestConfirmation(self, device, passkey): |
| 95 |
print("RequestConfirmation (%s, %06d)" % (device, passkey)) |
| 96 |
confirm = ask("Confirm passkey (yes/no): ") |
| 97 |
if (confirm == "yes"): |
| 98 |
set_trusted(device) |
| 99 |
return |
| 100 |
raise Rejected("Passkey doesn't match") |
| 101 |
|
| 102 |
@dbus.service.method(AGENT_INTERFACE, |
| 103 |
in_signature="o", out_signature="") |
| 104 |
def RequestAuthorization(self, device): |
| 105 |
print("RequestAuthorization (%s)" % (device)) |
| 106 |
auth = ask("Authorize? (yes/no): ") |
| 107 |
if (auth == "yes"): |
| 108 |
return |
| 109 |
raise Rejected("Pairing rejected") |
| 110 |
|
| 111 |
@dbus.service.method(AGENT_INTERFACE, |
| 112 |
in_signature="", out_signature="") |
| 113 |
def Cancel(self): |
| 114 |
print("Cancel") |
| 115 |
|
| 116 |
def pair_reply(): |
| 117 |
print("Device paired") |
| 118 |
set_trusted(dev_path) |
| 119 |
dev_connect(dev_path) |
| 120 |
mainloop.quit() |
| 121 |
|
| 122 |
def pair_error(error): |
| 123 |
err_name = error.get_dbus_name() |
| 124 |
if err_name == "org.freedesktop.DBus.Error.NoReply" and device_obj: |
| 125 |
print("Timed out. Cancelling pairing") |
| 126 |
device_obj.CancelPairing() |
| 127 |
else: |
| 128 |
print("Creating device failed: %s" % (error)) |
| 129 |
|
| 130 |
|
| 131 |
mainloop.quit() |
| 132 |
|
| 133 |
if __name__ == '__main__': |
| 134 |
dbus.mainloop.glib.DBusGMainLoop(set_as_default=True) |
| 135 |
|
| 136 |
bus = dbus.SystemBus() |
| 137 |
|
| 138 |
capability = "KeyboardDisplay" |
| 139 |
|
| 140 |
parser = OptionParser() |
| 141 |
parser.add_option("-i", "--adapter", action="store", |
| 142 |
type="string", |
| 143 |
dest="adapter_pattern", |
| 144 |
default=None) |
| 145 |
parser.add_option("-c", "--capability", action="store", |
| 146 |
type="string", dest="capability") |
| 147 |
parser.add_option("-t", "--timeout", action="store", |
| 148 |
type="int", dest="timeout", |
| 149 |
default=60000) |
| 150 |
(options, args) = parser.parse_args() |
| 151 |
if options.capability: |
| 152 |
capability = options.capability |
| 153 |
|
| 154 |
path = "/test/agent" |
| 155 |
agent = Agent(bus, path) |
| 156 |
|
| 157 |
mainloop = GObject.MainLoop() |
| 158 |
|
| 159 |
obj = bus.get_object(BUS_NAME, "/org/bluez"); |
| 160 |
manager = dbus.Interface(obj, "org.bluez.AgentManager1") |
| 161 |
manager.RegisterAgent(path, capability) |
| 162 |
|
| 163 |
print("Agent registered") |
| 164 |
|
| 165 |
|
| 166 |
if len(args) > 0 and args[0].startswith("hci"): |
| 167 |
options.adapter_pattern = args[0] |
| 168 |
del args[:1] |
| 169 |
|
| 170 |
if len(args) > 0: |
| 171 |
device = bluezutils.find_device(args[0], |
| 172 |
options.adapter_pattern) |
| 173 |
dev_path = device.object_path |
| 174 |
agent.set_exit_on_release(False) |
| 175 |
device.Pair(reply_handler=pair_reply, error_handler=pair_error, |
| 176 |
timeout=60000) |
| 177 |
device_obj = device |
| 178 |
else: |
| 179 |
manager.RequestDefaultAgent(path) |
| 180 |
|
| 181 |
mainloop.run() |
| 182 |
|
| 183 |
|
| 184 |
|