HomePython Page 4 - Bluetooth Programming in Python: Network Programming using RFCOMM
Developing the Client - Python
In the last article I discussed various Bluetooth profiles. If one wants to create a client-server based application using Bluetooth, then one should program for the RFCOMM profile. RFCOMM offers a socket-based client-server paradigm for providing services.
The steps necessary to developing the client are almost the same as those we needed to take to develop the server. The steps are
Create a socket
Connect to a device
Sending/receiving data
The first and third steps are same as that of creating the server. So only the second step needs scrutinizing. I explain the details below.
Create a socket
Just as with a server socket, to create a client socket the BluetoothSocket() method needs to be called with RFCOMM as the protocol. So, to create a socket that would connect to a server, the statement will be
To connect to a server, the client needs to know the address of the server. With Bluetooth, the address will be the address of the device, which is of the form "XX:XX:XX:XX:XX". So, to connect to a server running on a device with an address of "01:23:45:67:89:AB", the connect() method needs to be called on the socket object with the port number and address of the server. For example, if the server port number is 4000 and the address is "01:23:45:67:89:AB", then the statement to connect to it is
address="01:23:45:67:89:AB"
port=4000
client_sock.connect((address, port))
Sending/receiving data
At the client side too, the way to send and /or receive data is the same as that at the server side. So, to receive any data from the server, the statement would be
data = client_sockect.recv(1024)
print "received [%s]" % data
That completes the steps necessary to create a client and a server. Next, let us see how to develop a server that transfers a file using Bluetooth and RFCOMM.