Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Contributor
raviteja2007
Posts: 15
Registered: ‎11-13-2011
My Device: Blackberry Curve 9300

Best Network connection code to be used in a blackberry app, supporting OS >= 5.0

I tried two versions of the network connection code for my blackberry app. The code in the first version doesn't work on some carriers, but the response time is very good for the carriers it work. This code uses a connection string method.

 

The code in the second version works but the response time has increased by 40% for all carriers, which is not good. This code uses Connection Factory, which is an api provided by RIM for OS > 5.

 

************   First Version ***************************

 

     /**
     * Determines what connection type to use and returns the necessary string to use it.
     * @return A string with the connection info
     */
    public 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(Constants.USE_MDS_IN_SIMULATOR)
                {
                        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.");
            if (screen != null)
                        screen.requestFailed(Constants.NO_NETWORK_CONNECTION);
            // alert the user about no network 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.
     */
    private 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;
    }
}

 **** Second Version **********************

 

The second version has a function which returns the HTTP Connection directly.

 

  public static HttpConnection getConnectionForRequest(String url) 
    {
        /*final int[] availableTransportTypes = 
        TransportInfo.getAvailableTransportTypes();*/
        // make a list of transport types ordered according to preference (they will be tried in succession)
        int[] availableTransportTypes = {TransportInfo.TRANSPORT_BIS_B,
                                         TransportInfo.TRANSPORT_TCP_WIFI, 
                                         TransportInfo.TRANSPORT_MDS,
                                         TransportInfo.TRANSPORT_TCP_CELLULAR, 
                                         TransportInfo.TRANSPORT_WAP2, 
                                         TransportInfo.TRANSPORT_WAP};
        final ConnectionFactory connectionFactory = new ConnectionFactory();
        
        // Remove any transports that are not currently available.
        for (int i = 0; i < availableTransportTypes.length; i++) 
        {
            int transport = availableTransportTypes[i];
            if (!TransportInfo.isTransportTypeAvailable(transport) || !TransportInfo.hasSufficientCoverage(transport)) 
            {
                Arrays.removeAt(availableTransportTypes, i);
            }
        }
                
        connectionFactory.setPreferredTransportTypes(availableTransportTypes);
        
        //connectionFactory.setConnectionMode(ConnectionFactory.ACCESS_READ);
        final ConnectionDescriptor connectionDescriptor = connectionFactory.getConnection(url);
        HttpConnection connection = null;
        if (connectionDescriptor != null) 
        {
            // connection suceeded
            int transportUsed = connectionDescriptor.getTransportDescriptor().getTransportType();
            Log.i(TAG, "Transport type used: " + Integer.toString(transportUsed));
            connection = (HttpConnection) connectionDescriptor.getConnection();
        }
        return connection;
    }

 My application is in production, and any help is highly appreciated.

 

Thanks,

Ravi

Developer
shism2
Posts: 246
Registered: ‎12-13-2010
My Device: Not Specified

Re: Best Network connection code to be used in a blackberry app, supporting OS >= 5.0

If you use the connectionFactory you might run into issues with BIS-B and cookies.

Developer
peter_strange
Posts: 19,610
Registered: ‎07-14-2008
My Device: Not Specified

Re: Best Network connection code to be used in a blackberry app, supporting OS >= 5.0

I suspect the extra time is caused by ConnectionFactory checking for methods that fail.  To reduce that time, I would determine which method was actually used, and prioritise that in your availableTransportTypes for next time.

Contributor
raviteja2007
Posts: 15
Registered: ‎11-13-2011
My Device: Blackberry Curve 9300

Re: Best Network connection code to be used in a blackberry app, supporting OS >= 5.0

Hi Peter,

 

We have done it in the similar way you mentioned.

 

Even then, the response time is 860 ms, where as it is 4 ms with the old connection string method. But the problem with the connection string method is that it's not working with all carriers.

 

 It works this way:
1) On first request application detects available transport in order defined in code:
TransportInfo.TRANSPORT_BIS_B,
TransportInfo.TRANSPORT_TCP_WIFI,
TransportInfo.TRANSPORT_MDS,
TransportInfo.TRANSPORT_WAP2,
TransportInfo.TRANSPORT_TCP_CELLULAR, 
TransportInfo.TRANSPORT_WAP
2) All inavailable transports will removed from this list: 
TransportInfo.TRANSPORT_BIS_B,
TransportInfo.TRANSPORT_TCP_WIFI,
---------------------------------------------------
TransportInfo.TRANSPORT_WAP2,
--------------------------------------------------- , 
---------------------------------------------------
3) on next requests application will use these available transports in same order:
TransportInfo.TRANSPORT_BIS_B
TransportInfo.TRANSPORT_TCP_WIFI,
TransportInfo.TRANSPORT_WAP2
I think that ConnectionFactory function (that opens connection) works slow and it not depends on transport type. Because on next calls it takes first available transport from array.
 And it takes 860 ms to open connection with available transport anyway.
Peter, let me know what you think.
Developer
peter_strange
Posts: 19,610
Registered: ‎07-14-2008
My Device: Not Specified

Re: Best Network connection code to be used in a blackberry app, supporting OS >= 5.0

I'm suggesting a slight change:

 

2a) when the connection is made, the processing determines which connection mode was actually used, and moves it to be first in the list.

3) on next requests application will use these available transports.  But the first one in the list will be the one last used, so if WIFI was used the orer would be

TransportInfo.TRANSPORT_TCP_WIFI,
TransportInfo.TRANSPORT_BIS_B
TransportInfo.TRANSPORT_WAP2
3a) When the connection is made, the processing determines which connection mode was actually used, and moves it to be first in the list, this process repeats, so the first one tried is always the last one used. 
 
Make sense?