Blackberry connection Connector.open

The task under discussion is opening some connection on Blackberry device, and receive a content of some URL (or post some message).
The OS version that our application should support was 4.5+, so we were not able to use ConnectionFactory class, that is since 5.0.0 and seems to support method mentioned here.

So BlackBerry could be connected to the Internet over the several types:

  1. BIS – BlackBerry Internet Service. setup of this one is in Service Books
  2. MDS(Mobile Data System)/ (BES)(Blackberry Enterprise Server) – data service provided by installed BES. (MDS Activation)
  3. direct TCP – TCP Cellular transport also known as Direct TCP (settings in Advanced Options/TCP)
  4. TCP WiFi – for wifi enabled devices
  5. WAP and WAP2 – WAP connections provided by cellular.

Surprisingly, the Connector.open(URL) method is not able to choose the one of the currently available connection (unlike the ConnectionFactory.getConnection(URL), that is available from 5.0 OS version).
So here is the method that give you an additional string that you have to add to the end of the URL to successfully connect through one of the available channel to internet (it was taken from this place, thanks that guys so much).


/**
* Determines what connection type to use and returns the necessary string to use it.
* @return A string with the connection info
*/
private static String getConnectionString()
{
// This code is based on the connection code developed by Mike Nelson of AccelGolf.
// http://blog.accelgolf.com/2009/05/22/blackberry-cross-carrier-and-cross-network-http-connection
String connectionString = null;

// Simulator behavior is controlled by the USE_MDS_IN_SIMULATOR variable.
if(DeviceInfo.isSimulator())
{
if(UploaderThread.USE_MDS_IN_SIMULATOR)
{
logMessage(“Device is a simulator and USE_MDS_IN_SIMULATOR is true”);
connectionString = “;deviceside=false”;
}
else
{
logMessage(“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)
{
logMessage(“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)
{
logMessage(“Carrier coverage.”);

String carrierUid = getCarrierBIBSUid();
if(carrierUid == null)
{
// Has carrier coverage, but not BIBS. So use the carrier’s TCP network
logMessage(“No Uid”);
connectionString = “;deviceside=true”;
}
else
{
// otherwise, use the Uid to construct a valid carrier BIBS request
logMessage(“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)
{
logMessage(“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)
{
logMessage(“There is no available connection.”);
}

// In theory, all bases are covered so this shouldn’t be reachable.
else
{
logMessage(“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.
*/
private static String getCarrierBIBSUid()
{
ServiceRecord[] records = ServiceBook.getSB().getRecords();
int currentRecord;

for(currentRecord = 0; currentRecord <>= 0)
{
return records[currentRecord].getUid();
}
}
}

return null;
}

So, the Usage is following: Connector.open(URL+getConnectionString());