Saturday, July 14, 2012

Reading Bytes from InputStream

To develop blackberry application we continues need to reading file and communication to socket server. Today we study simple utility to reading bytes from input stream. Because inputstream use to read data from file as well as socket(TCP) connection.

Here we believe that "in" is Inputstream variable.

1. Read fully inputstream from file:

long dataLength = is.available();// here we get length of readable data on inputstream

byte[] bytes = new byte[(int) dataLength];
is.read(bytes);
String fileData = new String(bytes);
System.out.println(fileData);
This example we can't read socket inputstream. Following example use to read socket inputstream.

2. Read fully inputstream form TCP/Socket connection:

public byte[] getBytesFromInputStream(InputStream is){ 
 try{
 byte[] bytes = null;
  try {
    int dataLength = input.readInt();
    System.out.println("Read Data: "+dataLength);
    bytes = new byte[dataLength];
    int totalRead = 0;
    while(totalRead < dataLength){
      int read = input.read(bytes,totalRead,dataLength-totalRead);
      totalRead+=read;
    }
 System.out.println("Size of Byte Array: "+bytes.length +" Data "+bytes);
 }catch (Exception e) {
    System.out.println(">>>>>>>" + e);
 }
 return bytes;
}
Hope helpfull..

Friday, July 6, 2012

Blackberry Reliable Network Connection

Now a day so many application require to establish network connection In Blackberry there are no simple way to pass only URL and get connection. In blackberry there are more then Five type of connection available. Developer have to confirm which connection available so that can use on application. On Blackberry developer have to pass suffix after URL if device have wifi connection available then "interface=wifi" suffix is use.

Following Type of Connection available:

1. Blackberry Enterprise server using Blackberry Mobile Data System(MSD)

2. Blackberry Internet Service

3. Wireless(WiFi)

4. WAP 1.x

5. WAP 2.0

There are lot of problem when you establish connection on blackberry following example will resolve most of connection problem.


/**


* Determines what connection type to use and returns the necessary string
* to use it.
* @return A string with the connection info
*/

private 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()) {

    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 < records.length; currentRecord++) {
     if (records[currentRecord].getCid().toLowerCase().equals("ippp")) {
      if (records[currentRecord].getName().toLowerCase().indexOf("bibs") >= 0) {
        return records[currentRecord].getUid();

      }

   }

 }

return null;
}

private void logMessage(String msg){
   System.out.println(msg);
}


Help and Source from: http://www.localytics.com/blog/2009/how-to-reliably-establish-a-network-connection-on-any-blackberry-device/