testeapiappview.cpp.svn-base

来自「qaSDQDaca<FCSASDAVCASC SDVFDSVDF」· SVN-BASE 代码 · 共 323 行

SVN-BASE
323
字号
/*
============================================================================
 Name		: TesteAPIAppView.cpp
 Author	  : Tiago Sousa
 Copyright   : 
 Description : Application view implementation
============================================================================
*/

// INCLUDE FILES
#include <coemain.h>
#include "TesteAPIAppView.h"
#include "StringUtils.h"
#include <aknnotewrappers.h>
#include <vector>
using namespace std;

const TUint32 KBTServiceUUID=0x20ff;
_LIT(KBTDormantMessage, "Dormant.");
_LIT(KBTDiscoveringDevicesMessage, "Discovering devices...");
_LIT(KBTDiscoveringServicesMessage, "Discovering services...");
_LIT(KBTNoDevicesFoundMessage, "No devices found.");
_LIT(KBTNoServicesFoundMessage, "No services found.");
_LIT(KBTConnectingMessage, "Connecting to server...");
_LIT(KBTServer, "Advertising.");
_LIT(KBTServerConnected, "Server Connected.");
_LIT(KBTServerDisconnected, "Server Disconnected.");
_LIT(KBTClientConnected, "Client Connected.");
_LIT(KBTClientDisconnected, "Client Disconnected.");

// ============================ MEMBER FUNCTIONS ===============================

// -----------------------------------------------------------------------------
// CTesteAPIAppView::NewL()
// Two-phased constructor.
// -----------------------------------------------------------------------------
//
CTesteAPIAppView* CTesteAPIAppView::NewL( const TRect& aRect )
	{
	CTesteAPIAppView* self = CTesteAPIAppView::NewLC( aRect );
	CleanupStack::Pop( self );
	return self;
	}

// -----------------------------------------------------------------------------
// CTesteAPIAppView::NewLC()
// Two-phased constructor.
// -----------------------------------------------------------------------------
//
CTesteAPIAppView* CTesteAPIAppView::NewLC( const TRect& aRect )
	{
	CTesteAPIAppView* self = new ( ELeave ) CTesteAPIAppView();
	CleanupStack::PushL( self );
	self->ConstructL( aRect );
	return self;
	}

// -----------------------------------------------------------------------------
// CTesteAPIAppView::ConstructL()
// Symbian 2nd phase constructor can leave.
// -----------------------------------------------------------------------------
//
void CTesteAPIAppView::ConstructL( const TRect& aRect )
	{
	// Create a window for this application view
	CreateWindowL();

	// Set the windows size
	SetRect( aRect );

	// Activate the window, which makes it ready to be drawn
	ActivateL();
	
	iMessage[0] = '\0';
	
	Bluetooth = BluetoothAPI::create(this);
	status = 0;
	}

// -----------------------------------------------------------------------------
// CTesteAPIAppView::CTesteAPIAppView()
// C++ default constructor can NOT contain any code, that might leave.
// -----------------------------------------------------------------------------
//
CTesteAPIAppView::CTesteAPIAppView()
	{
	// No implementation required
	}

// -----------------------------------------------------------------------------
// CTesteAPIAppView::~CTesteAPIAppView()
// Destructor.
// -----------------------------------------------------------------------------
//
CTesteAPIAppView::~CTesteAPIAppView()
	{
	if ( Bluetooth )
		{
		delete Bluetooth;
		Bluetooth = NULL;
		}
	}

// -----------------------------------------------------------------------------
// CTesteAPIAppView::Draw()
// Draws the display.
// -----------------------------------------------------------------------------
//
void CTesteAPIAppView::Draw( const TRect& /*aRect*/ ) const
	{
	// Get the standard graphics context
		CWindowGc& gc = SystemGc();

		// Gets the control's extent
		TRect drawRect( Rect());

		// Clears the screen
		gc.Clear( drawRect );
		
		gc.SetPenStyle(CGraphicsContext::ESolidPen);
		gc.SetPenColor(KRgbRed);
		gc.UseFont(iCoeEnv->NormalFont());
			switch( status ){
				case 0:
					gc.DrawText( KBTDormantMessage, TPoint(10,20));
					break;
				case 1:
					gc.DrawText( KBTDiscoveringDevicesMessage, TPoint(10,20));
					break;
				case 2:
					gc.DrawText( KBTDiscoveringServicesMessage, TPoint(10,20));
					break;
				case 3:
					gc.DrawText( KBTNoDevicesFoundMessage, TPoint(10,20));
					break;
				case 4:
					gc.DrawText( KBTNoServicesFoundMessage, TPoint(10,20));
					break;
				case 5:
					gc.DrawText( KBTConnectingMessage, TPoint(10,20));
					break;
				case 6:
					gc.DrawText( KBTServer, TPoint(10,20));
					break;
				case 7:
					gc.DrawText( KBTServerConnected, TPoint(10,20));
					break;
				case 8:
					gc.DrawText( KBTServerDisconnected, TPoint(10,20));
					break;
				case 9:
					gc.DrawText( KBTClientConnected, TPoint(10,20));
					break;
				case 10:
					gc.DrawText( KBTClientDisconnected, TPoint(10,20));
					break;
			}

			gc.SetPenColor(KRgbBlack);
			TBuf<100> C;
			_LIT(KTxt, "%d Devices Found");
			C.Format(KTxt, DeviceList.capacity() );
			gc.DrawText( C, TPoint(10,40));
			
			unsigned int i = 0;
			
			gc.SetPenColor(KRgbBlack);
			_LIT(KTxt2, "%d Services Found");
			C.Format(KTxt2, ServiceList.capacity());//ServiceList.capacity() );
			gc.DrawText( C, TPoint(10,60+20*i));

			gc.SetPenColor(KRgbBlue);
			gc.SetPenColor(KRgbBlue);
			for (unsigned j = 0; j < ServiceList.capacity(); j++ ) {
					chartotbuf16( ServiceList[j].devName, C);
					i++;
					gc.DrawText( C, TPoint(10,60+20*i));
			}
			chartotbuf16(iMessage, C);
			gc.DrawText( C, TPoint(10,80+20*i));
	}

// -----------------------------------------------------------------------------
// CTesteAPIAppView::SizeChanged()
// Called by framework when the view size is changed.
// -----------------------------------------------------------------------------
//
void CTesteAPIAppView::SizeChanged()
	{
	DrawNow();
	}

void CTesteAPIAppView::devicesDiscoverFinished( vector<BTDeviceData>& aDiscoveredDeviceList )
	{
	DeviceList = aDiscoveredDeviceList;
	status = 2;
	if (DeviceList.capacity() == 0)
		status = 3;
	DrawNow();
	}

void CTesteAPIAppView::servicesDiscoverFinished( vector<BTDeviceData>& aDiscoveredServiceList )
	{
	ServiceList = aDiscoveredServiceList;
	status = 5;
	if (ServiceList.capacity() == 0)
		status = 4;
	DrawNow();
	}

void CTesteAPIAppView::BTConnectServer()
	{
	status = 6;
	Bluetooth->startAdvertising(KBTServiceUUID, "testeBT" );
	DrawNow();
	}

void CTesteAPIAppView::BTConnectClient()
	{
	status = 1;
	Bluetooth->searchServices(KBTServiceUUID);
	DrawNow();
	}

void CTesteAPIAppView::BTSendMsg(char* aMessage)
	{
	Bluetooth->sendMessage(aMessage);
	}

void CTesteAPIAppView::serverConnected()
{
	CAknInformationNote* note = new (ELeave) CAknInformationNote;
	TRAPD(err, note->ExecuteLD(_L("Server Connected")));
	status = 7;
	DrawNow();
}

void CTesteAPIAppView::serverDisconnected()
{
	CAknInformationNote* note = new (ELeave) CAknInformationNote;
	TRAPD(err, note->ExecuteLD(_L("Server Disconnected")));
	status = 8;
	DrawNow();
}

void CTesteAPIAppView::clientConnected()
{
CAknInformationNote* note = new (ELeave) CAknInformationNote;
TRAPD(err, note->ExecuteLD(_L("Client Connected")));
	status = 9;
	DrawNow();
}

void CTesteAPIAppView::clientDisconnected()
{
CAknInformationNote* note = new (ELeave) CAknInformationNote;
TRAPD(err, note->ExecuteLD(_L("Client Disconnected")));
	status = 10;
	DrawNow();
}

void CTesteAPIAppView::messageReceived( char *aMessage)
{
CAknInformationNote* note = new (ELeave) CAknInformationNote;
TRAPD(err, note->ExecuteLD(_L("Message Received")));
TInt i = 0;
	for(; aMessage[i] != '\0'; i++)
		iMessage[i] = aMessage[i];
	iMessage[i] = '\0';

	DrawNow();
}

void CTesteAPIAppView::messageSent()
{
CAknInformationNote* note = new (ELeave) CAknInformationNote;
TRAPD(err, note->ExecuteLD(_L("Message Sent")));
	char aux[] = "Message Sent";
TInt i = 0;
	for(; aux[i]; i++)
		iMessage[i] = aux[i];
	iMessage[i] = '\0';

	DrawNow();
}

void CTesteAPIAppView::errorOcurred(int aError)
{
CAknInformationNote* note = new (ELeave) CAknInformationNote;
TBuf<32> msg;
msg.Format(_L("Error: %d"), aError);
TRAPD(err, note->ExecuteLD(msg));

	char aux[22] = "Error ocurred -      ";
	aError = -aError;
	aux[20] = aError%10+48;
	if (!(aError/10%10))
		aux[19] = aError/10%10+48;
	if (!(aError/100%10))
		aux[18] = aError/100%10+48;
	if (!(aError/1000%10))
		aux[17] = aError/1000%10+48;
	if (!(aError/10000%10))
		aux[16] = aError/10000%10+48;	
TInt i = 0;
	for(; i < 21; i++)
		iMessage[i] = aux[i];
	iMessage[i] = '\0';
	DrawNow();
}

void CTesteAPIAppView::DisconnectAsServer()
{
	Bluetooth->stopServerAndAdvertising();	
}

void CTesteAPIAppView::DisconnectAsClient()
{
	Bluetooth->disconnectAsClient();
}

// End of File

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?