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:
Get a connection
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
Open a socket for that protocol.
RSocket listen; listen.Open(socketServ,KL2Cap);
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; addr.SetPort(KListeningPSM); User::LeaveIfError(listen.Bind(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); 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.
Add to the Bluetooth Service Discovery 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().User::LeaveIfError(listen.Listen(2));
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; User::LeaveIfError(accept.Open(socketServ)); listen.Accept(accept,status); User::WaitForRequest(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.
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.
Also search the Symbian DevNet for white papers and example applications.
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]
Copyright ©2010 Nokia Corporation and/or its subsidiary(-ies).
All rights
reserved. Unless otherwise stated, these materials are provided under the terms of the Eclipse Public License
v1.0.