examples/ForumNokia/BluetoothPMPExample/src/ServiceAdvertiser.cpp

00001 /*
00002  * Copyright © 2009 Nokia Corporation.
00003  */
00004 
00005 // INCLUDE FILES
00006 #include <btmanclient.h>
00007 #include "ServiceAdvertiser.h"
00008 
00009 // see https://www.bluetooth.org/foundry/assignnumb/document/service_discovery
00010 
00011 // service name and description for our service
00012 _LIT(KBTServiceName, "BTpmp");
00013 _LIT(KBTServiceDesc, "BTpmp");
00014 
00015 CServiceAdvertiser* CServiceAdvertiser::NewL()
00016     {
00017     CServiceAdvertiser* self = CServiceAdvertiser::NewLC();
00018     CleanupStack::Pop(self);
00019     return self;
00020     }
00021 
00022 
00023 CServiceAdvertiser* CServiceAdvertiser::NewLC()
00024     {
00025     CServiceAdvertiser* self = new (ELeave) CServiceAdvertiser();
00026     CleanupStack::PushL(self);
00027     self->ConstructL();
00028     return self;
00029     }
00030 
00031 
00032 void CServiceAdvertiser::ConstructL()
00033     {
00034     }
00035 
00036 
00037 // ----------------------------------------------------------------------------
00038 // CServiceAdvertiser::CServiceAdvertiser()
00039 //
00040 // constructor
00041 // ----------------------------------------------------------------------------
00042 CServiceAdvertiser::CServiceAdvertiser()
00043     {
00044     }
00045 
00046 
00047 // ----------------------------------------------------------------------------
00048 // CServiceAdvertiser::~CServiceAdvertiser()
00049 //
00050 // destructor
00051 // ----------------------------------------------------------------------------
00052 CServiceAdvertiser::~CServiceAdvertiser()
00053     {
00054     TRAPD(err, StopAdvertiserL());
00055     }
00056 
00057 
00058 
00059 // ----------------------------------------------------------------------------
00060 // CServiceAdvertiser::StartAdvertiserL(TInt aChannel)
00061 //
00062 // start service advertiser on given channel.  an entry to service discovery
00063 // database will be entered describing our advertised service.
00064 // ----------------------------------------------------------------------------
00065 void CServiceAdvertiser::StartAdvertiserL(TInt aChannel)
00066     {
00067     // open sdp session
00068     User::LeaveIfError(iSdp.Connect());
00069     // open sdp database session
00070     User::LeaveIfError(iSdpDB.Open(iSdp));
00071     // create a record of the correct service class
00072     TUUID serviceUUID(KBT_serviceID);
00073     iSdpDB.CreateServiceRecordL(serviceUUID, iRecord);
00074 
00075     // add a protocol to the record
00076     CSdpAttrValueDES* protocolDescriptorList = CSdpAttrValueDES::NewDESL(NULL);
00077     CleanupStack::PushL(protocolDescriptorList);
00078 
00079     TBuf8<1> channel;
00080     channel.Append((TChar)aChannel);
00081     // create protocol list for our service
00082     protocolDescriptorList
00083     ->StartListL()   //  list of protocols required for this method
00084         ->BuildDESL()
00085         ->StartListL()
00086             ->BuildUUIDL(KL2CAP)
00087         ->EndListL()
00088 
00089         ->BuildDESL()
00090         ->StartListL()
00091             ->BuildUUIDL(KRFCOMM)
00092             ->BuildUintL(channel)
00093         ->EndListL()
00094     ->EndListL();
00095 
00096     // set protocol list to the record
00097     iSdpDB.UpdateAttributeL(iRecord, KSdpAttrIdProtocolDescriptorList,
00098         *protocolDescriptorList);
00099     CleanupStack::PopAndDestroy(protocolDescriptorList);
00100 
00101     // add a name to the record
00102     iSdpDB.UpdateAttributeL(iRecord,
00103                                 KSdpAttrIdBasePrimaryLanguage +
00104                                     KSdpAttrIdOffsetServiceName,
00105                                 KBTServiceName);
00106 
00107     // add a description to the record
00108     iSdpDB.UpdateAttributeL(iRecord,
00109                                 KSdpAttrIdBasePrimaryLanguage +
00110                                     KSdpAttrIdOffsetServiceDescription,
00111                                 KBTServiceDesc);
00112 
00113     // set service available
00114     UpdateAvailabilityL(ETrue);
00115     }
00116 
00117 
00118 // ----------------------------------------------------------------------------
00119 // CServiceAdvertiser::StopAdvertiserL()
00120 //
00121 // stop service advertiser.  delete service record from service discovery
00122 // database to stop advertising.
00123 // ----------------------------------------------------------------------------
00124 void CServiceAdvertiser::StopAdvertiserL()
00125     {
00126     if ( iRecord!=0 )
00127         {
00128         // delete out record from service discovery database
00129         iSdpDB.DeleteRecordL(iRecord);
00130         // close sdp and sdp db sessions
00131         iSdpDB.Close();
00132         iSdp.Close();
00133         iRecord=0;
00134         }
00135     }
00136 
00137 
00138 // ----------------------------------------------------------------------------
00139 // CServiceAdvertiser::UpdateAvailability(TBool aAvailable)
00140 //
00141 // set availability of our advertised service.  the service record on the
00142 // service discovery database will be updated accordingly.
00143 // ----------------------------------------------------------------------------
00144 void CServiceAdvertiser::UpdateAvailabilityL(TBool aAvailable)
00145     {
00146     TInt state = aAvailable ? 0xFF : 0x00;
00147     // set availability
00148     iSdpDB.UpdateAttributeL(iRecord, KSdpAttrIdServiceAvailability, state);
00149     // mark record changed
00150     iSdpDB.UpdateAttributeL(iRecord, KSdpAttrIdServiceRecordState,
00151         ++iRecordState);
00152     }
00153 

Generated by  doxygen 1.6.2