To check whether a Wi-Fi connection is available or not, the following method will help you.
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; }
If wifi is not connected, the following methods will help add a data service.
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; }
An example of using the above methods.
String connParams=(isWifiConnected())?";interface=wifi":getConnParam();
Hope this helps you
koti
source share