Network programming for PyS60 (VI)

by Marcelo Barros

Before presenting some server code it is important to discuss about access points selection. When some socket calls like connect() are executed, a dialog is presented, allowing users to choose which connection they want to use. To avoid further connection dialogs or even for specifying exactly where you want to put your server, it is interesting to set your access point.

Python for S60 has four special functions for dealing with this issue in the socket module (Python 1.4.x) or btsocket module (Python 1.9.x). From Pyhton for S60 documentation:

  • select_access_point(): opens a popup selection where access points are listed and can be selected. Returns selected access point id.
  • access_point(apid): creates access point object by given apid. Returns access point object.
  • set_default_access_point(ap): sets the default access point that is used when socket is opened. Setting apo to “None” will clear default access point.
  • access_points(): lists access points id’s and names that are available.

The basic idea is to create an access point object from an access point ID. Thus, this object is used to set the default access point. For instance:

>>> import btsocket
>>> api=btsocket.select_access_point()
>>> print api
>>> apo = btsocket.access_point(api)
>>> btsocket.set_default_access_point(apo)

Defaut access point selection dialog

Defaut access point selection dialog

The access point object has three interesting methods:

>>> dir(apo)
['ip', 'start', 'stop']

With start() it is possible to activate this access point. Run it and after some seconds the phone will show the connection icon at phone home screen. If you are using DHCP, the IP will be negociated in this phase and it may be retrieved with apo.ip(). Finally, the access point connection may be interrupted with apo.stop().

Home screen showing connection icon

Home screen showing connection icon

Nice ? But how about to create your own selection point popup using access_points() ? The following code snippet should work with Python 1.4.x and 1.9.x:

# -*- coding: utf-8 -*-
import sys
from appuifw import *
    # Try to import 'btsocket' as 'socket' (just for 1.9.x)
    sys.modules['socket'] = __import__('btsocket')
except ImportError:
import socket
def sel_access_point():
    """ Select the default access point.
        Return True if the selection was done or False if not
    aps = socket.access_points()
    if not aps:
        note(u"No access points available","error")
        return False
    ap_labels = map(lambda x: x['name'], aps)
    item = popup_menu(ap_labels,u"Access points:")
    if item is None:
        return False
    apo = socket.access_point(aps[item]['iapid'])
    return True

Your customized dialog:

Customized access point dialog

Customized access point dialog

So, that is all ! Next post we will strike again with networking programming, using all we have learned until now. See you there !

Related posts:

  1. Network programming for PyS60 (VIII) Did you do your homework ? So, I would like...
  2. Network programming for PyS60 (VII) Everything is about "protocols" in computer networks, doesn't it ?...
  3. Network programming for PyS60 (XIV) Have you already heard about Beautiful Soup ? Beautiful Soup...
  4. Network programming for PyS60 (XII) Until now we have used only TCP in our examples...
  5. Network programming for PyS60 (XIII) In our last post we talked about multicast, a special...

Related posts brought to you by Yet Another Related Posts Plugin.