如何确定设备连接到Internet时使用的网络

时间:2012-08-22 04:50:52

标签: java blackberry

到目前为止,我尝试通过使用RadioInfo类来验证哪个网络已启用并检查RadioInfo.getNumberOfPacketsReceived()\Sent()中是否有任何更改,但是如果多于一个网络,则此方法无效上。任何人都可以指出我正确的方向。

对不起拼写和语法,英语不是我的第一语言。

1 个答案:

答案 0 :(得分:0)

您需要在网址后附加您的连接类型。

public static String getConnectionString() {

String connectionString = null;

// Simulator behaviour is controlled by the USE_MDS_IN_SIMULATOR
// variable.
if (DeviceInfo.isSimulator()) {

    connectionString = ";deviceside=true";
}

// Wifi is the preferred transmission method
else if (WLANInfo.getWLANState() == WLANInfo.WLAN_STATE_CONNECTED) {

    connectionString = ";interface=wifi";
}

// Is the carrier network the only way to connect?
else if ((CoverageInfo.getCoverageStatus() & CoverageInfo.COVERAGE_DIRECT) == CoverageInfo.COVERAGE_DIRECT) {

    String carrierUid = getCarrierBIBSUid();

    if (carrierUid == null) {
        // Has carrier coverage, but not BIBS. So use the carrier's TCP
        // network

        connectionString = ";deviceside=true";
    } else {
        // otherwise, use the Uid to construct a valid carrier BIBS
        // request

        connectionString = ";deviceside=false;connectionUID="+carrierUid + ";ConnectionType=mds-public";
    }
}

// Check for an MDS connection instead (BlackBerry Enterprise Server)
else if ((CoverageInfo.getCoverageStatus() & CoverageInfo.COVERAGE_MDS) == CoverageInfo.COVERAGE_MDS) {

    connectionString = ";deviceside=false";
}

// If there is no connection available abort to avoid hassling the user
// unnecssarily.
else if (CoverageInfo.getCoverageStatus() == CoverageInfo.COVERAGE_NONE) {
    connectionString = "none";

}

// In theory, all bases are covered by now so this shouldn't be reachable.But hey, just in case ...
else {

    connectionString = ";deviceside=true";
}



return connectionString;

}

/**
 * Looks through the phone's service book for a carrier provided BIBS
 * network
 * 
 * @return The uid used to connect to that network.
 */
 private synchronized static String getCarrierBIBSUid() {
ServiceRecord[] records = ServiceBook.getSB().getRecords();
int currentRecord;

for (currentRecord = 0; currentRecord < records.length; currentRecord++) {
    if (records[currentRecord].getCid().toLowerCase().equals("ippp")) {
        if (records[currentRecord].getName().toLowerCase()
                .indexOf("bibs") >= 0) {
            return records[currentRecord].getUid();
        }
    }
}

return null;
}

一旦你写了这段代码。使用

con = (HttpConnection) Connector.open(url + getConnectionString());

它将确定可用的网络类型。