⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 btserviceadvertiser.cpp

📁 S60培训教材代码(连载)
💻 CPP
字号:
#include <bt_sock.h>
#include "BTServiceAdvertiser.h"
#include "MessageProtocolConstants.h"

CBTServiceAdvertiser::CBTServiceAdvertiser(TUint aAppUID)
:   iAppUID(aAppUID),
    iRecord(0), 
    iIsConnected(EFalse)
    {
    // no implementation required
    }

CBTServiceAdvertiser::~CBTServiceAdvertiser()
    {
    if (IsAdvertising()) 
        {
        TRAPD(err,StopAdvertisingL());
        if (err != KErrNone)
            {
            User::Panic(_L("BTServiceAdvertiser"), err);
            }
        }

    iSdpDatabase.Close();
    iSdpSession.Close();
    }

void CBTServiceAdvertiser::ConnectL()
    {
    if (!iIsConnected)
        {
        User::LeaveIfError(iSdpSession.Connect());
        User::LeaveIfError(iSdpDatabase.Open(iSdpSession));
        iIsConnected = ETrue;
        }
    }

void CBTServiceAdvertiser::StartAdvertisingL(TInt aPort)
    {
    if (IsAdvertising())
        {
        // could be advertising on a different port
        StopAdvertisingL(); 
        }
    if (! iIsConnected)
        {
        ConnectL();
        }
    iSdpDatabase.CreateServiceRecordL(KServiceClass, iRecord);

    // add a Protocol to the record
    CSdpAttrValueDES* vProtocolDescriptor = CSdpAttrValueDES::NewDESL(NULL);
    CleanupStack::PushL(vProtocolDescriptor);
 
    TBuf8<1> channel;
    channel.Append((TChar)aPort);

    vProtocolDescriptor
        ->StartListL()
            ->BuildDESL()
            ->StartListL()   //  Details of lowest level protocol
                ->BuildUUIDL(KL2CAP)
            ->EndListL()

            ->BuildDESL()
            ->StartListL()
                ->BuildUUIDL(KRFCOMM)
                ->BuildUintL(channel)
            ->EndListL()
        ->EndListL();

    iSdpDatabase.UpdateAttributeL(iRecord, KSdpAttrIdProtocolDescriptorList, *vProtocolDescriptor);

    CleanupStack::PopAndDestroy(vProtocolDescriptor);

    // Add a name to the record
    iSdpDatabase.UpdateAttributeL(iRecord, 
                                  KSdpAttrIdBasePrimaryLanguage+KSdpAttrIdOffsetServiceName, 
                                  KServiceName);

    // Add a description to the record
    iSdpDatabase.UpdateAttributeL(iRecord, 
                                  KSdpAttrIdBasePrimaryLanguage+KSdpAttrIdOffsetServiceDescription, 
                                  KServiceDescription);

    // Add the app uid

    iSdpDatabase.UpdateAttributeL(iRecord, 
                                  KAppUIDAttr, 
                                  iAppUID);
    }


void CBTServiceAdvertiser::StopAdvertisingL()
    {
    if (IsAdvertising())
        {
        iSdpDatabase.DeleteRecordL(iRecord);
        iRecord = 0;
        }
    }

TBool CBTServiceAdvertiser::IsAdvertising()
    {
    return iRecord != 0;
    }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -