www.pudn.com > Bluetooth_PMP_Example_v1_0.zip > ServiceAdvertiser.cpp
/* * ============================================================================ * Name : CServiceAdvertiser from ServiceAdvertiser.h * Part of : BluetoothPMPExample * Created : 14.01.2004 by Forum Nokia * Implementation notes: * Initial content was generated by Series 60 AppWizard. * Version : * Copyright: Nokia Corporation * ============================================================================ */ // INCLUDE FILES #include#include "ServiceAdvertiser.h" // see https://www.bluetooth.org/foundry/assignnumb/document/service_discovery static const TInt KBTSerialClassID = 0x1101; // service name and description for our service _LIT(KBTServiceName, "BTpmp"); _LIT(KBTServiceDesc, "BTpmp"); CServiceAdvertiser* CServiceAdvertiser::NewL() { CServiceAdvertiser* self = CServiceAdvertiser::NewLC(); CleanupStack::Pop(self); return self; } CServiceAdvertiser* CServiceAdvertiser::NewLC() { CServiceAdvertiser* self = new (ELeave) CServiceAdvertiser(); CleanupStack::PushL(self); self->ConstructL(); return self; } void CServiceAdvertiser::ConstructL() { } // ---------------------------------------------------------------------------- // CServiceAdvertiser::CServiceAdvertiser() // // constructor // ---------------------------------------------------------------------------- CServiceAdvertiser::CServiceAdvertiser(): iRecord(0), iRecordState(0) { } // ---------------------------------------------------------------------------- // CServiceAdvertiser::~CServiceAdvertiser() // // destructor // ---------------------------------------------------------------------------- CServiceAdvertiser::~CServiceAdvertiser() { StopAdvertiserL(); } // ---------------------------------------------------------------------------- // CServiceAdvertiser::StartAdvertiserL(TInt aChannel) // // start service advertiser on given channel. an entry to service discovery // database will be entered describing our advertised service. // ---------------------------------------------------------------------------- void CServiceAdvertiser::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); } // ---------------------------------------------------------------------------- // CServiceAdvertiser::StopAdvertiserL() // // stop service advertiser. delete service record from service discovery // database to stop advertising. // ---------------------------------------------------------------------------- void CServiceAdvertiser::StopAdvertiserL() { if ( iRecord!=0 ) { // delete out record from service discovery database iSdpDB.DeleteRecordL(iRecord); // close sdp and sdp db sessions iSdpDB.Close(); iSdp.Close(); iRecord=0; } } // ---------------------------------------------------------------------------- // CServiceAdvertiser::UpdateAvailability(TBool aAvailable) // // set availability of our advertised service. the service record on the // service discovery database will be updated accordingly. // ---------------------------------------------------------------------------- void CServiceAdvertiser::UpdateAvailabilityL(TBool aAvailable) { TInt state = aAvailable ? 0xFF : 0x00; // set availability iSdpDB.UpdateAttributeL(iRecord, KSdpAttrIdServiceAvailability, state); // mark record changed iSdpDB.UpdateAttributeL(iRecord, KSdpAttrIdServiceRecordState, ++iRecordState); }