class CBtSvr : public public CActive
{
public :
// 当前服务器的状态
enum CBtBaseSvrStat {
// 空闲状态
EWaitingToGetConnection,
// 监听状态
EGettingConnection,
// 连接状态
EInConnection,
} ;
CBtSvr();
virtual ~CBtSvr(){} ;
// 类似消息处理的主循环
virtual void RunL();
// 启动服务器
virtual int StartL();
// 关闭服务器
virtual void CloseL();
// 取消当前提交的请求
virtual void DoCancel();
// 返回服务器当先状态当前状态
inline CBtBaseSvrStat Status(void) { return iStat; };
protected:
// socket 服务器
RSocketServ sockSvr;
// 监听socket
RSocket listenSock;
// 连接 socket
RSocket connSock;
// send状态标志
TRequestStatus iSendStatus;
// 服务的当前状态
CBtBaseSvrStat iStat;
TSockXfrLength iLen;
TInt channelNum;
protected:
// 绑定服务,留给子类实现
virtual int BindL(void) = 0;
// 监听
virtual int ListenL(void);
// 接受连接请求
virtual int AcceptL(void);
// 注册蓝牙服务中的Protocol段
virtual void BuildProtocolDescriptionL(CSdpAttrValueDES* aProtocolDescriptor, TInt aPort);
// 设置蓝牙服务安全
void SetSecurityOnChannelL(TBool aAuthentication, TBool aEncryption, TBool aAuthorisation, TInt aChannel);
// 注册蓝牙服务
int RegieterBlueToothServerL(const TDesC& KServiceName, TInt KSerialClassID);
// 绑定蓝牙服务名
int BuildSeriVCe(const TDesC& ServiceName, TInt KSerialClassID);
};
构造函数,初始化状态并加入活动对象调度器
CBtSvr::CBtSvr()
: iStat(EWaitingToGetConnection),CActive(0)
{
CActiveScheduler::Add(this);
}
开启蓝牙debug服务
int CBtSvr::StartL()
{
// 如果当前状态不对或者已经提交了事件
if (iStat != EWaitingToGetConnection || IsActive()) {
return -1;
}
// 建立绑定debug服务
if (BindL() < 0) {
return -1;
}
// 开始监听
if (ListenL() < 0) {
listenSock.Close();
sockSvr.Close();
return -1;
}
// 接受连接
if (AcceptL() < 0) {
listenSock.Close();
sockSvr.Close();
return -1;
}
return 0;
}
// 监听函数
int CBtSvr::ListenL(void)
{
assert(iStat == EWaitingToGetConnection);
// 只支持一个连接
if(listenSock.Listen(1) != KErrNone) {
return -1;
}
return 0;
}
// 提交接受连接请求
int CBtSvr::AcceptL(void)
{
if (connSock.Open(sockSvr) != KErrNone) {
return -1;
}
listenSock.Accept(connSock, iStatus);
iStat = EGettingConnection;
SetActive();
return 0;
}
// 实现CActive的doCancel函数供取消事件请求时调用
void CBtSvr::DoCancel()
{
if (!IsActive())
return;
switch (iStat) {
case EGettingConnection:
listenSock.CancelAll();
break;
case EInConnection:
connSock.CancelAll();
break;
default:
break;
}
}
// 服务器关闭函数
void CBtSvr::CloseL()
{
Cancel();
iStat = EWaitingToGetConnection;
listenSock.Close();
sockSvr.Close();
}
// 主事件循环
void CBtSvr::RunL()
{
if (iStatus != KErrNone) {
// 出错处理
switch (iStat) {
case EGettingConnection:
iStat = EWaitingToGetConnection;
break;
case EInConnection:
// 可能是对端断开连接
iStat = EWaitingToGetConnection;
// 重新提交接受连接事件
AcceptL();
break;
default:
break;
}
}
else {
switch (iStat) {
case EGettingConnection:
// 连接建立成功
iStat = EInConnection;
break;
case EInConnection:
// 收到数据不做任何处理
break;
default:
break;
}
}
}
// 这个函数是设置蓝牙服务安全,没有仔细研究,simple中搬出来
void CBtSvr::SetSecurityOnChannelL(TBool aAuthentication,
TBool aEncryption,
TBool aAuthorisation,
TInt aChannel)
{
const TUid KUidBTMobTimeObexAppValue = {0x0};
// a connection to the security manager
RBTMan secManager;
// a security session
RBTSecuritySettings secSettingsSession;
// define the security on this port
User::LeaveIfError(secManager.Connect());
CleanupClosePushL(secManager);
User::LeaveIfError(secSettingsSession.Open(secManager));
CleanupClosePushL(secSettingsSession);
// the security settings
TBTServiceSecurity serviceSecurity(KUidBTMobTimeObexAppValue, KSolBtRFCOMM, 0);
//Define security requirements
serviceSecurity.SetAuthentication(aAuthentication);
serviceSecurity.SetEncryption(aEncryption);
serviceSecurity.SetAuthorisation(aAuthorisation);
serviceSecurity.SetChannelID(aChannel);
TRequestStatus status;
secSettingsSession.ReGISterService(serviceSecurity, status);
User::WaitForRequest(status); // wait until the security settings are set
User::LeaveIfError(status.Int());
CleanupStack::PopAndDestroy(); // secManager
CleanupStack::PopAndDestroy(); // secSettingsSession
}
// 注册蓝牙串口服务
int CBtSvr::RegieterBlueToothServerL(const TDesC& KServiceName, TInt KSerialClassID)
{
// reg the sdp server database
RSdp sdp;
RSdpDatabase iSdpDatabase;
TSdpServRecordHandle iRecord;
// sdp服务器连接
if(sdp.Connect() != KErrNone) {
return -CNSE_SYS_ERR;
}
// 打开数据库
if(iSdpDatabase.Open(sdp) != KErrNone) {
return -CNSE_SYS_ERR;
}
// 创建一个服务
iSdpDatabase.CreateServiceRecordL(KSerialClassID, iRecord);
// add a Protocol to the record
CSdpAttrValueDES* vProtocolDescriptor = CSdpAttrValueDES::NewDESL(NULL);
CleanupStack::PushL(vProtocolDescriptor);
// 设置protocl相关信息
BuildProtocolDescriptionL(vProtocolDescriptor, channelNum);
iSdpDatabase.UpdateAttributeL(iRecord,
KSdpAttrIdProtocolDescriptorList,
*vProtocolDescriptor);
// Add 0x5 display 设置为可见
CSdpAttrValueDES* browseGroupList = CSdpAttrValueDES::NewDESL(NULL);
CleanupStack::PushL(browseGroupList);
browseGroupList
->StartListL() // List of protocols required for this method
->BuildUUIDL(TUUID(TUint16(0x1002)))
->EndListL();
iSdpDatabase.UpdateAttributeL(iRecord, KSdpAttrIdBrowseGroupList, *browseGroupList);
CleanupStack::PopAndDestroy(2);
// Add a name to the record,名字
iSdpDatabase.UpdateAttributeL(iRecord,
KSdpAttrIdBasePrimaryLanguage +
KSdpAttrIdOffsetServiceName,
KServiceName);
// Add a description to the record,描述
iSdpDatabase.UpdateAttributeL(iRecord,
KSdpAttrIdBasePrimaryLanguage +
KSdpAttrIdOffsetServiceDescription,
KServiceName);
iSdpDatabase.Close();
sdp.Close();
return 0;
}
// 这里设置蓝牙SDP服务中的协议相关信息
void CBtSvr::BuildProtocolDescriptionL(CSdpAttrValueDES* aProtocolDescriptor, TInt aPort)
{
TBuf8<1> channel;
channel.Append((TChar)aPort);
aProtocolDescriptor
->StartListL()
->BuildDESL()
->StartListL() // Details of lowest level protocol
// L2CAP层之上
->BuildUUIDL(KL2CAP)
->EndListL()
->BuildDESL()
->StartListL()
->BuildUUIDL(KRFCOMM)
// 这里是绑定的RFCOMM的端口号
->BuildUintL(channel)
->EndListL()
->EndListL();
}
int CBtSvr::BuildSerivce(const TDesC& ServiceName, TInt KSerialClassID)