📄 bluetoothengine.cpp
字号:
// INCLUDE FILES
#include <aknquerydialog.h> // for input dialogs
#include <utf.h>
#include <GouYu.rsg>
#include "Common.h"
#include "BluetoothEngine.h"
#include "DeviceDiscoverer.h"
#include "ServiceAdvertiser.h"
#include "ServiceDiscoverer.h"
#include "Listener.h"
#include "Connector.h"
#define HeaderLen 4
CBluetoothEngine* CBluetoothEngine::NewL(
CGouYuAppUi* aAppUi)
{
CBluetoothEngine* self =
CBluetoothEngine::NewLC(aAppUi);
CleanupStack::Pop(self);
return self;
}
CBluetoothEngine* CBluetoothEngine::NewLC(
CGouYuAppUi* aAppUi)
{
CBluetoothEngine* self =
new (ELeave) CBluetoothEngine(aAppUi);
CleanupStack::PushL(self);
self->ConstructL();
return self;
}
// Standard EPOC 2nd phase constructor
void CBluetoothEngine::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;
}
num_temp = 0;
// if(!iIsSlave)
// iAppUi->iAppView->iAppEngine->iPlayerNum = 0;
// else
// iAppUi->iAppView->iAppEngine->iPlayerNum = 1;
}
// ----------------------------------------------------------------------------
// CBluetoothEngine::CBluetoothEngine(
// CFightLandLordAppUi* aAppUi)
//
// constructor
// ----------------------------------------------------------------------------
CBluetoothEngine::CBluetoothEngine(
CGouYuAppUi* aAppUi):
iListener(NULL),
iDeviceDiscoverer(NULL),
iServiceAdvertiser(NULL),
iServiceDiscoverer(NULL),
iIsSlave(EFalse),
iMsgLines(0),
iConnectedDeviceCount(0)
{
iAppUi=aAppUi;
}
// ----------------------------------------------------------------------------
// CBluetoothEngine::~CBluetoothEngine()
//
// destructor
// ----------------------------------------------------------------------------
CBluetoothEngine::~CBluetoothEngine()
{
// 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();
num_temp = 0;
}
// ----------------------------------------------------------------------------
// 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 CBluetoothEngine::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->iAppView->ShowMessageL(iMsg);
iMsgLines++;
}
// ----------------------------------------------------------------------------
// CBluetoothEngine::DiscoverDevicesL()
//
// discover bluetooth devices within range.
// ----------------------------------------------------------------------------
void CBluetoothEngine::DiscoverDevicesL()
{
ShowMessageL(_L("Discovering devices,\nplease wait...\n"), true);
iDeviceDiscoverer->DiscoverDevicesL(&iDevDataList);
}
// ----------------------------------------------------------------------------
// CBluetoothEngine::DiscoverServicesL()
//
// discover services provided by the discovered devices.
// ----------------------------------------------------------------------------
void CBluetoothEngine::DiscoverServicesL()
{
ShowMessageL(_L("Discovering services,\nplease wait...\n"), true);
iServiceDiscoverer->DiscoverServicesL(&iDevDataList);
}
// ----------------------------------------------------------------------------
// CBluetoothEngine::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 CBluetoothEngine::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;
}
// ----------------------------------------------------------------------------
// CBluetoothEngine::DisconnectDevicesL()
//
// disconnect connected devices and clean up connections table
// ----------------------------------------------------------------------------
void CBluetoothEngine::DisconnectDevicesL()
{
for ( TInt idx=0; idx<KMaxConnectedDevices; idx++ )
{
if ( iConnectedDevices[idx] != NULL)
{
delete iConnectedDevices[idx];
iConnectedDevices[idx]=NULL;
}
}
}
// ----------------------------------------------------------------------------
// CBluetoothEngine::ConnectDevicesL()
//
// attempt to connect on all discovered devices (up to 7)
// ----------------------------------------------------------------------------
void CBluetoothEngine::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();
//add by LJC
iAppUi->iAppView->iAppEngine->SetPlayerNum(0);
MasterSendMessage(0);
}
// ----------------------------------------------------------------------------
// CBluetoothEngine::ShowConnectedDevicesL()
//
// display the devices we are connected to
// ----------------------------------------------------------------------------
void CBluetoothEngine::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);
}
}
// ----------------------------------------------------------------------------
// CBluetoothEngine::SendMessage()
//
// send a message to all connected devices. user will be prompted to enter
// the message text.
// ----------------------------------------------------------------------------
void CBluetoothEngine::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);
}
}
}
}
void CBluetoothEngine::MasterSendMessage(TInt8 aMsgType)
{
TInt8 aPayloadLen = 0;
if( aMsgType == 0)
{
TUint16 iTemp[5];
iTemp[0] = 0;
iTemp[1] = 0;
iTemp[2] = 3;
iTemp[3] = 1;
aPayloadLen = 1;
for (TInt idx=0; idx<KMaxConnectedDevices; idx++)
{
if ( iConnectedDevices[idx]!=NULL )
{
iTemp[4] = idx;
TBuf<20> formattedMessage ;
formattedMessage.Append( iTemp, HeaderLen+aPayloadLen );
TBuf8<20> buf;
TPtr8 msgtext8((TUint8*)buf.Ptr(), formattedMessage.Size());
CnvUtfConverter::ConvertFromUnicodeToUtf8(msgtext8, formattedMessage);
iConnectedDevices[idx]->SendDataL(msgtext8);
}
}
}
else if( aMsgType == 1 )
{
aPayloadLen = 18 ;
TUint16 *iTemp = new (ELeave)TUint16[aPayloadLen+HeaderLen+1];
iTemp[0] = 1;iTemp[1]=num_temp;iTemp[2]=2;iTemp[3]=aPayloadLen+1;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -