xinshebtview.cpp

来自「打开蓝牙」· C++ 代码 · 共 367 行

CPP
367
字号
/*
* ============================================================================
*  Name     : CXinSheBTView from CAknView
*  Part of  : XinSheBT
*  Copyright (c) 2009 Xinshe. All rights reserved.
* ============================================================================
*/

// INCLUDE FILES
#include <aknviewappui.h>
#include <avkon.hrh>

#include <XinSheBT.rsg>
#include "XinSheBT.hrh"
#include "XinSheBTview.h"
#include "XinSheBTContainer.h" 


// System includes
#include <AknGlobalNote.h>
#include <AknNoteWrappers.h>
#include <AknQueryDialog.h>
#include <AknWaitDialog.h>
#include <XinSheBT.rsg>		// R_XINSHEBTVIEW_DIALOG
#include <eikmenup.h>
#include <stringloader.h>		// StringLoader

// User includes
#include "XinSheBTDialog.h"	// CXinSheBTDialog
#include "XinSheBT.hrh"		// commands
#include "BluetoothDeviceSearcher.h"	// CBluetoothDeviceSearcher
#include "BluetoothServiceSearcher.h"	// CBluetoothServiceSearcher
#include "BluetoothClient.h"		// CBluetoothClient



// Constants
const TInt KFormattedMessagePrefixLength = 10;
_LIT(KFormattedMessagePrefix, "Message:\n");
const TInt KServerCleanupDelayMicroSeconds = 4000000;


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

// C++ default constructor can NOT contain any code, that
// might leave.
//
CXinSheBTView* CXinSheBTView::NewL()
{
    CXinSheBTView* self = NewLC();
    CleanupStack::Pop(self);
    return self;
}

CXinSheBTView* CXinSheBTView::NewLC()
{
    CXinSheBTView* self = new (ELeave) CXinSheBTView;
    CleanupStack::PushL(self);
    self->ConstructL();
    return self;
}

CXinSheBTView::CXinSheBTView()
    {
    }

// EPOC default constructor can leave.
void CXinSheBTView::ConstructL()
    {
    BaseConstructL(R_XINSHEBTVIEW);
	
	iAppDialog = new (ELeave) CXinSheBTDialog;
	iAppDialog->SetMopParent(this);
	iAppDialog->ExecuteLD(R_XINSHEBTVIEW_DIALOG);
	AppUi()->AddToStackL(iAppDialog);
	
	iClient = CBluetoothClient::NewL(*this);
    }

// Destructor
CXinSheBTView::~CXinSheBTView()
    {
    }

// ---------------------------------------------------------
// TUid CXinSheBTView::Id()
// Returns Id of the view.
// ---------------------------------------------------------
TUid CXinSheBTView::Id() const
    {
    return KViewId1;
    }



	/**
	* Dynamic Menu Construction
	*
	* Restricts options available to the User via the Menu
	* Performs checks on the state of the Client and Server to see what should be offered
	*
	* @param aResourceId
	* @param @aMenuPane
	* @return none
*/
// void CXinSheBTView::DynInitMenuPaneL(TInt aResourceId, CEikMenuPane* aMenuPane)
// {
// 	if (aResourceId == R_XINSHEBTVIEW_MENU_PANE)
// 	{
// 		if (iClient->IsConnected()/* || iServer->IsConnected()*/)
// 		{
// 			aMenuPane->SetItemDimmed(EXINSHEBTVIEWConnect, ETrue);
// 			aMenuPane->SetItemDimmed(EXINSHEBTVIEWReceive, ETrue);
// 			if (iClient->IsConnected())
// 			{
// 				aMenuPane->SetItemDimmed(EXINSHEBTVIEWStopReceive, ETrue);
// 			}
// 			if (!iClient->AvailableToSend()/* && !iServer->AvailableToSend()*/)
// 			{
// 				aMenuPane->SetItemDimmed(EXINSHEBTVIEWSend, ETrue);
// 			}
// 		}
// 	}
// }


// ---------------------------------------------------------
// CXinSheBTView::HandleCommandL(TInt aCommand)
// Handles commands
// ---------------------------------------------------------
/**
* From CEikAppUi, takes care of command handling.
*
* @param aCommand command to be handled
* @return none
*/
void CXinSheBTView::HandleCommandL(TInt aCommand)
{
	switch (aCommand)
	{
	case EXinSheBTViewConnect:
		{
			FindRemoteDeviceL();
			break;
		}

	case EXinSheBTWapBrowser:
		{
			AppUi()->HandleCommandL(aCommand);
			break;
		}

	case EEikCmdExit:
		{
			AppUi()->HandleCommandL(aCommand);
			break;
		}
		
	default:
		break;
	}
}

// ---------------------------------------------------------
// CXinSheBTView::HandleClientRectChange()
// Handles client rect change.
// ---------------------------------------------------------
void CXinSheBTView::HandleClientRectChange()
    {
    if (iContainer)
        {
        iContainer->SetRect(ClientRect());
        }
    }

// ---------------------------------------------------------
// CXinSheBTView::DoActivateL(...)
// Creates the Container class object.
// ---------------------------------------------------------
void CXinSheBTView::DoActivateL(const TVwsViewId& /*aPrevViewId*/,
                                    TUid /*aCustomMessageId*/,
                                    const TDesC8& /*aCustomMessage*/)
    {
    iContainer = new (ELeave) CXinSheBTContainer;
    iContainer->SetMopParent(this);
	TRect rc;
	rc.SetRect(ClientRect().iTl, TSize(176, 144));
    iContainer->ConstructL(rc);
    AppUi()->AddToStackL(*this, iContainer);
    }

// ---------------------------------------------------------
// CXinSheBTView::DoDeactivate()
// Deletes the Container class object.
// ---------------------------------------------------------
void CXinSheBTView::DoDeactivate()
    {
    if (iContainer)
        {
		AppUi()->RemoveFromStack(iContainer);
		delete iContainer;
		iContainer = NULL;
        }
    }

/**
* Begin Client side communication.
*
* Find a Device
* Find Service on device and obtain Port information
* Pass to the Client to form a connection
* @param none
* @return none
*/
void CXinSheBTView::FindRemoteDeviceL()
	{
	iDeviceSearcher = CBluetoothDeviceSearcher::NewL(*this);
	iDeviceSelectionResponse = new (ELeave) TBTDeviceResponseParamsPckg();
	iDeviceSearcher->SelectDeviceL(*iDeviceSelectionResponse);
	}

void CXinSheBTView::DeviceFoundL(TInt result)
	{
	delete iDeviceSearcher;	// delete and cleanup
	iDeviceSearcher = 0;

	if (result == KErrNone)
		{
		TInt port = 0;
		CBluetoothServiceSearcher* serviceSearcher = CBluetoothServiceSearcher::NewLC(port);
		TInt serviceFound = serviceSearcher->FindServiceL((*iDeviceSelectionResponse)().BDAddr());
		CleanupStack::PopAndDestroy( serviceSearcher );

		if (serviceFound == KErrNone)
			{
			iClient->ConnectToServerL((*iDeviceSelectionResponse)().BDAddr(), port);
			}
		else
			{
			CAknGlobalNote* globalNote = CAknGlobalNote::NewLC();
			HBufC* warning = StringLoader::LoadLC(R_SERVICE_NOT_FOUND);
			globalNote->ShowNoteL(EAknGlobalWarningNote, *warning);
			CleanupStack::PopAndDestroy(globalNote);
			CleanupStack::PopAndDestroy(warning);

			// Permit clean up of Bluetooth server connection
			User::After(TTimeIntervalMicroSeconds32(KServerCleanupDelayMicroSeconds));
			}
		}

	delete iDeviceSelectionResponse;
	iDeviceSelectionResponse = 0;
	}


/**
* Connection Made.
*
* Server - Remove the Cancel dialog
* Client - Provide feedback that a connection has been made
* @param none
* @return none
*/
void CXinSheBTView::ConnectedL()
	{
	if (iWaitDialog)
		{
		iWaitDialog->ProcessFinishedL();
		iWaitDialog = NULL;
		}

	if (iClient->IsConnected())
		{
		CAknInformationNote* infoDialog = new (ELeave) CAknInformationNote();
		HBufC* message = StringLoader::LoadLC(R_CONNECTED);
		infoDialog->ExecuteLD(*message);
		CleanupStack::PopAndDestroy(message);
		}
	}

/**
* Provides a UI Text Query Dialog requesting message to be sent.
*
* @param none
* @return true if message was composed by user
*/
TBool CXinSheBTView::ComposeMessageL()
	{
	TBool retVal = EFalse;

	delete iMessage;
	iMessage = NULL;
	iMessage = HBufC::NewL(KMaxMessageLength);
	TPtr message = iMessage->Des();

	HBufC* titleBuf = StringLoader::LoadLC(R_VIEW_TITLE);
	CAknTextQueryDialog* messageDialog = new (ELeave) CAknTextQueryDialog(message, *titleBuf, CAknTextQueryDialog::ENoTone);
	CleanupStack::PopAndDestroy(titleBuf);
	messageDialog->SetMaxLength(KMaxMessageLength);

	if (messageDialog->ExecuteLD(R_BLUETOOTH_VIEW_MESSAGE_DIALOG))
		{
		retVal = ETrue;
		}

	return retVal;
	}

/**
* Display the message received from remote device.
* @param aMessage the message received
* @return none
*/
void CXinSheBTView::DataReceivedL(const TDesC& aMessage)
	{
	TBuf<KMaxMessageLength + KFormattedMessagePrefixLength> formattedMessage;
	formattedMessage.Append(KFormattedMessagePrefix);
	formattedMessage.Append(aMessage);
	CAknQueryDialog* queryDialog = new (ELeave) CAknQueryDialog(formattedMessage, CAknQueryDialog::ENoTone);

	if (queryDialog->ExecuteLD(R_BLUETOOTH_VIEW_REPLY_DIALOG) == EBluetoothReply)
		{
		HandleCommandL(EXinSheBTViewSend);
		}
	}

/**
* Provide UI feedback of errors occured during communication
* @param aErrorCode code repesenting the error that occured
*/
void CXinSheBTView::HandleErrorL(TInt aErrorCode)
	{
	HBufC* message;

	switch (aErrorCode)
		{
		case (KErrDisconnected):
		case (KErrAbort):
			{
			message = StringLoader::LoadLC(R_DISCONNECTED);
			break;
			}

		case (KErrNotReady):
			{
			message = StringLoader::LoadLC(R_CONNECTION_LOST);
			break;
			}

		case (KErrCouldNotConnect):
			{
			message = StringLoader::LoadLC(R_COULD_NOT_CONNECT);
			break;
			}

		default:
			{
			message = StringLoader::LoadLC(R_UNKNOWN_ERROR);
			break;
			}
		}

	CAknWarningNote* errorDialog = new (ELeave) CAknWarningNote();
	errorDialog->ExecuteLD(*message);
	CleanupStack::PopAndDestroy(message);
	}

⌨️ 快捷键说明

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