2011-09-21 91 views
7

我開發了一個黑莓設備應用程序。如果通過數據服務提供商使用互聯網,該應用程序工作正常。檢查黑莓應用程序中的wifi條件

我有BB 9550,我想用我的應用程序使用WiFi。我嘗試了很多,但我無法得到正確的答案來檢查WiFi條件。

如何區分我們的無線或數據服務提供商?

回答

10

對於檢查wifi連接與否,以下方法將幫助你。

public static boolean isWifiConnected() 
    { 
     try 
     { 
      if (RadioInfo.getSignalLevel(RadioInfo.WAF_WLAN) != RadioInfo.LEVEL_NO_COVERAGE) 
      { 
       return true; 
      } 
     } 
     catch(Exception e) 
     { 
      System.out.println("Exception during get WiFi status"); 
     } 
     return false; 
    } 

如果wifi未連接,以下方法將有助於添加數據服務。

public static String getConnParam(){ 
     String connectionParameters = ""; 
     if (WLANInfo.getWLANState() == WLANInfo.WLAN_STATE_CONNECTED) { 
     // Connected to a WiFi access point 
     connectionParameters = ";interface=wifi"; 
     } else { 
     int coverageStatus = CoverageInfo.getCoverageStatus(); 
     ServiceRecord record = getWAP2ServiceRecord(); 
     if (record != null 
     && (coverageStatus & CoverageInfo.COVERAGE_DIRECT) == 
     CoverageInfo.COVERAGE_DIRECT) { 
     // Have network coverage and a WAP 2.0 service book record 
     connectionParameters = ";deviceside=true;ConnectionUID=" 
     + record.getUid(); 
     } else if ((coverageStatus & CoverageInfo.COVERAGE_MDS) == 
     CoverageInfo.COVERAGE_MDS) { 
     // Have an MDS service book and network coverage 
     connectionParameters = ";deviceside=false"; 
     } else if ((coverageStatus & CoverageInfo.COVERAGE_DIRECT) == 
     CoverageInfo.COVERAGE_DIRECT) { 
     // Have network coverage but no WAP 2.0 service book record 
     connectionParameters = ";deviceside=true"; 
     } 

    } 
     return connectionParameters; 
    } 
     private static ServiceRecord getWAP2ServiceRecord() { 
      ServiceBook sb = ServiceBook.getSB(); 
      ServiceRecord[] records = sb.getRecords(); 
      for(int i = 0; i < records.length; i++) { 
      String cid = records[i].getCid().toLowerCase(); 
      String uid = records[i].getUid().toLowerCase(); 

      if (cid.indexOf("wptcp") != -1 && 
      uid.indexOf("wifi") == -1 && 
      uid.indexOf("mms") == -1) { 
      return records[i]; 
      } 
      } 
      return null; 
      } 

使用上述方法的示例。

String connParams=(isWifiConnected())?";interface=wifi":getConnParam(); 

希望這將幫助你

+0

Thanx我得到了解決方案。現在它的工作 – Hitarth

3

試試這個:

private static String getParameters() { 
    if (GetWiFiCoverageStatus()) { 
     return ";deviceside=true;interface=wifi"; 
    } 
    else { 
     return yourParametersForEdge 
    } 
} 

private static boolean GetWiFiCoverageStatus() { 
    if((WLANInfo.getWLANState() == WLANInfo.WLAN_STATE_CONNECTED)) { 
     return true; 
    } 
    else 
     return false;   
} 

而當你需要連接,你就必須將參數添加到URL:

yourUrl = yourUrl + getParameters(); 
+0

Thaks你的代碼你的代碼也有助於我在url中添加參數 – Hitarth