Description
This page contains examples showing how manage HID devices using the BlueZ input service. Once the input device is created, it will reconnect automatically to the host when a key is pressed or moving the mouse.
Activating Input service
When a client wants to start the input service or discover the input service bus "id" address the ActivateService? method should be called. This "id" is required to send message to manage the devices.
import dbus bus = dbus.SystemBus() bmgr = dbus.Interface(bus.get_object('org.bluez', '/org/bluez'), 'org.bluez.Manager') bus_id = bmgr.ActivateService('input') # All input messages should be sent to this address print bus_id
Command line service activation:
$dbus-send --system --type=method_call --print-reply --dest=org.bluez \ /org/bluez org.bluez.Manager.ActivateService string:input Use the replied BUS ID in the next commands to setup the destination parameter.
Device Creation/Connection
After create the device, host and HID initiated connections will be enabled. If "Already exists" and device doesn't reconnect automatically means that probably the device is not "virtually cabled" to the host, re-create(RemoveDevice? + CreateDevice?) the input device will solve this problem.
import dbus bus = dbus.SystemBus() # service activation bmgr = dbus.Interface(bus.get_object('org.bluez', '/org/bluez'), 'org.bluez.Manager') bus_id = bmgr.ActivateService('input') imgr = dbus.Interface(bus.get_object(bus_id, '/org/bluez/input'), 'org.bluez.input.Manager') # device creation path = imgr.CreateDevice('xx:xx:xx:xx:xx:xx') idev = dbus.Interface (bus.get_object(bus_id, path), 'org.bluez.input.Device') # host initiated connection # idev.Connect()
Command line usage examples:
# device creation $dbus-send --system --type=method_call --print-reply --dest=":X.Y.Z" \ /org/bluez/input org.bluez.input.Manager.CreateDevice string:00:11:22:33:44:55 # host initiated connection $dbus-send --system --type=method_call --print-reply --dest=":X.Y.Z" \ /org/bluez/input/keyboard0 org.bluez.input.Device.Connect
Removing Devices
import dbus bus = dbus.SystemBus() # service activation bmgr = dbus.Interface(bus.get_object('org.bluez', '/org/bluez'), 'org.bluez.Manager') bus_id = bmgr.ActivateService('input') imgr = dbus.Interface(bus.get_object(bus_id, '/org/bluez/input'), 'org.bluez.input.Manager') devlist = imgr.ListDevices() # removing all devices objects and file system stored data for dev in devlist: imgr.RemoveDevice(dev)
Command line example to remove the HID device object and stored information:
$dbus-send --system --type=method_call --print-reply --dest=":X.Y.Z" \ /org/bluez/input org.bluez.input.Manager.RemoveDevice string:"/org/bluez/input/keyboardX"
RemoveDevice? will remove the persistent input storage data and it will send the virtual cable unplug command if the device is connected.
