In the last article I discussed about various Bluetooth profiles. If one wants to create a client-server based application using Bluetooth, then one should program for the RFCOMM profile. RFCOMM provides socket based client-server paradigm for providing services. In this article, I will focus on creating networked based application using RFCOMM. The first section will be about the whys and wherefores of RFCOMM. In the second section, I will enumerate the steps to create client-server based application using RFCOMM. In the last section, a single-threaded server will be developed that would use RFCOMM to provide a file transfer service. That’s the outline for this discussion.
RFCOMM – The Whys and Wherefores:
The term or abbreviation RFCOMM comes from the terms Radio Frequency or RF. The main aspect of RFCOMM is emulation of serial port communication using Radio Frequency, so the name RFCOMM. The serial port emulated by RFCOMM is RS-232 which has 9-circuits. RFCOMM uses the baseband of Bluetooth to provide reliable and in-sequence delivery of data stream. The main attributes of RFCOMM are:
1. It provides multiple concurrent connections. It does this by relying on L2CAP.
L2CAP can handle multiplexing over a single connection.
2. It supports flow control on separate and individual channels.
3. It does not provide error-control. The assumption that RFCOMM makes is L2CAP
will be providing error-free channel.
4. How devices should communicate using RFCOMM is decided by Serial Port Profile
(SPP), which is one of the Bluetooth profiles.
RFCOMM divides devices into two major classes. They are:
1. Type I
2. Type II
The division is based on whether the port is physically present or it is emulated. Here are the details
1. Type I:
The devices of this type have emulated serial ports. Emulated ports are entities used to map system specific services and their API to the RFCOMM services. Therefore, whenever applications are built for RFCOMM, then Type I devices are used. In other words, Type I Devices enable programmer to use serial port even if there is no physical serial port.
2. Type II:
These devices have physical serial ports. They act as intermediate devices. In other words, they are proxy for relaying transmissions from RFCOMM to an external RS-232 interfaces that may be linked with other devices.
As I have said before, RFCOMM provides a client-server based paradigm. In case of PyBluez, the client-server is based on sockets. And the steps to create them are similar to the steps that one would follow in creating TCP/IP or UDP based sockets. The next section is about those steps. Lets get started with the steps.
Developing Applications for RFCOMM – Step by Step:
The steps to develop RFCOMM based applications can be divided into two main set of steps. They are:
1. Creating the Server
2. Creating the Client
Each of the steps can be again divided into sub-steps. Let us take each step one at a time.
1. Creating the Server:
The server of RFCOMM applications, are essentially, Bluetooth based services. This has to be kept in mind when developing the server. The importance of this point will become clear in the steps regarding the client. Creating the server can be further divided into following steps
a. Creating the Server Socket
b. Binding to a port
c. Listening for requests
d. Accepting the requests
e. Sending data
The steps are same as that for creating TCP/IP-based server. However, the first and last step differs in the case of RFCOMM. The details are as follows
a. Creating the Server Socket:
First step is to create the socket that would listen for and accepts incoming requests and creates the connection. The sockets one would use, when working with RFCOMM, are Bluetooth sockets. To create a Bluetooth socket, BluetoothSocket needs to be called with the protocol to be used. In this case it is RFCOMM. So, in order to create a socket named server_socket the statement will be:
server_socket= Bluetooth.BluetoothSocket(Bluetooth.RFCOMM)
This statement only creates a simple Bluetooth based socket that uses RFCOMM for communication. The next three steps make it a server socket.
b. Binding to a Port:
This is the second step to make a simple socket work as server socket. The socket object needs to be bound to an address and a port so that it can start listening for requests. However, since the socket will be communicating over Bluetooth, IP address is not required. PyBluez will use the address of the device on which it is running. If the device is a Desktop PC, then the address will correspond to the address provided by the Bluetooth adapter or dongle. To bind a socket object to a port, bind() method needs to b called on the socket object. The argument passed is a tuple containing the address and the port no. with which the socket has to be bound. For example, to bind a socket to a port, say 11, the statement will be
server_socket.bind((“”,11))
One point to keep in mind while working with RFCOMM is that RFCOMM uses ports between 1-30 only.
c. Listening for requests:
Once a socket has been bound to a port, next step is to make it listen for
incoming requests. To do this, listen() method needs to be called on the socket object. The listen() method accepts no. of requests to be kept in queue as the argument. For example, to make a socket start listening with a queue of size 3, the statement will be
server_socket.listen(3)
d. Accepting the requests:
Once a request is received, it has to be accepted so that communication can start. To do so, one has to call accept() method on the socket object. The accept() method doesn’t have any arguments. It returns a tuple containing the address of the client and socket object through which further communication can be done. So the statement to accept connection is
client_socket,address=server_socket.accept()
e. Sending/receiving data:
The last step is sending and/or receiving data. If the server is on the desktop pc or normal pc, then sending and receiving can be done using Python library. However, if the server is on say, a smart phone, then the library required will be based on the OS of the smart phone. For a normal PC, one would have to call send() and recv() methods on the socket object returned by the accept() method. Both accept string as arguments. For example, if server wants to send a message say, “hello”, the code will be
client_socket.send(“Hello from server”)
That completes server part. Next comes the client.
2. Developing the Client
The steps to develop the client are almost common to that of developing the server. The steps are
a. Create a socket
b. Connect to a device
c. Sending/receiving data
The first and third steps are same as that of creating the server. So only second step needs scrutinizing. Here are the details
a. Create a socket
Just as with server socket, to create a client socket BluetoothSocket() needs to be called with RFCOMM as the protocol. So, to create a socket that would connect to a server, the statement will be
client_socket= Bluetooth.BluetoothSocket(Bluetooth.RFCOMM)
b. Connect to a device:
To connect to a server, client needs to know the address of the server. In case of 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”, connect() method needs to be called on socket object with the port no and address of the server. For example, if the server port no is 4000 and 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))
c. Sending/receiving data:
At the client side too, the way to send and /or receive data is same as that at the server-side. So, to receive any data from the server, the statement will be
data = client_sockect.recv(1024)
print “received [%s]” % data
That completes the steps to create a client and a server. Next, let us see how to develop a server that transfers a file using Bluetooth and RFCOMM.
RFCOMM in Real World:
The server will service only one client at a time. So, it is neither multithreaded nor multi-process based. Let us start. First comes the imports
from bluetooth import *
Then comes the class that will contain the server functionalities. Its constructor will take port no on which the server has to listen. It will also call the create_server function.
from bluetooth import *
class rfcomm_server:
def __init__(self,port):
self.port=port
self.create_server()
Next is the create_server method that will create a RFCOMM based socket and make it listen on the passed port. It then calls the start_server method.
from bluetooth import *
class rfcomm_server:
def __init__(self,port):
self.port=port
self.create_server()
def create_server(self):
self.server_socket=\
Bluetooth.BluetoothSocket(Bluetooth.RFCOMM)
self.server_socket.bind((“”,self.port))
self.start_server()
Next is the start_server method. This method makes the server listen on the port and then starts the connection. After that it asks the user for the file and then transfers it.
from bluetooth import *
class rfcomm_server:
def __init__(self,port):
self.port=port
self.create_server()
def create_server(self):
self.server_socket=\
Bluetooth.BluetoothSocket(Bluetooth.RFCOMM)
self.server_socket.bind((“”,self.port))
self.start_server()
def start_server(self):
while true:
self.server_socket.listen(3)
self.client_socket,address=server_socket.accept()
self.client_socket.send(“Enter the file name”)
file_name= self.client_socket.recv(2048)
if file_name is not None:
transfer_data=open(file_name,’r’).readlines()
for data in transfer_data:
self.client_socket.send(data)
self.client_socket.send(“Transfer complete”)
Next, let us start the server. To do that first we need to check whether main function is being executed. If it is being executed, then create the instance of server.
from bluetooth import *
class rfcomm_server:
def __init__(self,port):
self.port=port
self.create_server()
def create_server(self):
self.server_socket=\
Bluetooth.BluetoothSocket(Bluetooth.RFCOMM)
self.server_socket.bind((“”,self.port))
self.start_server()
def start_server(self):
while true:
self.server_socket.listen(3)
self.client_socket,address=server_socket.accept()
self.client_socket.send(“Enter the file name”)
file_name= self.client_socket.recv(2048)
if file_name is not None:
transfer_data=open(file_name,’r’).readlines()
for data in transfer_data:
self.client_socket.send(data)
self.client_socket.send(“Transfer complete”)
if ‘__name__’==’__main__’:
server=rfcomm_server(20)
That completes the application. It also brings us to the end of this discussion. The discussion until now has not touched upon the topic of service discovery. The next part of discussion will be about service discovery. Till then…

how are you I was fortunate to seek your blog in yahoo
your topic is quality
I get a lot in your Topics really thanks very much
btw the theme of you site is really marvelous
where can find it
I think you got talent in writing posts. Waiting for more posts
I’m glad that i found your website, there are a couple of cool posts
You made some good points here.Keep us posting. What template do you use in your site
i like it
Thanks for this! I’ve been searching all over the web for the info.
Good post, I have book marked this blog so ideally I will discover much more on this subject in the foreseeable future!