📄 bluetoothclient.cpp
字号:
/*
============================================================================================
Name : BluetoothClient.cpp
Author : BluetoothAPI is a initiative of Embedded LAB - http://www.embedded.ufcg.edu.br/
OpenC/SymbianC++ - http://efforts.embedded.ufcg.edu.br/symbiancpp
Version :
Copyright : This file is part of BluetoothAPI.
BluetoothAPI is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
BluetoothAPI is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with BluetoothAPI. If not, see <http://www.gnu.org/licenses/>.
Description : BluetoothClient.cpp implementation
===========================================================================================
*/
// INCLUDE FILES
#include "BluetoothClient.h"
_LIT(KBTRFCOMM, "RFCOMM");
/*
============================================================================
CBluetoothSockConnector's two stage constructor
============================================================================
*/
CBluetoothClient* CBluetoothClient::NewL(RSocket& aBtSocket, MBTClientObserver* aSockConnObs)
{
CBluetoothClient* self = new (ELeave) CBluetoothClient(aBtSocket, aSockConnObs);
CleanupStack::PushL(self);
self->ConstructL();
CleanupStack::Pop();
return self;
}
/*
============================================================================
CBluetoothSockConnector's costructor
============================================================================
*/
CBluetoothClient::CBluetoothClient(RSocket& aBtSocket, MBTClientObserver* aSockConnObs)
:CActive(CActive::EPriorityStandard), iClientSocket(aBtSocket), iBTClientObserver(aSockConnObs)
{
CActiveScheduler::Add(this);
}
void CBluetoothClient::ConnectL(RSocketServ& aSocketServ, TBTDevAddr aBTDevAddr, TInt aPort)
{
TBTSockAddr sockAddr;
sockAddr.SetBTAddr(aBTDevAddr);
sockAddr.SetPort(aPort);
User::LeaveIfError(iClientSocket.Open(aSocketServ, KBTRFCOMM));
iBTClientState = EBTClientConnecting;
iClientSocket.Connect(sockAddr, iStatus);
SetActive();
}
void CBluetoothClient::Disconnect()
{
iBTClientState = EBTClientDisconnected;
Cancel();
iClientSocket.CancelAll();
iClientSocket.Shutdown(RSocket::ENormal, iStatus);
SetActive();
}
/*
============================================================================
CBluetoothSockConnector's second phase constructor
============================================================================
*/
void CBluetoothClient::ConstructL()
{
}
/*
============================================================================
CBluetoothSockConnector's destructor
============================================================================
*/
CBluetoothClient::~CBluetoothClient()
{
Cancel();
}
/*
============================================================================
DoCancel is called as part of the active object's Cancel().
Outstanding operations for a BT socket include: read, write, Ioctl, connect,
accept, shutdown and the Baseband event notifier. All of these operations will
be completed by this call
============================================================================
*/
void CBluetoothClient::DoCancel()
{
iClientSocket.CancelAll();
}
/*
============================================================================
Handles the active object's socket connection completion event
============================================================================
*/
void CBluetoothClient::RunL()
{
if (iStatus==KErrNone)
{
switch (iBTClientState)
{
case EBTClientDisconnected:
break;
case EBTClientConnecting:
iBTClientState = EBTClientConnected;
iBTClientObserver->HandleBTClientConnected();
break;
case EBTClientConnected:
break;
case EBTClientSending:
iBTClientObserver->HandleBTMessageSent();
iBTClientState = EBTClientWaiting;
iClientSocket.RecvOneOrMore(iBuffer, 0, iStatus, iLen);
SetActive();
break;
case EBTClientWaiting:
iBTClientObserver->HandleBTClientMessageRcvd();
iBTClientState = EBTClientConnected;
break;
}
}
else if (iStatus==KErrDisconnected)
{
Disconnect();
iBTClientObserver->HandleBTClientDisconnected();
}
else
{
iBTClientObserver->HandleBTError(iStatus.Int());
}
}
void CBluetoothClient::SendL(const TDesC8& aMessage)
{
if (IsActive())
Cancel();
iClientSocket.CancelAll();
HBufC8* message = aMessage.AllocL();
iClientSocket.Write(*message, iStatus);
delete message;
iBTClientState = EBTClientSending;
SetActive();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -