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

📄 btservicesearcher.cpp

📁 Symbian下的p2p工程
💻 CPP
字号:
/* Copyright (c) 2004, Nokia. All rights reserved */


// INCLUDE FILES
#include <StringLoader.h>
#include <BTPointToPoint.rsg>

#include "BTServiceSearcher.h"
#include "BTServiceSearcher.pan"

// ============================ MEMBER FUNCTIONS ==============================

// ----------------------------------------------------------------------------
// CBTServiceSearcher::CBTServiceSearcher()
// Constructor.
// ----------------------------------------------------------------------------
//
CBTServiceSearcher::CBTServiceSearcher( MLog& aLog )
:  iIsDeviceSelectorConnected( EFalse ),
   iLog( aLog )
    {
    }

// ----------------------------------------------------------------------------
// CBTServiceSearcher::~CBTServiceSearcher()
// Destructor.
// ----------------------------------------------------------------------------
//
CBTServiceSearcher::~CBTServiceSearcher()
    {
    if ( iIsDeviceSelectorConnected )
        {
        iDeviceSelector.CancelNotifier( KDeviceSelectionNotifierUid );
        iDeviceSelector.Close();
        }
    delete iSdpSearchPattern;
    iSdpSearchPattern = NULL;

    delete iAgent;
    iAgent = NULL;
    }

// ----------------------------------------------------------------------------
// CBTServiceSearcher::SelectDeviceByDiscoveryL()
// Select a device.
// ----------------------------------------------------------------------------
//
void CBTServiceSearcher
::SelectDeviceByDiscoveryL( TRequestStatus& aObserverRequestStatus )
    {
    if ( ! iIsDeviceSelectorConnected )
        {
        User::LeaveIfError( iDeviceSelector.Connect() );
        iIsDeviceSelectorConnected = ETrue;
        }


    iSelectionFilter().SetUUID( ServiceClass() );

    iDeviceSelector.StartNotifierAndGetResponse( 
        aObserverRequestStatus, 
        KDeviceSelectionNotifierUid, 
        iSelectionFilter, 
        iResponse );
    }

// ----------------------------------------------------------------------------
// CBTServiceSearcher::FindServiceL()
// Find a service on the specified device.
// ----------------------------------------------------------------------------
//
void CBTServiceSearcher::FindServiceL( TRequestStatus& aObserverRequestStatus )
    {
    if ( !iResponse().IsValidBDAddr() )
        {
        User::Leave( KErrNotFound );
        }
    iHasFoundService = EFalse;

    // delete any existing agent and search pattern
    delete iSdpSearchPattern;
    iSdpSearchPattern = NULL;

    delete iAgent;
    iAgent = NULL;

    iAgent = CSdpAgent::NewL( *this, BTDevAddr() );

    iSdpSearchPattern = CSdpSearchPattern::NewL();
   
    iSdpSearchPattern->AddL( ServiceClass() );
    // return code is the position in the list that the UUID is inserted at 
    // and is intentionally ignored
  
    iAgent->SetRecordFilterL( *iSdpSearchPattern );

    iStatusObserver = &aObserverRequestStatus;

    iAgent->NextRecordRequestL();
    }

// ----------------------------------------------------------------------------
// CBTServiceSearcher::NextRecordRequestComplete()
// Process the result of the next record request.
// ----------------------------------------------------------------------------
//
void CBTServiceSearcher::NextRecordRequestComplete( 
    TInt aError, 
    TSdpServRecordHandle aHandle, 
    TInt aTotalRecordsCount )
    {
    TRAPD( error,
           NextRecordRequestCompleteL( aError, aHandle, aTotalRecordsCount );
    );

    if ( error != KErrNone ) 
        {
        Panic( EBTServiceSearcherNextRecordRequestComplete );
        }
    }

// ----------------------------------------------------------------------------
// CBTServiceSearcher::NextRecordRequestCompleteL()
// Process the result of the next record request.
// ----------------------------------------------------------------------------
//
void CBTServiceSearcher::NextRecordRequestCompleteL( 
    TInt aError, 
    TSdpServRecordHandle aHandle, 
    TInt aTotalRecordsCount )
    {
    if ( aError == KErrEof )
        {
        Finished();
        return;
        }

    if ( aError != KErrNone )
        {
        iLog.LogL( KErrNRRCErr, aError );
        Finished( aError );
        return;
        }

    if ( aTotalRecordsCount == 0 )
        {
        HBufC* errNRRCNoRecords = StringLoader
            ::LoadLC ( R_BTPO_ERR_NRRC_NO_RECORDS );
        iLog.LogL( *errNRRCNoRecords );
        CleanupStack::PopAndDestroy ( errNRRCNoRecords );
        Finished( KErrNotFound );
        return;
        }

    //  Request its attributes
    iAgent->AttributeRequestL( aHandle, KSdpAttrIdProtocolDescriptorList );
    }

// ----------------------------------------------------------------------------
// CBTServiceSearcher::AttributeRequestResult()
// Process the next attribute requested.
// ----------------------------------------------------------------------------
//
void CBTServiceSearcher::AttributeRequestResult( 
    TSdpServRecordHandle aHandle, 
    TSdpAttributeID aAttrID, 
    CSdpAttrValue* aAttrValue )
    {
    TRAPD( error,
         AttributeRequestResultL( aHandle, aAttrID, aAttrValue );
         );
    if ( error != KErrNone )
        {
        Panic( EBTServiceSearcherAttributeRequestResult );
        }

    // Delete obsolete local atribute pointer. 
    delete aAttrValue;
    }

// ----------------------------------------------------------------------------
// CBTServiceSearcher::AttributeRequestResultL()
// Process the next attribute requested.
// ----------------------------------------------------------------------------
//
void CBTServiceSearcher::AttributeRequestResultL( 
    TSdpServRecordHandle /*aHandle*/, 
    TSdpAttributeID aAttrID, 
    CSdpAttrValue* aAttrValue )
    {
    __ASSERT_ALWAYS( aAttrID == KSdpAttrIdProtocolDescriptorList, 
                     User::Leave( KErrNotFound ) );

    TSdpAttributeParser parser( ProtocolList(), *this );

    // Validate the attribute value, and extract the RFCOMM channel
    aAttrValue->AcceptVisitorL( parser );

    if ( parser.HasFinished() )
        {
        // Found a suitable record so change state
        iHasFoundService = ETrue;
        }
    }

// -----------------------------------------------------------------------------
// CBTServiceSearcher::AttributeRequestComplete()
// Process the attribute request completion.
// -----------------------------------------------------------------------------
//
void CBTServiceSearcher::AttributeRequestComplete( TSdpServRecordHandle aHandle,
                                                   TInt aError )
    {
    TRAPD( error,
        AttributeRequestCompleteL( aHandle, aError );
    );
    if ( error != KErrNone )
        {
        Panic( EBTServiceSearcherAttributeRequestComplete );
        }
    }

// ----------------------------------------------------------------------------
// CBTServiceSearcher::AttributeRequestCompleteL()
// Process the attribute request completion.
// ----------------------------------------------------------------------------
//
void CBTServiceSearcher::AttributeRequestCompleteL( TSdpServRecordHandle 
                                                    /*aHandle*/, 
                                                    TInt aError )
    {
    if ( aError != KErrNone )
        {
        HBufC* errCantGetAttribute = StringLoader
            ::LoadLC ( R_BTPO_ERR_CANT_GET_ATTRIBUTE );
        iLog.LogL( *errCantGetAttribute, aError );
        CleanupStack::PopAndDestroy ( errCantGetAttribute );
        }
    else if ( !HasFinishedSearching() )
        {
        // have not found a suitable record so request another
        iAgent->NextRecordRequestL();
        }
    else
        {
        HBufC* errAttrReqCom = StringLoader
            ::LoadLC ( R_BTPO_ERR_ATTR_REQ_COM );
        iLog.LogL( *errAttrReqCom );
        CleanupStack::PopAndDestroy ( errAttrReqCom );
        Finished();
        }
    }

// ----------------------------------------------------------------------------
// CBTServiceSearcher::Finished()
// The search has finished and notify the observer 
// that the process is complete.
// ----------------------------------------------------------------------------
//
void CBTServiceSearcher::Finished( TInt aError /* default = KErrNone */ )
    {
    if ( aError == KErrNone && !HasFoundService() )
        {
        aError = KErrNotFound;
        }
    User::RequestComplete( iStatusObserver, aError );
    }

// ----------------------------------------------------------------------------
// CBTServiceSearcher::HasFinishedSearching()
// Is the instance still wanting to search.
// ----------------------------------------------------------------------------
//
TBool CBTServiceSearcher::HasFinishedSearching() const
    {
    return EFalse;
    }

// ----------------------------------------------------------------------------
// CBTServiceSearcher::BTDevAddr()
// Returns the bluetooth device address.
// ----------------------------------------------------------------------------
//
const TBTDevAddr& CBTServiceSearcher::BTDevAddr()
    {
    return iResponse().BDAddr();
    }

// ----------------------------------------------------------------------------
// CBTServiceSearcher::ResponseParams()
// Returns information about the device selected by the user.
// ----------------------------------------------------------------------------
//
const TBTDeviceResponseParams& CBTServiceSearcher::ResponseParams()
    {
    return iResponse();
    }

// ----------------------------------------------------------------------------
// CBTServiceSearcher::HasFoundService()
// True if a service has been found.
// ----------------------------------------------------------------------------
//
TBool CBTServiceSearcher::HasFoundService() const
    {
    return iHasFoundService;
    }

// End of File

⌨️ 快捷键说明

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