Received SMS with python

Aleksander Morgado aleksander at
Tue Sep 19 16:39:30 UTC 2017

>> I came across an old example ( using dbus, but are there any
>> examples via gi/Glib for reading and deleting received SMSs (and any events
>> that can be hooked for catching the reception)?
>> I'm guessing the ModemMessaging class is the starting point? Then, in the
>> Sms class, when would be the times to use the 'dup_' functions instead of
>> 'get_'?
>> Thanks
> So far I've proven out some *basic* principles (sensible checking omitted for clarity) on a python command line:
> import gi
> gi.require_version('ModemManager', '1.0')
> from gi.repository import GLib, GObject, Gio, ModemManager
> connection = Gio.bus_get_sync (Gio.BusType.SYSTEM, None)
> manager = ModemManager.Manager.new_sync (connection, Gio.DBusObjectManagerClientFlags.DO_NOT_AUTO_START, None)
> for obj in manager.get_objects():
>    modem.get_manufacturer()
> msging = obj.get_modem_messaging()
> rcvd =  msging.list_sync()
> for m in rcvd:
>     print m.get_number(), m.get_timestamp(), '"%s"' % m.get_text()
> But I have noticed that if a new SMS is then received, this doesn't get reflected in list_sync()  - I seem to have to repeat all the way back to the Manager.new_sync() instantiation for it to ripple through?

The list_sync() operation is not a command sent to ModemManager for
which we get a reply. The list_sync() operation will create SMS
objects for each SMS path that is found in the "Messages" property:

See the list_sync() implementation here:

If you create a Modem object using the sync API you're getting a
"snapshot" of the state of the modem. In order for the snapshot to be
updated on real time (i.e. in order to have the "Messages" property
get updated with newly received messages), you need a GLib main loop
running, otherwise you're acting over and over on the same snapshot.

You could try to; e.g. run list_sync() peridically in a timeout
scheduled e.g. every 10s, and have the GLib main loop manage the
timeout source as well. That will make the modem object receive
property updates asynchronously and you'll see the modem state update

Another (better?) option would be to listen to updates in the
"Messages" property itself. E.g. just
msging.connect('notify::messages',...) and setup a callback that will
get called any time the Messages property is updated (and you can call
list_sync() inside the callback). If you get a clean example with that
logic we could add it to the examples/ directory in git ;)


More information about the ModemManager-devel mailing list