Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I am trying to get a list of currently plugged in USB devices in Ubuntu 10.10 and monitor changes that happen, like devices being plugged in or out using udev and D-Bus.

I'm fairly new to programming using D-Bus. I saw one example: "Linux: How to detect is usb keyboard is plugged and unplugged". Problem is that it uses HAL and I know that HAL is deprecated.

I found some working code, but it's working only with storage devices such as USB sticks, media players or CD-ROM drives. I want the whole thing: mice, keyboards, USB cameras, chargers; anything that is plugged in to the USB.

How can I listen D-Bus events for any USB device plug and unplug?

This is basically what I have now (also):

import dbus
import gobject
from dbus.mainloop.glib import DBusGMainLoop

def device_added_callback(device):
    print 'Device %s was added' % (device)

def device_changed_callback(device):
    print 'Device %s was changed' % (device)

#must be done before connecting to DBus
DBusGMainLoop(set_as_default=True)

bus = dbus.SystemBus()

proxy = bus.get_object("org.freedesktop.UDisks", 
                       "/org/freedesktop/UDisks")
iface = dbus.Interface(proxy, "org.freedesktop.UDisks.Device")

devices = iface.get_dbus_method('EnumerateDevices')()

print '%s' % (devices)

#addes two signal listeners
iface.connect_to_signal('DeviceAdded', device_added_callback)
iface.connect_to_signal('DeviceChanged', device_changed_callback)

#start the main loop
mainloop = gobject.MainLoop()
mainloop.run()
share|improve this question
    
I think this belongs on SO. –  mathepic Feb 24 '11 at 20:45
    
Have you found something relevant that you can share as an answer to your own question? I am trying to do something similar, but I can't piece together the vague answers around the web. –  Redsandro Jan 3 '13 at 14:10
add comment

1 Answer

up vote 1 down vote accepted

I don't have much experience with DBus or udev, but pyudev looks very promising.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.