⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 btdiscoverer.cpp

📁 Series_60_DP_1_0_2_0_Thread_And_Active_Objects_Example_v1_0 Symbian os S60线程与活动对象实例
💻 CPP
字号:
/*
* ============================================================================
*  Name     : CBTDiscoverer from BTDiscoverer.h
*  Part of  : ThreadAndAO
*  Created  : 12.1.2005 by Forum Nokia
*  Version  : 1.0
*  Copyright: Nokia Corporation
* ============================================================================
*/

// INCLUDES
#include "..\inc\btdiscoverer.h"
#include "SharedIntermediator.h"
#include "BluetoothInfo.h"

#include <btmanclient.h>
#include <btextnotifiers.h>
#include <aknlists.h>
#include <btsdp.h>

#include <bt_sock.h> 
#include <BTDevice.h>

_LIT(KBTProtocol, "BTLinkManager");
_LIT( KLine, "-" );

const TInt KMinBTAddressLength = 5;

CBTDiscoverer* CBTDiscoverer::NewL(CSharedIntermediator* aSMediator)
    {
    CBTDiscoverer* self = CBTDiscoverer::NewLC(aSMediator);
    CleanupStack::Pop(self);
    return self;
    }

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

// ----------------------------------------------------------------------------
// CBTDiscoverer::CBTDiscoverer(void)
//
// Constructor.
// ----------------------------------------------------------------------------
CBTDiscoverer::CBTDiscoverer(CSharedIntermediator* aSMediator)
	: CActive(EPriorityStandard),iSMediator (aSMediator)
	{
	iDeviceNames =  new (ELeave) CArrayPtrFlat<HBufC>(1);
	iInqSockAddrArray = new (ELeave) RArray<TInquirySockAddr>; 
	iDeviceAddress = new (ELeave) RArray<HBufC*>;
	}

// ----------------------------------------------------------------------------
// CBTDiscoverer::CBTDiscoverer(void)
//
// Destructor.
// ----------------------------------------------------------------------------
CBTDiscoverer::~CBTDiscoverer(void)
	{
	Cancel();

	iDeviceNames->ResetAndDestroy();
	delete iDeviceNames;
	iDeviceNames = 0;

	iInqSockAddrArray->Close();
	delete iInqSockAddrArray;
	iInqSockAddrArray = 0;

	iDeviceAddress->Close();
	delete iDeviceAddress;
	iDeviceAddress = 0;

	iHostResolver.Close();
	iSocketServer.Close();
	}

// Standard Symbian OS 2nd phase constructor
void CBTDiscoverer::ConstructL()
    {
	CActiveScheduler::Add(this);
    }
// ----------------------------------------------------------------------------
// CBTDiscoverer::GetNames()
//
// Return a pointer to list of names.
// ----------------------------------------------------------------------------
CArrayPtrFlat< HBufC >* CBTDiscoverer::GetNames()
	{
	return iDeviceNames;
	}

// ----------------------------------------------------------------------------
// CBTDiscoverer::GetDeviceAddress()
//
// Return a pointer to list of addresses.
// ----------------------------------------------------------------------------
RArray<HBufC*>* CBTDiscoverer::GetDeviceAddress()
	{
	return iDeviceAddress;
	}

// ----------------------------------------------------------------------------
// CBTDiscoverer::StartDiscoveringDevicesL()
//
// Find devices.
// ----------------------------------------------------------------------------
 void CBTDiscoverer::StartDiscoveringDevicesL()
	{
	User::LeaveIfError(iSocketServer.Connect() ); 
	
	TProtocolDesc protocolInfo;

	// Gets the description of a protocol by name.
	User::LeaveIfError(iSocketServer.FindProtocol( (TBuf<32> &)KBTProtocol, protocolInfo) ); 

	// Initialises a name resolution service provided by a particular protocol. 
	// It must be called before other object functions are used.
	User::LeaveIfError(iHostResolver.Open(iSocketServer, protocolInfo.iAddrFamily, 
		protocolInfo.iProtocol ) ); 

	// Interested in all bluetooth devices that have BT turned on.
	iSockAddr.SetIAC(KGIAC); 

	// Resolve name of the device and device address. Do not use cache.
	iSockAddr.SetAction(KHostResInquiry | KHostResName | KHostResIgnoreCache); 
	
	// Start discovering devices asynchronously. RunL is called.
	iHostResolver.GetByAddress(iSockAddr,iNameEntry,iStatus); 

	// Set this active object active.
	SetActive();

	}

// ----------------------------------------------------------------------------
// CBTDiscoverer::RunL()
//
// ----------------------------------------------------------------------------
 void CBTDiscoverer::RunL()
	{
	
	// Found a new device, iStatus would be KErrHostResNoMoreResults
	// if no more new devices are found.
	if ( iStatus == KErrNone ) 
		{	
		PushListL(iNameEntry); 
		// Find the next bluetooth device
		iHostResolver.Next(iNameEntry,iStatus);

		SetActive();
		}	
	}

// ----------------------------------------------------------------------------
// CBTDiscoverer::TransformSockAddress(TInquirySockAddr& aInquirySockAddr)
//
// Transform inquiryaddress to format XX-XX-XX-XX-XX (BT address)
// ----------------------------------------------------------------------------
void CBTDiscoverer::TransformSockAddress(TInquirySockAddr& aInquirySockAddr) 
	{
	TBTDevAddr localAddr= aInquirySockAddr.BTAddr();
	TBuf<1024> btAddr; 
		
	for(TInt i=0; i < localAddr.Des().Length() ;i++) 
		{ 
		btAddr.AppendNumFixedWidthUC(localAddr[i],EHex,2);
		if ( i != localAddr.Des().Length() -1 ) 
			{
			btAddr.Append( KLine );
			}
		}

	HBufC* element = HBufC::New(KBTDeviceAddress);
	element = btAddr.Alloc();
	iDeviceAddress->Append( element );
	}

// ----------------------------------------------------------------------------
// CBTDiscoverer::RunError(TInt aError)
// ----------------------------------------------------------------------------
TInt CBTDiscoverer::RunError(TInt)
	{
	return KErrNone;
	}

// ----------------------------------------------------------------------------
// CBTDiscoverer::DoCancel()
// ----------------------------------------------------------------------------
 void CBTDiscoverer::DoCancel()
	{
	iHostResolver.Cancel();
	}

// ----------------------------------------------------------------------------
// CBTDiscoverer::PushListL(TNameEntry& aNameEntry) 
//
// Separete both name/address from the aNameEntry and insert them into separe 
// lists.
// ----------------------------------------------------------------------------
void CBTDiscoverer::PushListL(TNameEntry& aNameEntry) 
	{ 

	TInquirySockAddr &addr = TInquirySockAddr::Cast(aNameEntry().iAddr); 

	// Add only different devices. 
	for( TInt i = 0; i < iInqSockAddrArray->Count(); i++ )
		{ 
		if( (*iInqSockAddrArray)[i].BTAddr() == addr.BTAddr( ) || 
			 addr.BTAddr().Des().Length() < KMinBTAddressLength )
			{ 
			return; 
			}	 
		}	 

	HBufC* element = HBufC::New(KBTDeviceLength);

	// Check the length of the bluetooth device name
	if ( aNameEntry().iName.Length() >= KBTDeviceLength )
		{
		element = aNameEntry().iName.Left( KBTDeviceLength -1 ).Alloc();
		}
	// Get the name 
	else {
		element = aNameEntry().iName.Alloc();
		}
	// Add the name onto the list. Devices must have some kind of name otherwise
	// it won't be added.
	if ( element->Length() != 0 ) 
		{
		iDeviceNames->AppendL( element );
		}
	else 
		{
		return;
		}
	
	TInquirySockAddr *addr_ptr = new (ELeave)TInquirySockAddr( addr ); 
	
	CleanupStack::PushL(addr_ptr); 

	// Append the BT address onto the list
	User::LeaveIfError( iInqSockAddrArray->Append(*addr_ptr) ); 
	CleanupStack::Pop(); 

	// Make address printable.
	TransformSockAddress( *addr_ptr );

	// Get name of the device and BT address as hex. Add name&address into shared intermediator.
	TPtr16 deviceName = (*iDeviceNames)[ iDeviceNames->Count() - 1 ]->Des();
	TPtr16 deviceBTAddr = (*iDeviceAddress)[ iDeviceAddress->Count() - 1 ]->Des();
	TBluetoothInfo btInfo = TBluetoothInfo (deviceName, deviceBTAddr);

	// Give data to the shared intermediator. Shared intermediator gives
	// device names to the listbox after this.
	iSMediator->AddBluetoothInfo(btInfo);
	}

// ----------------------------------------------------------------------------
// CBTDiscoverer::RefreshDevices()
//
// Cancels the wait for completion of an outstanding bluetooth request. 
// Cleans lists and starts the search again by calling GetByAddress.
// ----------------------------------------------------------------------------
void CBTDiscoverer::RefreshDevices()
	{
	
	// In case bt discover is already active.
	Cancel();

	 //Clear all lists 
	iDeviceNames->Reset();
	iInqSockAddrArray->Reset();
	iDeviceAddress->Reset();
	
	iSockAddr.SetAction(KHostResInquiry | KHostResName | KHostResIgnoreCache); 

	// Start discovering devices asynchronously. RunL is called afterwards.
	iHostResolver.GetByAddress(iSockAddr,iNameEntry,iStatus); 
	SetActive();
	}

⌨️ 快捷键说明

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