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

📄 objectexchangeclient.cpp

📁 This C++ code example provides a method for transferring objects or chunks of data from one device
💻 CPP
字号:
/* Copyright (c) 2004, Nokia. All rights reserved */


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

#include "ObjectExchangeClient.h"
#include "ObjectExchangeServiceSearcher.h"
#include "BTObjectExchange.pan"
#include "Log.h"

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

// ----------------------------------------------------------------------------
// CObjectExchangeClient::NewL()
// Symbian two-phased constructor.
// ----------------------------------------------------------------------------
//
CObjectExchangeClient* CObjectExchangeClient::NewL( MLog& aLog )
    {
    CObjectExchangeClient* self = NewLC( aLog );
    CleanupStack::Pop( self );
    return self;
    }

// ----------------------------------------------------------------------------
// CObjectExchangeClient::NewLC()
// Symbian two-phased constructor.
// ----------------------------------------------------------------------------
//
CObjectExchangeClient* CObjectExchangeClient::NewLC( MLog& aLog )
    {
    CObjectExchangeClient* self = new ( ELeave ) CObjectExchangeClient( aLog );
    CleanupStack::PushL( self );
    self->ConstructL();
    return self;
    }

// ----------------------------------------------------------------------------
// CObjectExchangeClient::CObjectExchangeClient()
// Constructor.
// ----------------------------------------------------------------------------
//
CObjectExchangeClient::CObjectExchangeClient( MLog& aLog )
: CActive( CActive::EPriorityStandard ),
  iState( EWaitingToGetDevice ),
  iLog( aLog )
    {
    CActiveScheduler::Add( this );
    }

// ----------------------------------------------------------------------------
// CObjectExchangeClient::~CObjectExchangeClient()
// Destructor.
// ----------------------------------------------------------------------------
//
CObjectExchangeClient::~CObjectExchangeClient()
    {

    if ( iState != EWaitingToGetDevice && iClient )
    {
        iClient->Abort();
        iStatus = KErrNone;
    }

    Cancel();
    
    delete iCurrObject;
    iCurrObject = NULL;

    delete iServiceSearcher;
    iServiceSearcher = NULL;

    delete iClient;
    iClient = NULL;
    }

// ----------------------------------------------------------------------------
// CObjectExchangeClient::ConstructL()
// Perform second phase construction of this object.
// ----------------------------------------------------------------------------
//
void CObjectExchangeClient::ConstructL()
    {
    iServiceSearcher = CObjectExchangeServiceSearcher::NewL( iLog );
    }

// ----------------------------------------------------------------------------
// CObjectExchangeClient::DoCancel()
// Cancel any outstanding requests.
// ----------------------------------------------------------------------------
//
void CObjectExchangeClient::DoCancel()
    {
    }

// ----------------------------------------------------------------------------
// CObjectExchangeClient::RunL()
// Respond to an event.
// ----------------------------------------------------------------------------
//
void CObjectExchangeClient::RunL()
    {
    HBufC* textResource = NULL;

    if ( iStatus != KErrNone )
        {
        switch ( iState )
            {
            case EGettingDevice:
                if ( iStatus == KErrCancel )
                    {
                    textResource = StringLoader::LoadLC ( R_BTOB_NO_DEVICE );
                    iLog.LogL( *textResource );
                    CleanupStack::PopAndDestroy ( textResource );
                    }
                iState = EWaitingToGetDevice;
                break;
            case EGettingService:
            case EGettingConnection:
            case EDisconnecting:
                textResource = StringLoader::LoadLC ( R_BTOB_CONNECTION_ERROR );
                iLog.LogL( *textResource, iStatus.Int() );
                CleanupStack::PopAndDestroy ( textResource );
                iState = EWaitingToGetDevice;
                break;
            case EWaitingToSend:
                textResource = StringLoader::LoadLC ( R_BTOB_SEND_ERROR );
                iLog.LogL( *textResource, iStatus.Int() );
                CleanupStack::PopAndDestroy ( textResource );
                iState = EWaitingToGetDevice;
                break;
            default:
                Panic( EBTObjectExchangeUnexpectedLogicState );
                break;
            }
        }
    else 
        {
        switch ( iState )
            {
            case EGettingDevice:
                // found a device now search for a suitable service
                iLog.LogL( iServiceSearcher->ResponseParams().DeviceName() );
                iState = EGettingService;
                iStatus = KRequestPending; 
                // this means that the RunL can not be called until
                // this program does something to iStatus
                iServiceSearcher->FindServiceL( iStatus );
                SetActive();
                break;

            case EGettingService:
                textResource = StringLoader::LoadLC ( R_BTOB_FOUND_SERVICE );
                iLog.LogL( *textResource );
                CleanupStack::PopAndDestroy ( textResource );
                iState = EGettingConnection;
                ConnectToServerL();
                break;

            case EGettingConnection:
                textResource = StringLoader::LoadLC ( R_BTOB_CONNECTED );
                iLog.LogL( *textResource );
                CleanupStack::PopAndDestroy ( textResource );
                iState = EWaitingToSend;
                break;

            case EWaitingToSend:
                textResource = StringLoader::LoadLC ( R_BTOB_SENT_OBJECT );
                iLog.LogL( *textResource );
                CleanupStack::PopAndDestroy ( textResource );
                break;

            case EDisconnecting:
                textResource = StringLoader::LoadLC ( R_BTOB_DISCONNECTED );
                iLog.LogL( *textResource);
                CleanupStack::PopAndDestroy ( textResource );
                iState = EWaitingToGetDevice;
                break;

            default:
                Panic( EBTObjectExchangeSdpRecordDelete );
                break;
            };
        }
    }

// ----------------------------------------------------------------------------
// CObjectExchangeClient::ConnectL()
// Connect to a service.
// ----------------------------------------------------------------------------
//
void CObjectExchangeClient::ConnectL()
    {
    if ( iState == EWaitingToGetDevice && !IsActive() )
        {
        iServiceSearcher->SelectDeviceByDiscoveryL( iStatus );
        iState = EGettingDevice;
        SetActive();
        }
    else
        {
        HBufC* strClientBusy = StringLoader::LoadLC( R_BTOB_CLIENT_BUSY );
        iLog.LogL( *strClientBusy );
        CleanupStack::PopAndDestroy ( strClientBusy );
        User::Leave( KErrInUse );
        }
    }

// ----------------------------------------------------------------------------
// CObjectExchangeClient::ConnectToServerL()
// Connect to the server.
// ----------------------------------------------------------------------------
//
void CObjectExchangeClient::ConnectToServerL()
    {
    TObexBluetoothProtocolInfo protocolInfo;

    protocolInfo.iTransport.Copy( KServerTransportName );
    protocolInfo.iAddr.SetBTAddr( iServiceSearcher->BTDevAddr() );
    protocolInfo.iAddr.SetPort( iServiceSearcher->Port() );

    if ( iClient )
        {
        delete iClient;
        iClient = NULL;
        }
    iClient = CObexClient::NewL( protocolInfo );
    
    iClient->Connect( iStatus );
    SetActive();
    }

// ----------------------------------------------------------------------------
// CObjectExchangeClient::SendObjectL()
// Send a message to a service on a remote machine.
// ----------------------------------------------------------------------------
//
void CObjectExchangeClient::SendObjectL(TFileName& aName)
    {
    if ( iState != EWaitingToSend )
        {
        User::Leave( KErrDisconnected );
        }
    else if ( IsActive() ) 
        {
        User::Leave( KErrInUse );
        }
    
    delete iCurrObject;
    iCurrObject = NULL;
    iCurrObject = CObexFileObject::NewL(aName);
    
    TParsePtr parsePtr (aName);        
    TPtrC ptr = parsePtr.NameAndExt();
    
    iCurrObject->SetNameL( ptr );
        
    iClient->Put( *iCurrObject, iStatus );
    SetActive();
    }

// ----------------------------------------------------------------------------
// CObjectExchangeClient::StopL()
// Aborts command.
// ----------------------------------------------------------------------------
//
void CObjectExchangeClient::StopL()
    {
    if ( iClient && iClient->IsConnected() )
        {
        iClient->Abort();
        iState = EWaitingToGetDevice;
        }
    }

// ----------------------------------------------------------------------------
// CObjectExchangeClient::DisconnectL()
// Disconnects from the remote machine.
// ----------------------------------------------------------------------------
//
void CObjectExchangeClient::DisconnectL()
    {
    if ( iState == EWaitingToGetDevice )
        {
        return;
        }
    if ( iState == EWaitingToSend )
        {
        HBufC* strDisconnecting = StringLoader::LoadLC( R_BTOB_DISCONNECTING );
        iLog.LogL( *strDisconnecting );
        CleanupStack::PopAndDestroy ( strDisconnecting );
        iState = EDisconnecting;
        iClient->Disconnect( iStatus );
        SetActive();
        }
    else 
        {
        User::Leave( KErrInUse );
        }
    }

// ----------------------------------------------------------------------------
// CObjectExchangeClient::IsBusy()
// True, if the client is performing some operation..
// ----------------------------------------------------------------------------
//
TBool CObjectExchangeClient::IsBusy()
    {
    return IsActive();
    }

// ----------------------------------------------------------------------------
// CObjectExchangeClient::IsConnected()
// True, if the client is performing some operation..
// ----------------------------------------------------------------------------
//
TBool CObjectExchangeClient::IsConnected()
    {
    return iState == EWaitingToSend;
    }

// End of File

⌨️ 快捷键说明

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