📄 bluetoothpmpexampleengine.cpp
字号:
/*
* ============================================================================
* Name : CBluetoothPMPExampleEngine from BluetoothPMPExampleEngine.h
* Part of : BluetoothPMPExample
* Created : 06.06.2006 by Forum Nokia
* Implementation notes:
* Initial content was generated by Series 60 AppWizard.
* Version :
* Copyright: Nokia Corporation
* ============================================================================
*/
// INCLUDE FILES
#include <aknquerydialog.h> // for input dialogs
#include <utf.h>
#include "Common.h"
#include "BluetoothPMPExampleEngine.h"
#include "DeviceDiscoverer.h"
#include "ServiceAdvertiser.h"
#include "ServiceDiscoverer.h"
#include "Listener.h"
#include "Connector.h"
#include <BtPmpEx.rsg>
CBluetoothPMPExampleEngine* CBluetoothPMPExampleEngine::NewL(
CBluetoothPMPExampleAppUi& aAppUi)
{
CBluetoothPMPExampleEngine* self =
CBluetoothPMPExampleEngine::NewLC(aAppUi);
CleanupStack::Pop(self);
return self;
}
CBluetoothPMPExampleEngine* CBluetoothPMPExampleEngine::NewLC(
CBluetoothPMPExampleAppUi& aAppUi)
{
CBluetoothPMPExampleEngine* self =
new (ELeave) CBluetoothPMPExampleEngine(aAppUi);
CleanupStack::PushL(self);
self->ConstructL();
return self;
}
// Standard EPOC 2nd phase constructor
void CBluetoothPMPExampleEngine::ConstructL()
{
// get socket server session
User::LeaveIfError(iSocketServ.Connect());
// init listener
iListener = CListener::NewL(*this, iSocketServ);
// init device discoverer
iDeviceDiscoverer = CDeviceDiscoverer::NewL(iSocketServ, *this);
// init service advertiser
iServiceAdvertiser = CServiceAdvertiser::NewL();
// init service discoverer
iServiceDiscoverer = CServiceDiscoverer::NewL(*this);
// clean connections table to begin with
for ( TInt idx=0; idx<KMaxConnectedDevices; idx++ )
{
iConnectedDevices[idx] = NULL;
}
}
// ----------------------------------------------------------------------------
// CBluetoothPMPExampleEngine::CBluetoothPMPExampleEngine(
// CBluetoothPMPExampleAppUi* aAppUi)
//
// constructor
// ----------------------------------------------------------------------------
CBluetoothPMPExampleEngine::CBluetoothPMPExampleEngine(
CBluetoothPMPExampleAppUi& aAppUi):
iIsSlave(EFalse),
iAppUi(aAppUi)
{
//Nothing to do
}
// ----------------------------------------------------------------------------
// CBluetoothPMPExampleEngine::~CBluetoothPMPExampleEngine()
//
// destructor
// ----------------------------------------------------------------------------
CBluetoothPMPExampleEngine::~CBluetoothPMPExampleEngine()
{
// disconnect all devices and clean up connections table
DisconnectDevices();
// stop and kill helpers
delete iServiceAdvertiser;
iServiceAdvertiser=NULL;
delete iListener;
iListener=NULL;
delete iDeviceDiscoverer;
iDeviceDiscoverer = NULL;
delete iServiceDiscoverer;
iServiceDiscoverer=NULL;
iSocketServ.Close();
// wipe device data list
iDevDataList.ResetAndDestroy();
}
// ----------------------------------------------------------------------------
// ShowMessageL(
// const TDesC& aMsg, TBool aDrawLine=EFalse)
//
// displays text referenced by aMsg in the label, will append the aMsg in the
// existing text
// ----------------------------------------------------------------------------
void CBluetoothPMPExampleEngine::ShowMessageL(const TDesC& aMsg,
TBool aDrawLine=EFalse)
{
if( aDrawLine )
iAppUi.Container().DrawLineL();
iAppUi.Container().ShowMessageL(aMsg);
}
// ----------------------------------------------------------------------------
// CBluetoothPMPExampleEngine::DiscoverDevicesL()
//
// discover bluetooth devices within range.
// ----------------------------------------------------------------------------
void CBluetoothPMPExampleEngine::DiscoverDevicesL()
{
//timing:
iStartTime.HomeTime();
ShowMessageL(KDiscDevicesTxt, ETrue);
iDeviceDiscoverer->DiscoverDevicesL(&iDevDataList);
}
// ----------------------------------------------------------------------------
// CBluetoothPMPExampleEngine::DiscoverServicesL()
//
// discover services provided by the discovered devices.
// ----------------------------------------------------------------------------
void CBluetoothPMPExampleEngine::DiscoverServicesL()
{
iStartTime.HomeTime();
ShowMessageL(KDiscServicesTxt, ETrue);
iServiceDiscoverer->DiscoverServicesL(&iDevDataList);
}
// ----------------------------------------------------------------------------
// CBluetoothPMPExampleEngine::StartSlaveL()
//
// set application in slave mode. the device will be set to listen to
// a bluetooth channel, and advertise its service on the channel.
// ----------------------------------------------------------------------------
void CBluetoothPMPExampleEngine::StartSlaveL()
{
if ( iIsSlave )
return;
ShowMessageL(KSlaveInitTxt, ETrue);
TInt channel;
iListener->StartListenerL(channel);
TBuf<KThirty> msg;
msg.AppendFormat(KListeningTxt, channel);
ShowMessageL(msg, EFalse);
iServiceAdvertiser->StartAdvertiserL(channel);
ShowMessageL(KSlaveInitCompTxt, EFalse);
iIsSlave=ETrue;
}
// ----------------------------------------------------------------------------
// CBluetoothPMPExampleEngine::DisconnectDevices()
//
// disconnect connected devices and clean up connections table
// ----------------------------------------------------------------------------
void CBluetoothPMPExampleEngine::DisconnectDevices()
{
for ( TInt idx=0; idx<KMaxConnectedDevices; idx++ )
{
if ( iConnectedDevices[idx] )
{
delete iConnectedDevices[idx];
iConnectedDevices[idx]=NULL;
}
}
}
// ----------------------------------------------------------------------------
// CBluetoothPMPExampleEngine::ConnectDevicesL()
//
// attempt to connect on all discovered devices (up to 7)
// ----------------------------------------------------------------------------
void CBluetoothPMPExampleEngine::ConnectDevicesL()
{
// close and delete all existing connections
DisconnectDevices();
ShowMessageL(KConnectingTxt, ETrue);
// now attempt to connect
CConnector *connector = NULL;
for ( TInt idx=0; idx<(iDevDataList.Count()); idx++ )
{
if ( iConnectedDeviceCount>=KMaxConnectedDevices )
return;
TDeviceData *dev = iDevDataList[idx];
connector = CConnector::NewL(*this, iSocketServ);
CleanupStack::PushL(connector);
// if matching service on remote device was found, the service port
// is set and will be > 0. if so, we can attempt to connect.
if ( dev->iDeviceServicePort>0 )
{
if ( (connector->ConnectL(dev->iDeviceName,
dev->iDeviceAddr,
dev->iDeviceServicePort))==KErrNone )
{
iConnectedDevices[iConnectedDeviceCount] = connector;
iConnectedDeviceCount++;
}
else
{
// connection to device failed!!
delete connector;
}
}
CleanupStack::Pop(connector);
}
ShowConnectedDevicesL();
}
// ----------------------------------------------------------------------------
// CBluetoothPMPExampleEngine::ShowConnectedDevicesL()
//
// display the devices we are connected to
// ----------------------------------------------------------------------------
void CBluetoothPMPExampleEngine::ShowConnectedDevicesL()
{
TInt count=0;
ShowMessageL(KConnDevicesTxt, ETrue);
for (TInt idx=0; idx<KMaxConnectedDevices; idx++)
{
if ( iConnectedDevices[idx] )
{
THostName name;
name = iConnectedDevices[idx]->iName;
name.Append(KNewLine);
ShowMessageL(name, EFalse);
count++;
}
}
if ( count==0 )
{
// no connections!
// this may be because of no devices has been discovered,
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -