现在的位置: 首页 > 综合 > 正文

Symbian网络接入点常用编程

2013年08月24日 ⁄ 综合 ⁄ 共 4760字 ⁄ 字号 评论关闭
Symbian网络接入点常用编程
2009-05-14 19:04

从CommDb中获取接入点信息

CCommsDatabase* commsDB=CCommsDatabase::NewL(EDatabaseTypeIAP);
CCommsDbTableView* table= commsDB->OpenIAPTableViewMatchingBearerSetLC(
ECommDbBearerGPRS|ECommDbBearerWLAN, //GPRS和无线局域网
ECommDbConnectionDirectionOutgoing); //方向无所谓的,写Unknown也可以
User::LeaveIfError(table->GotoFirstRecord()); //转到第一条记录

TInt err = KErrNone;
do
{
TBuf<32> name;
TUint32 id;
table->ReadTextL(TPtrC(COMMDB_NAME), name); //获取当前接入点的名称
table->ReadUintL(TPtrC(COMMDB_ID), id); //获取当前接入点的IapId
… //do something
err = table->GotoNextRecord(); //转到下一条记录
}
while (err == KErrNone);

以上代码演示了如何遍历接入点列表,实现方式比较直接,我们也可以使用另一个包装好的类CApSelect来实现:

CCommsDatabase* CommDb = CCommsDatabase::NewL(EDatabaseTypeIAP);
CApSelect* ApSelect = CApSelect::NewLC(*CommDb, KEApIspTypeAll ,EApBearerTypeAll ,KEApSortUidAscending); //指定了ISP类型和Bearer类型,以及排序规则

if (ApSelect->MoveToFirst())
{
do
{
TPtrC a = ApSelect->Name(); //接入点名称
TUint uid = ApSelect->Uid(); //接入点IapId
//put here your IAP stuff
}
while(ApSelect->MoveNext()); //下一条记录
}

弹框让用户选择接入点

单纯是让用户选择使用哪个接入点,可以借助RConnection类搞定:

RSocketServ iSocketServ; // data member
RConnection iConnection;
// data member

User::LeaveIfError(iSocketServ.Connect());
User::LeaveIfError(iConnection.Open(iSocketServ));

TCommDbConnPref connectPref;
connectPref.SetDialogPreference(ECommDbDialogPrefPrompt); //提示用户
connectPref.SetBearerSet(ECommDbBearerWLAN | ECommDbBearerGPRS);

User::LeaveIfError(iConnection.Start(connectPref)); //同步函数会弹出框来等待用户选择哪个接入点此时就建立了连接。

此处是选择接入点和建立连接一起绑定进行的,如果我们在选择接入点后并不是马上进行连接,该怎么实现呢?向下看。

显示缺省接入点设置&不经用户确认进行连接

显示缺省的接入点设置就是调出真机中设置-连接-接入点 中具体某个接入点的显示画面。用户可以根据现实来选择不同的接入点。得到了用户希望使用的接入点的IapId之后,就可以在需要的时候不经用户确认直接建立连接了:
使用类:CApSettingsHandler

      // Show IAP selection dialog,列出接入点
    CActiveApDb* aDb = CActiveApDb::NewL();
    CleanupStack::PushL(aDb);
CApSettingsHandler* settings = CApSettingsHandler::NewLC(
            *aDb,
            ETrue,
            EApSettingsSelListIsPopUp,
            EApSettingsSelMenuSelectNormal,
            KEApIspTypeAll,
            bearerFilter,
            KEApSortNameAscending,
            0,
            EVpnFilterBoth,
            ETrue);
   
    TInt iapRet = settings->RunSettingsL(0, iSelectedIap); //保存选择的接入点存到iSelectedIap中
    CleanupStack::PopAndDestroy(settings);
    CleanupStack::PopAndDestroy(aDb);
   
    if (iapRet != KApUiEventSelected)
    {
        // Exit no selection
        User::Leave(KErrNotReady);
    }
    else
    {
        // IAP Selected,根据选择的接入点进行连接
        // Open socket server and start the connection
        User::LeaveIfError(iSocketServ.Connect());
        User::LeaveIfError(iConnection.Open(iSocketServ));
       
        // Now we have the iap Id. Use it to connect for the connection
        TCommDbConnPref connectPref;
      
        // Setup preferences
        connectPref.SetDialogPreference(ECommDbDialogPrefPrompt);
       
        // Sets the CommDb ID of the IAP to use for this connection
        connectPref.SetIapId(iSelectedIap);
       
        // Start connection
        User::LeaveIfError(iConnection.Start(connectPref));
        ...
}

完全可以把上面的代码分成两个部分来实现选择接入点和建立连接的分离。即建立连接的代码如下:
        // IAP Selected,根据选择的接入点进行连接
        // Open socket server and start the connection
        User::LeaveIfError(iSocketServ.Connect());
        User::LeaveIfError(iConnection.Open(iSocketServ));
       
        // Now we have the iap Id. Use it to connect for the connection
        TCommDbConnPref connectPref;
      
        // Setup preferences
        connectPref.SetDialogPreference(ECommDbDialogPrefPrompt);
       
        // Sets the CommDb ID of the IAP to use for this connection
        connectPref.SetIapId(iSelectedIap);
       
        // Start connection
        User::LeaveIfError(iConnection.Start(connectPref));

MTM方式下屏蔽系统接入点的做法

  1. TImIAPChoice apChoice;
  2. apChoice.iIAP = 0;   // //store IAP id
  3. apChoice.iDialogPref = ECommDbDialogPrefDoNotPrompt;//prompt dialog
  4. CImIAPPreferences* apPref = CImIAPPreferences::NewLC();
  5. apPref->AddIAPL(apChoice);
  6. mailAccount->SavePopIapSettingsL(popAccount,*apPref);
  7. CleanupStack::PopAndDestroy(apPref);

 

void GetCMWAPID()
{
iCmwapId = 0;
CCommsDatabase* commDb = CCommsDatabase::NewL(EDatabaseTypeIAP);
CleanupStack::PushL(commDb);
commDb->BeginTransaction();
CCommsDbTableView* commView = commDb->OpenTableLC( TPtrC( OUTGOING_GPRS ) );
TInt ret = commView->GotoFirstRecord();
TBool find = EFalse;
while( ret == KErrNone )
{
TBuf8<50> name;
commView->ReadTextL( TPtrC(GPRS_APN), name );
TRAPD(leaveCode,commView->ReadUintL( TPtrC( COMMDB_ID), iCmwapId ) );
if( name.CompareF(_L8("CMWAP") ) == 0 )
{
CCommsDatabase* commDb = CCommsDatabase::NewL(EDatabaseTypeIAP);
CleanupStack::PushL(commDb);
CCommsDbTableView* pIAPView = commDb->OpenViewMatchingUintLC( TPtrC(IAP), TPtrC(IAP_SERVICE), iCmwapId ) ;
TInt ret = pIAPView->GotoFirstRecord();
while( ret == KErrNone )
{
pIAPView->ReadTextL( TPtrC(IAP_SERVICE_TYPE), name );
if( name.CompareF(_L8("OutgoingGPRS")) == 0 )
{
  TRAPD(err, pIAPView->ReadUintL(TPtrC(COMMDB_ID), iCmwapId));
  find = ETrue;
  break;
}
ret = pIAPView->GotoNextRecord();
}
CleanupStack::PopAndDestroy(2);
}
if(find)
break;
ret = commView->GotoNextRecord();
}
CleanupStack::PopAndDestroy(2);
}

抱歉!评论已关闭.