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

📄 btconnectionclient.cpp

📁 S60培训教材代码(连载)
💻 CPP
字号:
/* Copyright (c) 2002, Nokia Mobile Phones. All rights reserved */

#include "BTConnectionClient.h"
#include "BTServiceSearcher.h"
#include "S60BTLab.pan"
#include "Log.h"
#include "BTClientNotify.h"
#include "MessageProtocolConstants.h"
#include "S60btlabapp.h"

CBTConnectionClient* CBTConnectionClient::NewL(MBTClientNotify& aNotify, MLog& aLog)
    {
    CBTConnectionClient* self = new (ELeave) CBTConnectionClient(aNotify, aLog);
    CleanupStack::PushL(self);
    self->ConstructL();
    CleanupStack::Pop(self);
    return self;
    }

CBTConnectionClient::CBTConnectionClient(MBTClientNotify& aNotify, MLog& aLog) :
    CBTConnectionBase(aLog),
    iNotify(aNotify),
    iState(EWaitingToGetDevice)
    {
    }

CBTConnectionClient::~CBTConnectionClient()
    {
    Cancel();

    delete iServiceSearcher;
    iServiceSearcher = NULL;

    iDataSocket.Close();
    iSocketServer.Close();
    }

void CBTConnectionClient::ConstructL()
    {
    iServiceSearcher = new (ELeave) CBTServiceSearcher(KServiceClass, KUidS60BTLab.iUid, iLog);
    }

void CBTConnectionClient::DoCancel()
    {
    if (EConnected == iState && IsActive()) 
        {
        iDataSocket.CancelRecv();
        }
    }

void CBTConnectionClient::RunL()
    {
    if (iStatus != KErrNone)
        {
        switch (iState)
            {
            case EConnecting:
            case EGettingService:
                iLog.LogL(_L8("Connection error "), iStatus.Int());
                iState = EWaitingToGetDevice;
                iNotify.HostConnected(iStatus.Int());
                break;
            case EConnected:
                iLog.LogL(_L8("Read error "), iStatus.Int());
                iNotify.HostDisconnected(iStatus.Int());
                break;
            default:
                Panic(ES60BTLabInvalidClientState);
                break;
            }
        }
    else 
        {
        switch (iState)
            {
            case EGettingService:
                iLog.LogL(_L8("Found Service"));
                // TODO: now connect to the server using ConnectToServerL()
                break;
            case EConnecting:
                iLog.LogL(_L8("Connected"));
                iState = EConnected;
                // TODO: notify the calling code (CS60BTLabAppView in this case)
                // that we are now connected to the host
                // Hint: use MBTClientNotify::HostConnected()
                // (this class has a MBTClientNotify& member variable)
                break;
            case EConnected:
                iLog.LogL(_L8("Received message"));
                // TODO: notify the calling code (CS60BTLabAppView in this case)
                // that data has been read from the host
                // Hint: use MBTClientNotify::HostDataRead()
                // The data is contained in iBuffer
                break;
            default:
                Panic(ES60BTLabInvalidClientState);
                break;
            };
        }
    }

void CBTConnectionClient::ConnectL()
    {
    if (iState == EWaitingToGetDevice && !IsActive())
        {
        iState = EGettingService;
        iStatus = KRequestPending; // this means that the RunL can not be called until
                                   // this program does something to iStatus
        iServiceSearcher->FindServiceL(iStatus);
        SetActive();
        }
    else
        {
        iLog.LogL(_L8("Client busy"));
        User::Leave(KErrInUse);
        }
    }

void CBTConnectionClient::ConnectToServerL()
    {
    iLog.LogL(_L8("Connecting to Service"));

    // TODO: If any of the actions that are implemented in the following 2 TODO sections
    // return an error, the function should leave.
    // Hint: use User::LeaveIfError()

    // TODO: Connect to the socket server here using RSocketServ::Connect() with iSocketServer

    // TODO: Open a socket to connect to the host here using iDataSocket (defined in base class)
    // and RSocket::Open()
    // Use: addrFamily  - KBTAddrFamily,
    //      sockType    - KSockStream
    //      protocol    - KRFCOMM


    TBTSockAddr address;
    TBuf<100> unicodeDevAddr;

    iServiceSearcher->BTDevAddr().GetReadable(unicodeDevAddr);

    TBuf8<100> devAddr;
    devAddr.Copy(unicodeDevAddr);
    iLog.LogL(_L8("Device Addr = "), devAddr);

    iLog.LogL(_L8("Port No = "), iServiceSearcher->Port());

    // TODO: Set the remote device bluetooth address in the "address" local variable
    // on the 
    // Hint: use CBTServiceSearcher::BTDevAddr() as a parameter in TBTSockAddr::SetBTAddr()
    // (this class contains a member varible of type CBTServiceSearcher*)

    // TODO: Set the remote device port no in the "address" local variable
    // Hint: use CBTServiceSearcher::Port() as a parameter in TBTSockAddr::SetPort()
    // (this class contains a member varible of type CBTServiceSearcher*)

    // TODO: Connect to the remote device
    // Hint: Use RSocket::Connect() on the socket that opened above
    // The address is contained within the "address" local variable and use
    // the iStatus member variable of this class (declared in CActive)

    iState = EConnecting;
    SetActive();
    }

⌨️ 快捷键说明

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