📄 bluetoothadvertiser.cpp
字号:
/**
*
* @brief Definition of CBluetoothAdvertiser
*
* Copyright (c) EMCC Software Ltd 2003
* @version 1.0
*/
#include <bt_sock.h>
#include "BluetoothAdvertiser.h"
#include "BluetoothDefinitions.h"
// Constants
const TUint KStateFullyUnused = 0xFF;
const TUint KStateFullyUsed = 0x00;
/**
* Constructor.
* Private constructor.
* @see NewL
* @see NewLC
* @param none
* @return none
**/
CBluetoothAdvertiser::CBluetoothAdvertiser()
: iRecord(0),
iIsConnected(EFalse)
{
}
/**
* Destructor.
* Stop Advertising and cleanup sessions to Service Discovery Database
* @param none
* @return none
**/
CBluetoothAdvertiser::~CBluetoothAdvertiser()
{
if (IsAdvertising())
{
TRAPD(err,StopAdvertisingL());
}
iSdpDatabase.Close();
iSdpSession.Close();
}
/**
* Factory Constructor.
* Only available way to construct class.
* This function can leave L
* @param none
* @return new instance of the CBluetoothAdvertiser
*/
CBluetoothAdvertiser* CBluetoothAdvertiser::NewL()
{
CBluetoothAdvertiser* self = CBluetoothAdvertiser::NewLC();
CleanupStack::Pop(self);
return self;
}
/**
* Factory Constructor.
* Only available way to construct class.
* This function can leave L.
* @param none
* @return new instance of the CBluetoothAdvertiser, this is also left on the cleanup stack C
*/
CBluetoothAdvertiser* CBluetoothAdvertiser::NewLC()
{
CBluetoothAdvertiser* self = new (ELeave) CBluetoothAdvertiser();
CleanupStack::PushL(self);
self->ConstructL();
return self;
}
/**
* Second Stage Constructor.
* This function can leave L
* @param none
* @see NewL
* @see NewLC
* @param none
* @return none
*/
void CBluetoothAdvertiser::ConstructL()
{
}
/**
* Connect Method
* If not presently connected open a session on the Service Discovery Database
**/
void CBluetoothAdvertiser::ConnectL()
{
if (!iIsConnected)
{
User::LeaveIfError(iSdpSession.Connect());
User::LeaveIfError(iSdpDatabase.Open(iSdpSession));
iIsConnected = ETrue;
}
}
/**
* Start Advertising.
* Create Service Record, add attributes, and enter into the Service Discovery Database
* @param aPort the port number to advertise
* @return none
**/
void CBluetoothAdvertiser::StartAdvertisingL(TInt aPort)
{
if (IsAdvertising())
{
// could be advertising on a different port
StopAdvertisingL();
}
if (! iIsConnected)
{
ConnectL();
}
iSdpDatabase.CreateServiceRecordL(KServiceClass, iRecord);
// add a Protocol to the record
CSdpAttrValueDES* vProtocolDescriptor = CSdpAttrValueDES::NewDESL(NULL);
CleanupStack::PushL(vProtocolDescriptor);
BuildProtocolDescriptionL(vProtocolDescriptor,aPort);
iSdpDatabase.UpdateAttributeL(iRecord, KSdpAttrIdProtocolDescriptorList, *vProtocolDescriptor);
CleanupStack::PopAndDestroy(vProtocolDescriptor);
// Add a name to the record
iSdpDatabase.UpdateAttributeL(iRecord,
KSdpAttrIdBasePrimaryLanguage+KSdpAttrIdOffsetServiceName,
KServiceName);
// Add a description to the record
iSdpDatabase.UpdateAttributeL(iRecord,
KSdpAttrIdBasePrimaryLanguage+KSdpAttrIdOffsetServiceDescription,
KServiceDescription);
}
/**
* Update Availability.
* Toggle function for the availability of Bluetooth Service
* @param aIsAvailable a boolean representing the availablity of the service
* @return none
**/
void CBluetoothAdvertiser::UpdateAvailabilityL(TBool aIsAvailable)
{
TUint state;
if (aIsAvailable)
{
state = KStateFullyUnused;
}
else
{
state = KStateFullyUsed; // Fully used -> can't connect
}
// Update the availibility attribute field
iSdpDatabase.UpdateAttributeL(iRecord, KSdpAttrIdServiceAvailability, state);
// Mark the record as changed - by increasing its state number (version)
iSdpDatabase.UpdateAttributeL(iRecord, KSdpAttrIdServiceRecordState, ++iRecordState);
}
/**
* Stop Advertising.
* Used when a connection is made (to avoid multiple connections) or when closing down advertiser
* @param none
* @return none
**/
void CBluetoothAdvertiser::StopAdvertisingL()
{
if (IsAdvertising())
{
iSdpDatabase.DeleteRecordL(iRecord);
iRecord = 0;
}
}
/**
* Build Protocol Description.
* Builds the Data Element Sequence for the KSdpAttrIdProtocolDescriptorList attribute of Bluetooth Service record
* @param aProtocolDescriptor a pointer to the protocol descriptor to be populated with data
* @param aPort the port number to advertise
* @return none
**/
void CBluetoothAdvertiser::BuildProtocolDescriptionL(CSdpAttrValueDES* aProtocolDescriptor, TInt aPort)
{
TBuf8<1> channel;
channel.Append((TChar)aPort);
aProtocolDescriptor
->StartListL() // List of protocols required for this method
->BuildDESL()
->StartListL() // Details of lowest level protocol
->BuildUUIDL(KL2CAP)
->EndListL()
->BuildDESL()
->StartListL()
->BuildUUIDL(KRFCOMM)
->BuildUintL(channel)
->EndListL()
->EndListL();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -