广播蓝牙服务
文章信息
兼容于
平台: S60 3rd Edition, MR
平台安全性
能力: LocalServices
文章
关键词: RSdpDatabase
翻译:
由 hoolee
最后由 hamishwillee
在 06 Aug 2012 编辑
- 详细描述
RSdp提供一个到Service Discovery Database的会话session。它可以用来生成到数据库功能的子会话。客户端必须在使
用RSdpDatabase子会话访问数据库时生成并连接到一个会话。
RSdpDatabase是一个SDP的子会话,通过它服务可以记录,它们的属性可以被添加,删除或更新。
下列示例代码展示了如何广播一个客户端蓝牙服务(KBT_serviceID 0x10ff),如何发现特殊服务的范例在这里 Archived:Discovering Bluetooth services using Symbian C++ 可以找到。
监听蓝牙连接可以在这里找到Archived:Establishing a Bluetooth connection using Symbian C++,CMyServiceAdvertiser::StartAdvertiserL(TInt aChannel)中的频道数目参数可以在这里找到。
MMP文件
需要下列能力和链接库
CAPABILITY LocalServices
LIBRARY sdpagent.lib
LIBRARY sdpdatabase.lib
头文件
#include <btsdp.h>
#include <bt_sock.h>
// The service id that identifies the service. This id will be
// used when advertising the service and discovering the service.
#define KBT_serviceID 0x10ff
// Service name and description for our service
_LIT(KBTServiceName, "BTpmp");
_LIT(KBTServiceDesc, "BTpmp");
// Service discovery protocol session
RSdp iSdp;
// Service discovery database (sdp)
RSdpDatabase iSdpDB;
// Service record
TSdpServRecordHandle iRecord;
// Service record state
TInt iRecordState;
源文件
void CMyServiceAdvertiser::StartAdvertiserL(TInt aChannel)
{
// Open sdp session
User::LeaveIfError(iSdp.Connect());
// Open sdp database session
User::LeaveIfError(iSdpDB.Open(iSdp));
// Create a record of the correct service class
TUUID serviceUUID(KBT_serviceID);
iSdpDB.CreateServiceRecordL(serviceUUID, iRecord);
// Add a protocol to the record
CSdpAttrValueDES* protocolDescriptorList = CSdpAttrValueDES::NewDESL(NULL);
CleanupStack::PushL(protocolDescriptorList);
TBuf8<1> channel;
channel.Append((TChar)aChannel);
// Create protocol list for our service
protocolDescriptorList
->StartListL() // list of protocols required for this method
->BuildDESL()
->StartListL()
->BuildUUIDL(KL2CAP)
->EndListL()
->BuildDESL()
->StartListL()
->BuildUUIDL(KRFCOMM)
->BuildUintL(channel)
->EndListL()
->EndListL();
// Set protocol list to the record
iSdpDB.UpdateAttributeL(iRecord, KSdpAttrIdProtocolDescriptorList,
*protocolDescriptorList);
CleanupStack::PopAndDestroy(protocolDescriptorList);
// Add a name to the record
iSdpDB.UpdateAttributeL(iRecord,
KSdpAttrIdBasePrimaryLanguage +
KSdpAttrIdOffsetServiceName,
KBTServiceName);
// Add a description to the record
iSdpDB.UpdateAttributeL(iRecord,
KSdpAttrIdBasePrimaryLanguage +
KSdpAttrIdOffsetServiceDescription,
KBTServiceDesc);
// Set service available
UpdateAvailabilityL(ETrue);
}
设置广播服务的可用性,service discovery database上的服务记录将依此更新:
void CMyServiceAdvertiser::UpdateAvailabilityL(TBool aAvailable)
{
TInt state = aAvailable ? 0xFF : 0x00;
// Set availability
iSdpDB.UpdateAttributeL(iRecord, KSdpAttrIdServiceAvailability, state);
// Mark record changed
iSdpDB.UpdateAttributeL(iRecord, KSdpAttrIdServiceRecordState,
++iRecordState);
}
停止广播服务
void CMyServiceAdvertiser::StopAdvertiserL()
{
if ( iRecord!=NULL )
{
// Delete record from service discovery database
iSdpDB.DeleteRecordL(iRecord);
// Close sdp and sdp db sessions
iSdpDB.Close();
iSdp.Close();
iRecord=NULL;
}
}


(no comments yet)