- 開發夥伴平臺:
S60 3rd Edition, MR
- 詳細描述
RSdp提供一個到Service Discovery Database的會話session。它可以用來生成到數據庫功能的子會話。客戶端必須在使
用RSdpDatabase子會話訪問數據庫時生成並連接到一個會話。
RSdpDatabase是一個SDP的子會話,通過它服務可以記錄,它們的屬性可以被添加,刪除或更新。
MMP文件
需要下列能力和鏈接庫
CAPABILITY LocalServices LIBRARY sdpagent.lib LIBRARY sdpdatabase.lib
頭文件
#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;
源文件
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); }
設置廣播服務的可用性,service discovery database上的服務記錄將依此更新:
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); }
停止廣播服務
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; } }