Signals

Robert Rawlins - Think Blue robert.rawlins at thinkbluemedia.co.uk
Wed Apr 18 10:31:33 EDT 2007


Thanks Carsten,

Sorry for not mentioning the dbus before, I thought that the signals were a general python thing, I didn’t realize it was a specific dbus thing.

I've now modified the code so it looks like this.

#!/usr/bin/python

import dbus
from gobject import MainLoop, idle_add
from dbus.mainloop.glib import DBusGMainLoop

def main_function():
	bus = dbus.SystemBus()
	manager = dbus.Interface(bus.get_object('org.bluez', '/org/bluez'),'org.bluez.Manager')
	adapter = dbus.Interface(bus.get_object('org.bluez', manager.DefaultAdapter()),'org.bluez.Adapter')

	def remote_device_found(addr, class_, rssi):
		print 'Found:', addr

	adapter.connect_to_signal('RemoteDeviceFound', remote_device_found)

	adapter.DiscoverDevices()

if __name__ == '__main__':
    idle_add(main_function)
    dbus_mainloop_wrapper = DBusGMainLoop(set_as_default=True)
    mainloop = MainLoop()
    mainloop.run()

Which should implement the main loop and keep the application alive, however when running the program I get the following error thrown at me.

File "./scan4.py", line 5, in ?
    from dbus.mainloop.glib import DBusGMainLoop
ImportError: No module named mainloop.glib

Any ideas what might be causing this? My guess is that I don’t have one of the dependency installed properly, but I have no idea which ones I need.

Thanks for any more help you can offer, its greatly appreciated.

Rob 
-----Original Message-----
From: Carsten Haese [mailto:carsten at uniqsys.com] 
Sent: 18 April 2007 13:43
To: Robert Rawlins - Think Blue
Cc: python-list at python.org
Subject: Re: Signals

On Wed, 2007-04-18 at 08:39 +0100, Robert Rawlins - Think Blue wrote:
> Hello Chaps,
> 
>  
> 
> I posted about this the other day but I’m still struggling to get any
> form of result from it. Basically I have the following piece of code,
> and according to its API is produces singals when particular events
> occur, but i have no idea how to list for events, I’ve tried all sorts
> of combinations using the signal module but haven’t been able to get
> it working.
> [...]
> import dbus, signal
> [...]
>
> Now here is a copy of the API documentation that lists the signals
> thrown by the API, this is the one I’m trying to listen for.
> 
>  
> 
>         void RemoteDeviceFound(string address, uint32 class, int16
> rssi)
> 
>  
> 
>                This signal will be send every time an inquiry result
> 
>                has been found by the service daemon. In general they
> 
>                only appear during a device discovery.
> 
>  
> 
> Basically I’m just looking to run a function that will print those
> details to screen. Can anyone help with working out how to listen for
> this signal?

The signal module is for handling process signals the operating system
sends to your python process. The API you're using (dbus, a crucial
detail you neglected to mention before) doesn't send this kind of
signal.

Googling for 'python dbus documentation' brings up
http://dbus.freedesktop.org/doc/dbus-python/api/ , which seems to
explain everything you need. You need to get an instance of
dbus.Interface, which you seem to have done, and call its
connect_to_signal() method, which you don't seem to have done.

>  I’d also like to know how i can keep the application running until I
> decided to stop it manually, as the DiscoverDevices() can take a while
> to complete and send out several RemoteDeviceFound() signals in that
> period.

The very first section of the above mentioned documentation talks about
needing a main loop for receiving signals, and it provides example
boilerplate code for how to set up a main loop.

Hope this helps,

Carsten.






More information about the Python-list mailing list