📄 btconnectionclient.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"));
ConnectToServerL();
break;
case EConnecting:
iLog.LogL(_L8("Connected"));
iState = EConnected;
iNotify.HostConnected(KErrNone);
break;
case EConnected:
iLog.LogL(_L8("Received message"));
iNotify.HostDataRead(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"));
User::LeaveIfError(iSocketServer.Connect());
User::LeaveIfError(iDataSocket.Open(iSocketServer, KBTAddrFamily, KSockStream, 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());
// get the device address from the device discovery process
address.SetBTAddr(iServiceSearcher->BTDevAddr());
// get the channel number from the remote SDP query
address.SetPort(iServiceSearcher->Port());
iDataSocket.Connect(address, iStatus);
iState = EConnecting;
SetActive();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -