×
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)
Forum Nokia KB (Talk | contribs)
Line 1: Line 1:
 
__NOTOC__
 
__NOTOC__
 
__NOEDITSECTION__
 
__NOEDITSECTION__
 
+
{{KBCS}}
 
{|style="background:#eceff2" width="660px" border="1" cellpadding="5" cellspacing="0"  
 
{|style="background:#eceff2" width="660px" border="1" cellpadding="5" cellspacing="0"  
 
|-
 
|-
|'''ID''' ||
+
|'''ID''' ||CS000938
|'''Creation date''' || April 25, 2008
+
|'''Creation date''' || May 2, 2008
 
|-
 
|-
 
|'''Platform''' || S60 3rd Edition, MR
 
|'''Platform''' || S60 3rd Edition, MR
Line 22: Line 22:
  
 
==Overview==
 
==Overview==
<tt>RSdp</tt> provides a session to the Service Discovery Database.
+
<tt>RSdp</tt> provides a session to the Service Discovery Database. It is used to create subsessions to database functionality. A client must create and connect a session before using a RSdpDatabase subsession to access the 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.
+
  
<tt>RSdpDatabase</tt> is subsession to the SDP through which service  
+
<tt>RSdpDatabase</tt> is a subsession of the SDP through which service records and their attributes can be added, deleted, and updated.
records and their attributes can be added, deleted, and updated.
+
  
Following example show how to advertise own (KBT_serviceID 0x10ff)
+
The following example shows how to advertise a certain (KBT_serviceID 0x10ff) Bluetooth service. Discovering this particular service is explained in [[CS9000937 - Discovering Bluetooth services]].
bluetooth service. Discovering this particular service can be
+
found from [[CSdpAgent]].
+
  
Listening of the bluetooth connection is explained in [[Bluetooth_connection]].
+
Listening to the Bluetooth connection is explained in [[CS000939 - Establishing a Bluetooth connection]]. The channel number parameter in <tt>CMyServiceAdvertiser::StartAdvertiserL(TInt aChannel)</tt> method comes there.
Channel number parameter in
+
<tt>CMyServiceAdvertiser::StartAdvertiserL(TInt aChannel)</tt> method
+
comes there.
+
  
 
==MMP file==
 
==MMP file==
Line 52: Line 43:
 
#include <bt_sock.h>
 
#include <bt_sock.h>
  
// The service id that identifies our service. This id will be  
+
// The service id that identifies the service. This id will be  
 
// used when advertising the service and discovering the service.
 
// used when advertising the service and discovering the service.
 
#define KBT_serviceID 0x10ff
 
#define KBT_serviceID 0x10ff
Line 74: Line 65:
  
 
==Source file==
 
==Source file==
Start service advertiser on given channel. Entry to service discovery
+
Start the service advertiser on a given channel. Entry to service discovery database will be entered describing the advertised service.
database will be entered describing our advertised service.
+
 
<code cpp>
 
<code cpp>
 
void CMyServiceAdvertiser::StartAdvertiserL(TInt aChannel)
 
void CMyServiceAdvertiser::StartAdvertiserL(TInt aChannel)
Line 132: Line 122:
 
</code>
 
</code>
  
Set availability of our advertised service. Service record on the
+
Set the availability of the advertised service. The service record on the
 
service discovery database will be updated accordingly.
 
service discovery database will be updated accordingly.
 
<code cpp>
 
<code cpp>
Line 148: Line 138:
 
</code>
 
</code>
  
Stop advertising service
+
Stop advertising the service
 
<code cpp>
 
<code cpp>
 
void CMyServiceAdvertiser::StopAdvertiserL()
 
void CMyServiceAdvertiser::StopAdvertiserL()
Line 154: Line 144:
 
     if ( iRecord!=NULL )
 
     if ( iRecord!=NULL )
 
         {
 
         {
         // Delete out record from service discovery database
+
         // Delete record from service discovery database
 
         iSdpDB.DeleteRecordL(iRecord);
 
         iSdpDB.DeleteRecordL(iRecord);
 
          
 
          
Line 167: Line 157:
  
 
==Postconditions==  
 
==Postconditions==  
Own bluetooth service is advertised and can be discovered according to
+
The Bluetooth service is advertised and can be discovered as explained in
[[CSdpAgent]].
+
[[CS000937 - Discovering Bluetooth services]].
  
 
==See also==
 
==See also==
[[RHostResolver]] Bluetooth device discovering <br>
+
[[CS000936 - Discovering Bluetooth devices]]
[[Bluetooth_connection]] Bluetooth connection listening <br>
+
 
[[CSdpAgent]] Bluetooth service discovery <br>
+
[[CS000937 - Discovering Bluetooth services]]
[[RSdp]] Bluetooth service advertising <br>
+
 
 +
[[CS000939 - Establishing a Bluetooth connection]]
 +
 
 +
[[KIS000330 - RHostResolver and redundant display of access point selection dialog]]
 +
 
 +
==Example application==
 +
This code snippet has been used in the [http://www.forum.nokia.com/info/sw.nokia.com/id/e56fccb6-2d70-4a02-9008-7b3e97927057/S60_Platform_Bluetooth_Point_to_Multipoint_Example.html S60 Platform: Bluetooth Point-to-multipoint Example application].
  
 
[[Category:Symbian C++]][[Category:Code Examples]][[Category:Bluetooth]]
 
[[Category:Symbian C++]][[Category:Code Examples]][[Category:Bluetooth]]

Revision as of 12:32, 2 May 2008


Template:KBCS

ID CS000938 Creation date May 2, 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. It is used to create subsessions to database functionality. A client must create and connect a session before using a RSdpDatabase subsession to access the database.

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

The following example shows how to advertise a certain (KBT_serviceID 0x10ff) Bluetooth service. Discovering this particular service is explained in CS9000937 - Discovering Bluetooth services.

Listening to the Bluetooth connection is explained in CS000939 - Establishing a Bluetooth connection. The 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 the 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 the service advertiser on a given channel. Entry to service discovery database will be entered describing the 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 the availability of the advertised service. The 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 the service

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


Postconditions

The Bluetooth service is advertised and can be discovered as explained in CS000937 - Discovering Bluetooth services.

See also

CS000936 - Discovering Bluetooth devices

CS000937 - Discovering Bluetooth services

CS000939 - Establishing a Bluetooth connection

KIS000330 - RHostResolver and redundant display of access point selection dialog

Example application

This code snippet has been used in the S60 Platform: Bluetooth Point-to-multipoint Example application.

137 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.

 

Thank you!

We appreciate your feedback.

×