×
Namespaces

Variants
Actions
(Difference between revisions)

Advertising Bluetooth services using Symbian C++

From Nokia Developer Wiki
Jump to: navigation, search
Forum Nokia KB (Talk | contribs)
m (Protected "RSdp" [edit=sysop:move=sysop])
Forum Nokia KB (Talk | contribs)

Revision as of 12:22, 2 May 2008


ID Creation date April 25, 2008
Platform S60 3rd Edition, MR Tested on devices Nokia N95
Category Symbian C++ Subcategory Bluetooth


Keywords (APIs, classes, methods, functions): RSdp, RSdpDatabase, TSdpServRecordHandle, RSdpDatabase::CreateServiceRecordL(), RSdpDatabase::UpdateAttributeL()

Overview

RSdp provides a session to the Service Discovery Database. Used to create subsessions to database functionality. A clients must create and connect a session, before using a RSdpDatabase subsession to access the database.

RSdpDatabase is subsession to the SDP through which service records and their attributes can be added, deleted, and updated.

Following example show how to advertise own (KBT_serviceID 0x10ff) bluetooth service. Discovering this particular service can be found from CSdpAgent.

Listening of the bluetooth connection is explained in Bluetooth_connection. Channel number parameter in CMyServiceAdvertiser::StartAdvertiserL(TInt aChannel) method comes there.

MMP file

The following capabilities and libraries are required:

CAPABILITY      LocalServices
LIBRARY sdpagent.lib
LIBRARY sdpdatabase.lib

Header file

#include <btsdp.h>
#include <bt_sock.h>
 
// The service id that identifies our service. This id will be
// used when advertising the service and discovering the service.
#define KBT_serviceID 0x10ff
 
// Service name and description for our service
_LIT(KBTServiceName, "BTpmp");
_LIT(KBTServiceDesc, "BTpmp");
 
// Service discovery protocol session
RSdp iSdp;
 
// Service discovery database (sdp)
RSdpDatabase iSdpDB;
 
// Service record
TSdpServRecordHandle iRecord;
 
// Service record state
TInt iRecordState;

Source file

Start service advertiser on given channel. Entry to service discovery database will be entered describing our advertised service.

void CMyServiceAdvertiser::StartAdvertiserL(TInt aChannel)
{
// Open sdp session
User::LeaveIfError(iSdp.Connect());
// Open sdp database session
User::LeaveIfError(iSdpDB.Open(iSdp));
 
// Create a record of the correct service class
TUUID serviceUUID(KBT_serviceID);
iSdpDB.CreateServiceRecordL(serviceUUID, iRecord);
 
// Add a protocol to the record
CSdpAttrValueDES* protocolDescriptorList = CSdpAttrValueDES::NewDESL(NULL);
CleanupStack::PushL(protocolDescriptorList);
 
TBuf8<1> channel;
channel.Append((TChar)aChannel);
 
// Create protocol list for our service
protocolDescriptorList
->StartListL() // list of protocols required for this method
->BuildDESL()
->StartListL()
->BuildUUIDL(KL2CAP)
->EndListL()
 
->BuildDESL()
->StartListL()
->BuildUUIDL(KRFCOMM)
->BuildUintL(channel)
->EndListL()
->EndListL();
 
// Set protocol list to the record
iSdpDB.UpdateAttributeL(iRecord, KSdpAttrIdProtocolDescriptorList,
*protocolDescriptorList);
CleanupStack::PopAndDestroy(protocolDescriptorList);
 
// Add a name to the record
iSdpDB.UpdateAttributeL(iRecord,
KSdpAttrIdBasePrimaryLanguage +
KSdpAttrIdOffsetServiceName,
KBTServiceName);
 
// Add a description to the record
iSdpDB.UpdateAttributeL(iRecord,
KSdpAttrIdBasePrimaryLanguage +
KSdpAttrIdOffsetServiceDescription,
KBTServiceDesc);
 
// Set service available
UpdateAvailabilityL(ETrue);
}

Set availability of our advertised service. Service record on the service discovery database will be updated accordingly.

void CMyServiceAdvertiser::UpdateAvailabilityL(TBool aAvailable)
{
TInt state = aAvailable ? 0xFF : 0x00;
 
// Set availability
iSdpDB.UpdateAttributeL(iRecord, KSdpAttrIdServiceAvailability, state);
 
// Mark record changed
iSdpDB.UpdateAttributeL(iRecord, KSdpAttrIdServiceRecordState,
++iRecordState);
}

Stop advertising service

void CMyServiceAdvertiser::StopAdvertiserL()
{
if ( iRecord!=NULL )
{
// Delete out record from service discovery database
iSdpDB.DeleteRecordL(iRecord);
 
// Close sdp and sdp db sessions
iSdpDB.Close();
iSdp.Close();
iRecord=NULL;
}
}


Postconditions

Own bluetooth service is advertised and can be discovered according to CSdpAgent.

See also

RHostResolver Bluetooth device discovering
Bluetooth_connection Bluetooth connection listening
CSdpAgent Bluetooth service discovery
RSdp Bluetooth service advertising

133 page views in the last 30 days.
×