📄 bluetoothpmpexampleengine.cpp
字号:
/*
* ============================================================================
* Name : CBluetoothPMPExampleEngine from BluetoothPMPExampleEngine.h
* Part of : BluetoothPMPExample
* Created : 14.01.2004 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 <BluetoothPMPExample.rsg>
#include "Common.h"
#include "BluetoothPMPExampleEngine.h"
#include "DeviceDiscoverer.h"
#include "ServiceAdvertiser.h"
#include "ServiceDiscoverer.h"
#include "Listener.h"
#include "Connector.h"
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):
iListener(NULL),
iDeviceDiscoverer(NULL),
iServiceAdvertiser(NULL),
iServiceDiscoverer(NULL),
iIsSlave(EFalse),
iMsgLines(0),
iConnectedDeviceCount(0)
{
iAppUi=aAppUi;
}
// ----------------------------------------------------------------------------
// CBluetoothPMPExampleEngine::~CBluetoothPMPExampleEngine()
//
// destructor
// ----------------------------------------------------------------------------
CBluetoothPMPExampleEngine::~CBluetoothPMPExampleEngine()
{
// disconnect all devices and clean up connections table
DisconnectDevicesL();
// stop and kill helpers
delete iServiceAdvertiser;
iServiceAdvertiser=NULL;
delete iListener;
iListener=NULL;
delete iServiceDiscoverer;
iServiceDiscoverer=NULL;
iSocketServ.Close();
// wipe device data list
iDevDataList.ResetAndDestroy();
}
// ----------------------------------------------------------------------------
// ShowMessageL(
// const TDesC& aMsg, TBool aReset=false)
//
// displays text referenced by aMsg in the label, will append the aMsg in the
// existing text in label if aReset is false, otherwise will reset the label
// text.
// ----------------------------------------------------------------------------
void CBluetoothPMPExampleEngine::ShowMessageL(const TDesC& aMsg,
TBool aReset=false)
{
// if true, clear the message on the label prior to output
// max 8 lines displayed
if (aReset || iMsgLines>8)
{
iMsg.Zero();
iMsgLines=0;
}
iMsg.Append(aMsg);
iAppUi->iAppContainer->ShowMessageL(iMsg);
iMsgLines++;
}
// ----------------------------------------------------------------------------
// CBluetoothPMPExampleEngine::DiscoverDevicesL()
//
// discover bluetooth devices within range.
// ----------------------------------------------------------------------------
void CBluetoothPMPExampleEngine::DiscoverDevicesL()
{
ShowMessageL(_L("Discovering devices,\nplease wait...\n"), true);
iDeviceDiscoverer->DiscoverDevicesL(&iDevDataList);
}
// ----------------------------------------------------------------------------
// CBluetoothPMPExampleEngine::DiscoverServicesL()
//
// discover services provided by the discovered devices.
// ----------------------------------------------------------------------------
void CBluetoothPMPExampleEngine::DiscoverServicesL()
{
ShowMessageL(_L("Discovering services,\nplease wait...\n"), true);
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(_L("Slave init.."), true);
TInt channel;
channel = iListener->StartListenerL();
TBuf<30> msg;
msg.AppendFormat(_L("\nListening channel %d"), channel);
ShowMessageL(msg, false);
iServiceAdvertiser->StartAdvertiserL(channel);
ShowMessageL(_L("\nSlave init complete!\nWaiting for connection."), false);
iIsSlave=ETrue;
}
// ----------------------------------------------------------------------------
// CBluetoothPMPExampleEngine::DisconnectDevicesL()
//
// disconnect connected devices and clean up connections table
// ----------------------------------------------------------------------------
void CBluetoothPMPExampleEngine::DisconnectDevicesL()
{
for ( TInt idx=0; idx<KMaxConnectedDevices; idx++ )
{
if ( iConnectedDevices[idx] != NULL)
{
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
DisconnectDevicesL();
ShowMessageL(_L("Connecting...\n"), true);
// 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);
// 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;
}
}
}
ShowConnectedDevicesL();
}
// ----------------------------------------------------------------------------
// CBluetoothPMPExampleEngine::ShowConnectedDevicesL()
//
// display the devices we are connected to
// ----------------------------------------------------------------------------
void CBluetoothPMPExampleEngine::ShowConnectedDevicesL()
{
TInt count=0;
ShowMessageL(_L("Connected devices:\n"), true);
for (TInt idx=0; idx<KMaxConnectedDevices; idx++)
{
if ( iConnectedDevices[idx]!=NULL )
{
THostName name;
name = iConnectedDevices[idx]->iName;
name.Append(_L("\n"));
ShowMessageL(name, false);
count++;
}
}
if ( count==0 )
{
// no connections!
// this may be because of no devices has been discovered,
// or no devices are offering the requested service.
ShowMessageL(_L("No connections!"), false);
}
}
// ----------------------------------------------------------------------------
// CBluetoothPMPExampleEngine::SendMessage()
//
// send a message to all connected devices. user will be prompted to enter
// the message text.
// ----------------------------------------------------------------------------
void CBluetoothPMPExampleEngine::SendMessage()
{
// prompt for text
TBuf<20> msgtext;
CAknTextQueryDialog * dlg = CAknTextQueryDialog::NewL(msgtext);
dlg->SetMaxLength(20);
dlg->SetPromptL(_L("message"));
dlg->ExecuteLD(R_BLUETOOTHEXAMPLE_MESSAGEINPUT);
// explicitly convert from 16bit to 8bit character set
TBuf8<20> buf;
TPtr8 msgtext8((TUint8*)buf.Ptr(), msgtext.Size());
CnvUtfConverter::ConvertFromUnicodeToUtf8(msgtext8, msgtext);
TBuf<80> msg;
if ( iIsSlave )
{
// slave sending data
iListener->SendDataL(msgtext8);
msg.Format(_L("> %S\n"), &msgtext);
ShowMessageL(msg, false);
}
else
{
// master sending data
for (TInt idx=0; idx<KMaxConnectedDevices; idx++)
{
if ( iConnectedDevices[idx]!=NULL )
{
iConnectedDevices[idx]->SendDataL(msgtext8);
THostName name;
name=iConnectedDevices[idx]->iName;
msg.Format(_L("> %S: %S\n"), &name, &msgtext);
ShowMessageL(msg, false);
}
}
}
}
// ----------------------------------------------------------------------------
// CBluetoothPMPExampleEngine::HandleListenerDataReceivedL(TDesC& aData)
//
// display data the slave listener receives from the master. this is a
// callback that CListener class will invoke when it receives new data.
// ----------------------------------------------------------------------------
void CBluetoothPMPExampleEngine::HandleListenerDataReceivedL(TDesC& aData)
{
// display received message
TBuf<80> msg;
msg.Format(_L("< %S\n"), &aData);
ShowMessageL(msg, false);
}
// ----------------------------------------------------------------------------
// CBluetoothPMPExampleEngine::HandleListenerConnectedL()
//
// a callback received from CListener to indicate that it has been connected to
// ----------------------------------------------------------------------------
void CBluetoothPMPExampleEngine::HandleListenerConnectedL()
{
ShowMessageL(_L("Connected!\n"), true);
}
// ----------------------------------------------------------------------------
// CBluetoothPMPExampleEngine::HandleListenerDisconnectedL()
//
// a callback received from CListener to indicate that it has been disconnected
// ----------------------------------------------------------------------------
void CBluetoothPMPExampleEngine::HandleListenerDisconnectedL()
{
if ( !iIsSlave )
return;
// listener has already been stopped, request advertiser to stop as well
iServiceAdvertiser->StopAdvertiserL();
ShowMessageL(_L("Disconnected!\nSlave stopped.\n"), true);
}
// ----------------------------------------------------------------------------
// CBluetoothPMPExampleEngine::HandleConnectorDataReceivedL(THostName aName,
// TDesC& aData)
//
// display data the master receives from a connected slave. this is a
// callback that CConnector class will invoke when it receives data from slave.
// ----------------------------------------------------------------------------
void CBluetoothPMPExampleEngine::HandleConnectorDataReceivedL(THostName aName,
TDesC& aData)
{
// display received message
TBuf<80> msg;
msg.Format(_L("< %S: %S\n"), &aName, &aData);
ShowMessageL(msg, false);
}
// ----------------------------------------------------------------------------
// CBluetoothPMPExampleEngine::HandleDeviceDiscoveryCompleteL()
//
// a callback received from device discoverer to indicate that the device
// discovery has completed.
// ----------------------------------------------------------------------------
void CBluetoothPMPExampleEngine::HandleDeviceDiscoveryCompleteL()
{
// iterate and display found devices, if any
if ( iDevDataList.Count()> 0 )
{
// found devices
ShowMessageL(_L("Found devices:\n"), true);
for (TInt idx=0; idx<(iDevDataList.Count()); idx++)
{
TDeviceData *dev = iDevDataList[idx];
TBuf<40> name;
name = dev->iDeviceName;
name.Append(_L("\n"));
ShowMessageL(name, false);
}
}
else
{
// no devices found
ShowMessageL(_L("\nNo devices found!"), false);
}
}
// ----------------------------------------------------------------------------
// CBluetoothPMPExampleEngine::HandleServiceDiscoveryCompleteL()
//
// a callback received from service discoverer to indicate that the service
// discovery has completed.
// ----------------------------------------------------------------------------
void CBluetoothPMPExampleEngine::HandleServiceDiscoveryCompleteL()
{
TInt count=0;
ShowMessageL(_L("Found service on:\n"), true);
// display devices with service we can use
for ( TInt idx=0; idx<(iDevDataList.Count()); idx++ )
{
TDeviceData *dev = iDevDataList[idx];
if ( dev->iDeviceServicePort>0 )
{
THostName name = dev->iDeviceName;
name.Append(_L("\n"));
ShowMessageL(name, false);
count++;
}
}
if ( count==0 )
{
ShowMessageL(_L("No services found!\n"), false);
}
}
// ----------------------------------------------------------------------------
// CBluetoothPMPExampleEngine::HasConnections()
//
// returns true if master has any connections to slave(s)
// ----------------------------------------------------------------------------
TBool CBluetoothPMPExampleEngine::HasConnections()
{
return ( iConnectedDeviceCount>0 );
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -