📄 bluetoothserver.cpp.svn-base
字号:
/*
============================================================================================
Name : BluetoothServer.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 : BluetoothServer.cpp implementation
===========================================================================================
*/
#include "BluetoothServer.h"
CBluetoothServer::CBluetoothServer(MBTServerObserver* aObserver) :
CActive( EPriorityStandard ), // Standard priority
iBTServerState(EBTServerDisconnected),
iBTServerObserver(aObserver)
{
CActiveScheduler::Add(this);
}
CBluetoothServer* CBluetoothServer::NewL(MBTServerObserver* aObserver)
{
CBluetoothServer* self = new ( ELeave ) CBluetoothServer(aObserver);
CleanupStack::PushL( self );
self->ConstructL();
CleanupStack::Pop(); // self;
return self;
}
void CBluetoothServer::ConstructL()
{
}
CBluetoothServer::~CBluetoothServer()
{
Cancel(); // Cancel any request, if outstanding
// Delete instance variables if any
}
void CBluetoothServer::RunL()
{
if (iStatus==KErrNone)
{
switch(iBTServerState)
{
case EBTServerDisconnected:
break;
case EBTServerConnecting:
iBTServerObserver->HandleBTServerConnected();
iBTServerState = EBTServerWaiting;
iConnectedSocket.RecvOneOrMore(iBuffer, 0, iStatus, iLen);
SetActive();
break;
case EBTServerWaiting:
iBTServerObserver->HandleBTServerMessageRecvd();
iBTServerState = EBTServerConnected;
break;
case EBTServerSending:
iBTServerObserver->HandleBTMessageSent();
iBTServerState = EBTServerWaiting;
iConnectedSocket.RecvOneOrMore(iBuffer, 0, iStatus, iLen);
SetActive();
break;
case EBTServerConnected:
break;
}
}
else if (iStatus==KErrDisconnected)
{
Cancel();
Stop();
iBTServerObserver->HandleBTServerDisconnected();
}
else
{
iBTServerObserver->HandleBTError(iStatus.Int());
}
}
TInt CBluetoothServer::StartL(RSocketServ& aSocketServ)
{
TProtocolDesc pdesc;
User::LeaveIfError(aSocketServ.FindProtocol(_L("RFCOMM"), pdesc));
User::LeaveIfError(iListenSocket.Open(aSocketServ, pdesc.iAddrFamily,
pdesc.iSockType, KRFCOMM));
TInt port;
User::LeaveIfError(iListenSocket.GetOpt
(KRFCOMMGetAvailableServerChannel, KSolBtRFCOMM, port));
TBTSockAddr sockAddr;
sockAddr.SetPort(port);
User::LeaveIfError(iListenSocket.Bind(sockAddr));
User::LeaveIfError(iListenSocket.Listen(5));
SetChannelSecurityL(EFalse, EFalse, EFalse, port, sockAddr);
iConnectedSocket.Close();
User::LeaveIfError(iConnectedSocket.Open(aSocketServ));
iBTServerState = EBTServerConnecting;
iListenSocket.Accept(iConnectedSocket, iStatus);
SetActive();
return port;
}
void CBluetoothServer::Stop()
{
iBTServerState = EBTServerDisconnected;
Cancel();
iConnectedSocket.CancelAll();
iConnectedSocket.Close();
iListenSocket.Close();
}
void CBluetoothServer::SendL(const TDesC8& aMessage)
{
iConnectedSocket.CancelAll();
Cancel();
HBufC8* message = aMessage.AllocL();
iConnectedSocket.Write(*message, iStatus);
delete message;
iBTServerState = EBTServerSending;
SetActive();
}
void CBluetoothServer::DoCancel()
{
}
void CBluetoothServer::SetChannelSecurityL(TBool aAuthentication, TBool aEncryption,
TBool aAuthorisation, TInt aChannel, TBTSockAddr& aSockAddr)
{
RBTMan btManager;
User::LeaveIfError(btManager.Connect());
CleanupClosePushL(btManager);
TBTServiceSecurity serviceSecurity;
serviceSecurity.SetAuthentication(aAuthentication);
serviceSecurity.SetEncryption(aEncryption);
serviceSecurity.SetAuthorisation(aAuthorisation);
aSockAddr.SetSecurity(serviceSecurity);
CleanupStack::PopAndDestroy();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -