www.pudn.com > Bluetooth_PMP_Example_v1_0.zip > 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 	// for input dialogs 
#include  
 
#include  
 
#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; idx8) 	 
	{ 
		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 ) 
			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; idxiName; 
			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; idxSendDataL(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 ); 
	}