![Symbian Developer Library](../../../../../../../a_stock/images/mainheading.gif)
![]() |
![]() |
|
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.
The steps are given below:
Connect to the Sockets Server and select the protocol to be used.
RSocketServ socketServ;
socketServ.Connect();
_LIT(KL2Cap, "L2CAP"); // or RFCOMM as appropriate
Open a socket for that protocol.
RSocket listen;
listen.Open(socketServ,KL2Cap);
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;
addr.SetPort(KListeningPSM);
User::LeaveIfError(listen.Bind(addr));
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);
serviceSecurity.SetAuthentication(EFalse);
serviceSecurity.SetEncryption(EFalse);
serviceSecurity.SetAuthorisation(EFalse);
serviceSecurity.SetDenied(EFalse);
User::LeaveIfError(secset.RegisterService(serviceSecurity, status));
User::WaitForRequest(status);
test(status.Int()==KErrNone);
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.
Enter record into the database.
RSdpDatabase sdprec;
TSdpServRecordHandle recordHandle = 0;
sdprec.CreateServiceRecordL(*UUIDlist, recordHandle);
Tell the socket to listen for incoming connections using
RSocket::Listen()
.
User::LeaveIfError(listen.Listen(2));
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;
User::LeaveIfError(accept.Open(socketServ));
listen.Accept(accept,status);
User::WaitForRequest(status);
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.
accept.Close();
listen.Close();
secset.UnregisterService(KSolBtL2CAP, status);
socketServ.Close();
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.
For more information on:
setting security requirements for incoming connections to the service, see Using Bluetooth Security Manager.
advertising the service's availability, see Using Bluetooth Service Discovery Database.
Working with sockets, see Using Socket Clients.
Also search the Symbian DevNet for white papers and example applications.
This tutorial set takes you through all the steps involved in setting up and communicating over a Bluetooth connection.
Listening for Incoming Bluetooth Connections - This document