特定接入点的查找/ 创建
1. Overview
特定接入点的查找/ 创建 应该是这样的一个过程。首先你从一个配置文件里面读出你要选择的 AP 的名字(一般情况下我们是知道要用那个接入点的,而且为了方便配置一般也把它写到配置文件中去)。在得到这个名字后,我们就遍历机 器上所有的接入点,查找是否有我们需要的,如果有我们就返回那个接入点的索引,如果没有就要在设备上创建一个 AP ,创建这个 AP 的参数也存在配置文件中,成功创建后返回其索引以备 后用。
2. Code
// 从配置文件中读出接入点的名称
TBuf<64> apnConnName;
GetAPNnameFromConfig(apnConnName);
apnConnName.LowerCase();
// 遍历通信数据库,寻找对应的接入点的索引
TUint32 iapId(-1);
CCommsDatabase* commDb = CCommsDatabase::NewL();
CleanupStack::PushL(commDb);
CCommsDbTableView* view = commDb->OpenTableLC(TPtrC( IAP ));
TFileName iapName;
User::LeaveIfError(view->GotoFirstRecord());
do
{
view->ReadTextL(TPtrC(COMMDB_NAME), iapName);
iapName.LowerCase();
if (!iapName.Compare(*apName))
{
view->ReadUintL(TPtrC(COMMDB_ID), iapId);
break;
}
}
While (KErrNone == view->GotoNextRecord());
CleanupStack::PopAndDestroy(2); // view, commDb
CleanupStack::PopAndDestroy(apName);
// 找到就返回
if (iapId != -1)
return iapId;
// 没有找到就创建一个,从配置文件中读出参数
TInt apnDataBearer;
TBuf<64> apnAccessPointName;
TBool apnAuthentication;
TBuf<64> apnProxyNameAddr;
TInt apnProxyPortNumber;
GetAPNBearerFromConf(apnDataBearer);
GetAPNnameFromConf(apnAccessPointName);
GetAPNAuthFromConf(apnAuthentication);
GetAPNProxyNameFromConf(apnProxyNameAddr);
GetAPNProxyPortFromConf(apnProxyPortNumber);
// 创建一个接入点
CApAccessPointItem* apn = CApAccessPointItem::NewLC();
apn->SetNamesL( apnConnName );
apn->SetBearerTypeL( (TApBearerType)apnDataBearer );
apn->WriteLongTextL( EApGprsAccessPointName, apnAccessPointName );
if( apnAuthentication )
{
apn->WriteBool( EApGprsDisablePlainTextAuth, ETrue );
apn->WriteBool( EApGprsDisablePlainTextAuth, ETrue );
}
else
{
apn->WriteBool( EApGprsDisablePlainTextAuth, EFalse );
}
if( apnProxyNameAddr.Length() )
{
apn->WriteLongTextL( EApProxyServerAddress, apnProxyNameAddr );
apn->WriteUint( EApProxyPortNumber, (TUint)apnProxyPortNumber );
}
CCommsDatabase* commDb = CCommsDatabase::NewL();
CleanupStack::PushL( commDb );
CApDataHandler* handler = CApDataHandler::NewLC( *commDb );
TInt commErr = commDb ->BeginTransaction();
newApId = (TInt32)handler->CreateFromDataL( *apn );
commErr = commDb->CommitTransaction();
CleanupStack::PopAndDestroy( handler );
CleanupStack::PopAndDestroy( commDb );
CleanupStack::PopAndDestroy( apn );
return newApId;