Listening for incoming Bluetooth connections

This document describes how a Bluetooth device can allow incoming remote device connections.

Incoming Bluetooth connection examples include:

  • one end of a chat application,

  • the Audio Gateway role in HSP (licensee),

  • or the Gateway in DUNP (licensee).

Incoming requests for a Bluetooth connection are handled through the RSocket API (as with outgoing connections).

To receive an incoming connection a service must open a listening socket, register the socket with the Bluetooth security manager, and advertise the socket, then connection requests can be accepted.

How to set up an incoming connection

The steps are given below:

Get a connection

Connect to the Sockets Server and select the protocol to be used.

RSocketServ socketServ;
_LIT(KL2Cap, "L2CAP"); // or RFCOMM as appropriate

Open a socket

Open a socket for that protocol.

RSocket listen;

Create the Bluetooth socket address

Create a Bluetooth socket address object (TBTSockAddr) and set its port to the PSM or server channel (for L2CAP or RFCOMM respectively) and bind the socket to this address. Note you do not have to set the local Bluetooth device address in the address.

TBTSockAddr addr;

Register with the Bluetooth security manager

Add the connection to the Bluetooth security manager. Incoming connections will not work unless they have been registered with the Bluetooth security manager, thus allowing the incoming traffic through the security wall, which by default denies access to all connection attempts.

RBTSecuritySettings secset;
TRequestStatus status;
TBTServiceSecurity serviceSecurity(aMyUid,KSolBtRFCOMM,aChannel);
User::LeaveIfError(secset.RegisterService(serviceSecurity, status));

The security profile is created and packaged in serviceSecurity and applied using RBTSecuritySettings::RegisterService(). The security profile overrides the default security settings thereby allowing incoming connections.

Add to the Bluetooth Service Discovery Database

Enter record into the database.
RSdpDatabase sdprec;
TSdpServRecordHandle recordHandle = 0;
sdprec.CreateServiceRecordL(*UUIDlist, recordHandle);

Start listening for connections

Tell the socket to listen for incoming connections using RSocket::Listen().

Start listening for data over a connection

Create a blank socket and pass it to the listening socket through RSocket::Accept(). When this call completes, the socket passed in a parameter is now fully connected and can be used to send and receive data. The listening socket remains in place, ready for another socket to be passed in when the program can handle another connection.
RSocket accept;
TRequestStatus status;

Shutdown remote Bluetooth connection

Shutdown connection and unregister security settings. When the receiver wishes to shutdown, it must ensure that it closes the listening socket as well as any connected ones. This releases the Bluetooth connection to other applications.

secset.UnregisterService(KSolBtL2CAP, status);

The socket server is closed using the RHandleBase::Close() method. It will close the handle to the socket server and destroy it if there are no other referencing objects.


If the channel is already in use, as indicated if Bind() returns an error, you can find a free channel using the KRFCOMMGetAvailableServerChannel ioctl.

Also search the Symbian DevNet for white papers and example applications.

Finding more...

For more information on:

  • setting security requirements for incoming connections to the service, see [xref href="GUID-A035B592-F423-5980-8E7E-E726CF24CF3E"]Using Bluetooth Security Manager[/xref]

  • advertising the service's availability, see [xref href="GUID-756D831F-F302-594C-8116-144358DD8442"]Using Bluetooth Service Discovery Database[/xref]

Where Next?

This tutorial set takes you through all the steps involved in setting up and communicating over a Bluetooth connection.