0

How can we check internet connection is available or not in a blackberry application?

When I send request to my server that time I want to check my device connection availability.

Kara
  • 6,115
  • 16
  • 50
  • 57
Hitarth
  • 1,950
  • 3
  • 27
  • 52
  • Surely the API you're using to send a request to your server returns an error code if your server isn't reachable? – sarnold Oct 22 '11 at 06:59
  • cant we check manually that WLAN/GPRS (Blackberry service) already activated or not ?? – Hitarth Oct 22 '11 at 07:29

3 Answers3

2

Hi u can test your connection by this code.

public  static boolean isConnectionAvailable(String networkName) {
    HttpConnection connection;
    try {
        HttpConnectionFactory httpConnectionFactory = new HttpConnectionFactory("http://m.google.com");
        connection = httpConnectionFactory.makeConnectionUsing(networkName);
        if(connection == null)
            return false;
        else if (connection.getResponseCode() == HttpConnection.HTTP_OK) 
        {
            connection.close();
            return true;
        }
        connection.close();
    } catch( Exception e) {}

    return false;
}
1

you can make use of HttpConnection.requestCode() method to check whether connection is available or not.If it returns 200, means connection exists.Try this, i used this code to check connection, i dont know if there is any other way.

      public void run() {
    try{
        HttpConnection conn = (HttpConnection)Connector.open("http://www.goole.com");

        conn.setRequestMethod(HttpConnection.GET);
        int i = conn.getResponseCode();
        if(conn.getResponseCode()==HttpConnection.HTTP_OK)
        {

            System.out.println("----------------------------------------responsecode-------------------->>>>>>: " + conn.getResponseCode());
            _screen.requestSuccess("Connection Available. ResponseCode:" + i);
            conn.close();
        }
        else
        {
            System.out.println("----------------------------------------responsecode-------------------->>>>>>: " + conn.getResponseCode());
            _screen.requestFailed("Connection Not Available.ResponseCode:" + i);
            conn.close();


        }

    }
sid_sket
  • 81
  • 8
1

You can test if you coverage with something like this:

public static boolean hasSignal() {
    if (RadioInfo.getState() == RadioInfo.STATE_OFF || RadioInfo.getSignalLevel() == RadioInfo.LEVEL_NO_COVERAGE) {
        System.out.println(" -- no signal");
        return false;
    } else {
        return true;
    }
}

If you are using a wifi device you need to check wifi separately (i think)

public static boolean hasWifi() {
    if ( ( RadioInfo.getActiveWAFs() & RadioInfo.WAF_WLAN ) != 0 ) {
        return (CoverageInfo.isCoverageSufficient(CoverageInfo.COVERAGE_DIRECT, RadioInfo.WAF_WLAN, true));
    } else {
        System.out.println(" -- no wifi");
        return false;
    }
}

I use those in my app and they seem to work correctly.

icchanobot
  • 3,323
  • 27
  • 37