4.7 btsocket -- Provides Bluetooth (BT) support

Availability: S60.

The socket module of the previous PyS60 releases has been renamed as btsocket. For information on the usage of this module refer to Ensymble README. The following related constants and functions are defined:

Note: In release 1.0 the functions bt_advertise_service, bt_obex_receive, and bt_rfcomm_get_available_server_channel incorrectly expected to be given the internal e32socket.socket object as the socket parameter instead of the proper socket object. Now the functions work correctly. The old calling convention is still supported but it is deprecated and may be removed in a future release.

AF_BT

Represents the Bluetooth address family.

BTPROTO_RFCOMM

This constant represents the Bluetooth protocol RFCOMM.

RFCOMM
OBEX

Bluetooth service classes supported by bt_advertise_service.

AUTH
ENCRYPT
AUTHOR

Bluetooth security mode flags.

bt_advertise_service( name, socket, flag, class)

Sets a service advertising the service name (Unicode) on local channel that is bound to socket. If flag is True, the advertising is turned on, otherwise it is turned off. The service class to be advertised is either RFCOMM or OBEX.

bt_discover( [address])

Performs the Bluetooth device discovery (if the optional BT device address is not given) and the discovery of RFCOMM class services on the chosen device. Returns a pair: BT device address, dictionary of services, where Unicode service name is the key and the corresponding port is the value.

bt_obex_discover( [address])

Same as discover, but for discovery of OBEX class services on the chosen device.

bt_obex_send_file( address, channel, filename)

Sends file filename (Unicode) wrapped into an OBEX object to remote address, channel.

bt_obex_receive( socket, filename)

Receives a file as an OBEX object, unwraps and stores it into filename (Unicode). socket is a bound OBEX socket.

bt_rfcomm_get_available_server_channel( socket)

Returns an available RFCOMM server channel for socket.

set_security( socket, mode)

Sets the security level of the given bound socket. The mode is an integer flag that is formed using a binary or operation of one or more of: AUTH (authentication), ENCRYPT, AUTHOR (authorization). Example: set_security(s, AUTH | AUTHOR).

Note: When listening to a Bluetooth socket on the phone, it is necessary to set the security level.

For examples on the usage of these functions, see Programming with Python for S60 Platform [].

Setting default Access Point (AP) has been added to the standard socket module. The following related constants and functions are defined:

select_access_point( )
This opens popup selection where access points are listed and can be selected. Returns selected access point id.

access_point( apid)
This creates access point object by given apid. Returns access point object.

set_default_access_point( apo)
This sets the default access point that is used when socket is opened. Setting apo to "None" will clear default access point.

access_points( )
This lists access points id's and names that are available.

Example 1:

import btsocket
#access point is selected from the list
apid = btsocket.select_access_point()
apo = btsocket.access_point(apid)
btsocket.set_default_access_point(apo)

s = btsocket.socket(btsocket.AF_INET, btsocket.SOCK_STREAM)
print apo.ip()
s.connect(('www.sourceforge.net',80))
s.send('GET /\r\n\r\n')
s.recv(100)
s.close()
apo.stop()

Example 2:

import btsocket
#Access point id is already known
apo = btsocket.access_point(1)
btsocket.set_default_access_point(apo) 

s = btsocket.socket(btsocket.AF_INET, btsocket.SOCK_STREAM)
s.connect(('www.sourceforge.net',80))
s.send('GET /\r\n\r\n')
s.recv(100)
s.close()
apo.stop()

Example 3:

import btsocket
#display interface ip.
#access point is selected from the list
apid = btsocket.select_access_point()
apo = btsocket.access_point(apid)
apo.start()
#Note that ip-address is given by operator, if static ip-address is not defined,
#when connection is started
print apo.ip()
#When connection is closed dynamic ip-address is released
apo.stop()

See About this document... for information on suggesting changes.