Я знаю, что эта ветка давно закрыта, но я подумал, что если это кому-то понадобится, когда-нибудь это понадобится. Эта проблема на несколько часов замедлила мою разработку, поэтому я решил, что ей стоит поделиться. Я предоставлю два метода, которые помогут проверить устройство и использовать соответствующий доступный транспорт. Чтобы использовать эти методы, просто вызовите метод getConnectionString и добавьте в конец строки URL-адреса, и все готово.
//the method returns the connection string
public String getConnectionString()
{
String connectionString = "";
if(DeviceInfo.isSimulator())
{
if(true)// toggle between true and false to enable simulator use mds
{
// Log.i(TAG, "Device is a simulator and USE_MDS_IN_SIMULATOR is true");
connectionString = ";deviceside=false";
}
else
{
// Log.i(TAG, "Device is a simulator and USE_MDS_IN_SIMULATOR is false");
connectionString = ";deviceside=true";
}
}
// Wifi is the preferred transmission method
else if(WLANInfo.getWLANState() == WLANInfo.WLAN_STATE_CONNECTED)
{
// Log.i(TAG, "Device is connected via Wifi.");
connectionString = ";interface=wifi";
}
// Is the carrier network the only way to connect?
else if((CoverageInfo.getCoverageStatus() & CoverageInfo.COVERAGE_DIRECT) == CoverageInfo.COVERAGE_DIRECT)
{
// Log.i(TAG, "Carrier coverage.");
String carrierUid = getCarrierBIBSUid();
if(carrierUid == null)
{
// Has carrier coverage, but not BIBS. So use the carrier's TCP network
// Log.i(TAG, "No Uid");
connectionString = ";deviceside=true";
}
else
{
// otherwise, use the Uid to construct a valid carrier BIBS request
// Log.i(TAG, "uid is: " + carrierUid);
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)
{
// Log.i(TAG, "MDS coverage found");
connectionString = ";deviceside=false";
}
// If there is no connection available abort to avoid bugging the user unnecssarily.
else if(CoverageInfo.getCoverageStatus() == CoverageInfo.COVERAGE_NONE)
{
// Log.i(TAG, "There is no available connection.");
}
// In theory, all bases are covered so this shouldn't be reachable.
else
{
// Log.i(TAG, "no other options found, assuming device.");
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. This is called from getConnectionString()
*/
private 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;
}
Пример использования можно найти ниже:
String url = "specific url"+getConnectionString();
HttpConnection connection = (HttpConnection)Connector.open( url, Connector.READ, true );
Этот метод является полным доказательством и был протестирован на нескольких устройствах, поэтому я решил представить его в кратком формате здесь.
Удачи