summaryrefslogtreecommitdiff
path: root/test/bt-handover
blob: b4ead093d32b63ed0ffc709963a019d54eacf0af (plain)
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
#!/usr/bin/python

import os
import sys
import gobject

import dbus
import dbus.mainloop.glib
import neardutils

from dbus.lowlevel import MethodCallMessage, HANDLER_RESULT_NOT_YET_HANDLED

mainloop = gobject.MainLoop()

def device_added(path, interfaces):
	for iface, props in interfaces.iteritems():
		if "org.neard.Device" in interfaces:
			print("Pairing with %s" % (path))
			device = dbus.Interface(bus.get_object("org.neard", path),
							"org.neard.Device")
			device.Push(({ "Type" : "Handover", "Carrier" : "bluetooth"}))
			break

def device_removed(path, interfaces):
	for iface in interfaces:
		if "org.neard.Device" in interfaces:
			print("Lost device %s" % (path))
			mainloop.quit()
			break

def remove_paired_devices():
	bluez_manager = dbus.Interface(bus.get_object("org.bluez", "/"),
					"org.bluez.Manager")

	bluez_adapter_path = bluez_manager.DefaultAdapter()
	bluez_adapter = dbus.Interface(bus.get_object("org.bluez",
						bluez_adapter_path),
						"org.bluez.Adapter")

	print("Using %s" % bluez_adapter.object_path)

	for bluez_path in bluez_adapter.ListDevices():
		print("Removing %s" % (bluez_path))
		bluez_adapter.RemoveDevice(bluez_path)

if __name__ == '__main__':
	dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)

	bus = dbus.SystemBus()

	adapter_path = neardutils.find_adapter().object_path
	print("Adapter path %s" % (adapter_path))
	adapter = dbus.Interface(bus.get_object("org.neard", adapter_path),
							"org.neard.Adapter")

	adapter_props = dbus.Interface(bus.get_object("org.neard", adapter_path),
					"org.freedesktop.DBus.Properties")
	powered = adapter_props.Get("org.neard.Adapter", "Powered")
	if (powered == dbus.Boolean(0)):
		adapter_props.Set("org.neard.Adapter", "Powered", dbus.Boolean(1))

	remove_paired_devices()

	polling = adapter_props.Get("org.neard.Adapter", "Polling")
	if (polling == dbus.Boolean(0)):
		adapter.StartPollLoop("Initiator")

	bus.add_signal_receiver(device_added, bus_name="org.neard",
			dbus_interface="org.freedesktop.DBus.ObjectManager",
			signal_name="InterfacesAdded")

	bus.add_signal_receiver(device_removed, bus_name="org.neard",
			dbus_interface="org.freedesktop.DBus.ObjectManager",
			signal_name="InterfacesRemoved")

	mainloop.run()