www.pudn.com > ExampleBluetoothChat.rar > BluetoothChatAppUi.cpp


/** 
* 
* @brief Definition of CBluetoothChatAppUi 
* 
* Copyright (c) EMCC Software Ltd 2003 
* @version 1.0 
*/ 
 
// Class include 
#include "BluetoothChatAppUi.h" 
 
// System includes 
#include  
#include  
#include  
#include  
#include 		// R_BLUETOOTHCHAT_DIALOG 
#include  
#include 		// StringLoader 
 
// User includes 
#include "BluetoothChatDialog.h"	// CBluetoothChatDialog 
#include "BluetoothChat.hrh"		// commands 
#include "BluetoothDeviceSearcher.h"	// CBluetoothDeviceSearcher 
#include "BluetoothServiceSearcher.h"	// CBluetoothServiceSearcher 
#include "BluetoothClient.h"		// CBluetoothClient 
#include "BluetoothServer.h"		// CBluetoothServer 
 
 
// Constants 
const TInt KFormattedMessagePrefixLength = 10; 
_LIT(KFormattedMessagePrefix, "Message:\n"); 
const TInt KServerCleanupDelayMicroSeconds = 4000000; 
 
 
// ================= MEMBER FUNCTIONS ======================= 
 
/** 
* Symbian OS 2nd phase constructor. 
* Constructs and executes the application's dialog, 
* setting itself as the dialog's MOP parent, and adds it to the control 
* stack. 
* 
* Creates an instance of the Bluetooth Client and Server 
* 
* @param none 
* @return none 
*/ 
void CBluetoothChatAppUi::ConstructL() 
	{ 
	BaseConstructL(); 
	iAppDialog = new (ELeave) CBluetoothChatDialog; 
	iAppDialog->SetMopParent(this); 
	iAppDialog->ExecuteLD(R_BLUETOOTHCHAT_DIALOG); 
	AddToStackL(iAppDialog); 
 
	iClient = CBluetoothClient::NewL(*this); 
	iServer = CBluetoothServer::NewL(*this); 
	} 
 
/** 
* Destructor. 
* Removes the application's dialog from the stack and deletes it. 
* 
* Deletes the Bluetooth Client and Server, and message buffer 
* 
*/ 
CBluetoothChatAppUi::~CBluetoothChatAppUi() 
	{ 
	if (iAppDialog) 
		{ 
		RemoveFromStack(iAppDialog); 
		delete iAppDialog; 
		} 
 
	delete iWaitDialog; 
 
	delete iClient; 
	delete iServer; 
 
	delete iMessage; 
	} 
 
/** 
* 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 CBluetoothChatAppUi::DynInitMenuPaneL(TInt aResourceId, CEikMenuPane* aMenuPane) 
	{ 
	if (aResourceId == R_BLUETOOTHCHAT_MENU_PANE) 
		{ 
		if (iClient->IsConnected() || iServer->IsConnected()) 
			{ 
			aMenuPane->SetItemDimmed(EBluetoothChatConnect, ETrue); 
			aMenuPane->SetItemDimmed(EBluetoothChatReceive, ETrue); 
			if (iClient->IsConnected()) 
				{ 
				aMenuPane->SetItemDimmed(EBluetoothChatStopReceive, ETrue); 
				} 
			if (!iClient->AvailableToSend() && !iServer->AvailableToSend()) 
				{ 
				aMenuPane->SetItemDimmed(EBluetoothChatSend, ETrue); 
				} 
			} 
		else 
			{ 
			aMenuPane->SetItemDimmed(EBluetoothChatStopReceive, ETrue); 
			aMenuPane->SetItemDimmed(EBluetoothChatSend, ETrue); 
			} 
		} 
	} 
 
/** 
* From CEikAppUi, takes care of command handling. 
* 
* @param aCommand command to be handled 
* @return none 
*/ 
void CBluetoothChatAppUi::HandleCommandL(TInt aCommand) 
	{ 
	switch (aCommand) 
		{ 
		case EBluetoothChatConnect: 
			{ 
			FindRemoteDeviceL(); 
			break; 
			} 
 
		case EBluetoothChatReceive: 
			{ 
			StartReceivingL(); 
			break; 
			} 
 
		case EBluetoothChatStopReceive: 
			{ 
			iServer->StopL(); 
			break; 
			} 
 
		case EBluetoothChatSend: 
			{ 
			if (ComposeMessageL()) 
				{ 
				if (iClient->AvailableToSend()) 
					{ 
					iClient->Send(*iMessage); 
					} 
				else if (iServer->AvailableToSend()) 
					{ 
					iServer->Send(*iMessage); 
					} 
				} 
			break; 
			} 
 
		case EEikCmdExit: 
			{ 
			Exit(); 
			break; 
			} 
 
		default: 
			break; 
		} 
	} 
 
/** 
* 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 CBluetoothChatAppUi::FindRemoteDeviceL() 
	{ 
	iDeviceSearcher = CBluetoothDeviceSearcher::NewL(*this); 
	iDeviceSelectionResponse = new (ELeave) TBTDeviceResponseParamsPckg(); 
	iDeviceSearcher->SelectDeviceL(*iDeviceSelectionResponse); 
	} 
 
void CBluetoothChatAppUi::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; 
	} 
 
/** 
* Begin Server side communication. 
* 
* Starts Server - this will start the server and configure the security settings 
* 
* @param none 
* @return none 
*/ 
void CBluetoothChatAppUi::StartReceivingL() 
	{ 
	iServer->StartServerL(); 
	} 
 
 
/** 
* React to completion of server startup. 
* 
* Starts advertising on server 
* Display a dialog box with the option to cancel this listening process 
* 
* @param none 
* @return none 
*/ 
void CBluetoothChatAppUi::ServerStartedL() 
	{ 
 
	// start the server's advertiser. 
	iServer->StartAdvertisingL(); 
 
	// display a dialog to allow a user to cancel listening if required. 
	delete iWaitDialog; 
	iWaitDialog = NULL; 
	iWaitDialog = new (ELeave) CAknWaitDialog(NULL, ETrue); 
	HBufC* waitingMessage = StringLoader::LoadLC(R_WAITING_MESSAGE); 
	iWaitDialog->SetTextL(*waitingMessage); 
	CleanupStack::PopAndDestroy(waitingMessage); 
	TInt retVal = iWaitDialog->ExecuteLD(R_BLUETOOTH_CONNECTION_WAIT_DIALOG); 
	iWaitDialog = NULL; 
 
	if (retVal != EAknSoftkeyDone) 
		{ 
		// User has cancelled this connection process 
		iServer->StopL(); 
		} 
 
	} 
 
 
/** 
* Connection Made. 
* 
* Server - Remove the Cancel dialog 
* Client - Provide feedback that a connection has been made 
* @param none 
* @return none 
*/ 
void CBluetoothChatAppUi::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 CBluetoothChatAppUi::ComposeMessageL() 
	{ 
	TBool retVal = EFalse; 
 
	delete iMessage; 
	iMessage = NULL; 
	iMessage = HBufC::NewL(KMaxMessageLength); 
	TPtr message = iMessage->Des(); 
 
	HBufC* titleBuf = StringLoader::LoadLC(R_CHAT_TITLE); 
	CAknTextQueryDialog* messageDialog = new (ELeave) CAknTextQueryDialog(message, *titleBuf, CAknTextQueryDialog::ENoTone); 
	CleanupStack::PopAndDestroy(titleBuf); 
	messageDialog->SetMaxLength(KMaxMessageLength); 
 
	if (messageDialog->ExecuteLD(R_BLUETOOTH_CHAT_MESSAGE_DIALOG)) 
		{ 
		retVal = ETrue; 
		} 
 
	return retVal; 
	} 
 
/** 
* Display the message received from remote device. 
* @param aMessage the message received 
* @return none 
*/ 
void CBluetoothChatAppUi::DataReceivedL(const TDesC& aMessage) 
	{ 
	TBuf formattedMessage; 
	formattedMessage.Append(KFormattedMessagePrefix); 
	formattedMessage.Append(aMessage); 
	CAknQueryDialog* queryDialog = new (ELeave) CAknQueryDialog(formattedMessage, CAknQueryDialog::ENoTone); 
 
	if (queryDialog->ExecuteLD(R_BLUETOOTH_CHAT_REPLY_DIALOG) == EBluetoothReply) 
		{ 
		HandleCommandL(EBluetoothChatSend); 
		} 
	} 
 
/** 
* Provide UI feedback of errors occured during communication 
* @param aErrorCode code repesenting the error that occured 
*/ 
void CBluetoothChatAppUi::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); 
	} 
 
// End of File