Blob: test-health-sink

Blob id: fcdc58e3d0095f7345e4d46355626a5d04ebbbe0

Size: 2.5 KB

  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
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#!/usr/bin/env python3
# SPDX-License-Identifier: LGPL-2.1-or-later

from __future__ import absolute_import, print_function, unicode_literals
# -*- coding: utf-8 -*-

import sys
import dbus
import dbus.service
from dbus.mainloop.glib import DBusGMainLoop
try:
  from gi.repository import GObject
except ImportError:
  import gobject as GObject

BUS_NAME = 'org.bluez'
PATH = '/org/bluez'
ADAPTER_INTERFACE = 'org.bluez.Adapter1'
HEALTH_MANAGER_INTERFACE = 'org.bluez.HealthManager1'
HEALTH_DEVICE_INTERFACE = 'org.bluez.HealthDevice1'

DBusGMainLoop(set_as_default=True)
loop = GObject.MainLoop()

bus = dbus.SystemBus()

type = 4103
if len(sys.argv) > 1:
	type = int(sys.argv[1])

hdp_manager = dbus.Interface(bus.get_object(BUS_NAME, PATH),
						HEALTH_MANAGER_INTERFACE)
app_path = hdp_manager.CreateApplication({"DataType": dbus.types.UInt16(type),
					"Role": "sink"})

print(app_path)

manager = dbus.Interface(bus.get_object(BUS_NAME, "/"),
					"org.freedesktop.DBus.ObjectManager")

objects = manager.GetManagedObjects()
adapters = []

for path, ifaces in objects.items():
	if ifaces.has_key(ADAPTER_INTERFACE):
		adapters.append(path)

i = 1
for ad in adapters:
	print("%d. %s" % (i, ad))
	i = i + 1

print("Select an adapter: ",)
select = None
while select == None:
	try:
		pos = int(sys.stdin.readline()) - 1
		if pos < 0:
			raise TypeError
		select = adapters[pos]
	except (TypeError, IndexError, ValueError):
		print("Wrong selection, try again: ",)
	except KeyboardInterrupt:
		sys.exit()

adapter =  dbus.Interface(bus.get_object(BUS_NAME, select),
						ADAPTER_INTERFACE)

devices = []
for path, interfaces in objects.items():
	if "org.bluez.Device1" not in interfaces:
		continue
	properties = interfaces["org.bluez.Device1"]
	if properties["Adapter"] != select:
		continue;

	if HEALTH_DEVICE_INTERFACE not in interfaces:
		continue
	devices.append(path)

if len(devices) == 0:
	print("No devices available")
	sys.exit()

i = 1
for dev in devices:
	print("%d. %s" % (i, dev))
	i = i + 1

print("Select a device: ",)
select = None
while select == None:
	try:
		pos = int(sys.stdin.readline()) - 1
		if pos < 0:
			raise TypeError
		select = devices[pos]
	except (TypeError, IndexError, ValueError):
		print("Wrong selection, try again: ",)
	except KeyboardInterrupt:
		sys.exit()

print("Connecting to %s" % (select))
device = dbus.Interface(bus.get_object(BUS_NAME, select),
						HEALTH_DEVICE_INTERFACE)

chan = device.CreateChannel(app_path, "Any")

print(chan)

print("Push Enter for finishing")
sys.stdin.readline()

hdp_manager.DestroyApplication(app_path)