我試圖獲取列表中的USB設備在Ubuntu 10.10當前插入和監控發生變化,比如或縮小使用udev和d-BUS被插入的設備。我對使用D-BUS編程相當陌生。我看到一個例子:Linux : How to detect is usb keyboard is plugged and unplugged只有一個使用HAL,我知道HAL已被棄用。我發現了一些工作代碼,稍加修改,只適用於任何設備,如USB棒,媒體播放器或光盤設備。我希望整個事情鼠標,鍵盤,USB相機充電器任何插入到USB我想我的程序知道它。這基本上就是我所擁有的(http://moserei.de/2010/01/08/accessing-devicekit-with-dbus-and-python.html):USB設備udev和d-BUS
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()
任何幫助都會被認可。預先感謝您, Calota羅密歐
供未來參考(我正在尋找這個代碼,而不是通用的USB處理程序),您需要更改: iface = dbus.Interface(proxy,「org.freedesktop.UDisks.Device」) 至少在使用udisks 1.0.4時,iface = dbus.Interface(代理,「org.freedesktop.UDisks」) 到 。 – 2012-11-27 10:26:24