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
Developer
Posts: 144
Registered: ‎12-20-2008
My Device: Not Specified

Connection Methods depending signaL strength

I use in all the conection this aproach in order to set the proper method is there a way to select the best one sometimes it works but having the same connections edge data voice and signal other times is does not but internet works some ideas here is the code i use to select he best way for speed

initializeTransportAvailability();

if (ElRecreo.srWAP2 != null && ElRecreo.coverageWAP2) {
                                ElRecreo.baseURL = _Url;
                                _UrlBuscar = getWAP2URL();
                                _tipoConexion = "WAP2";
                        }
                        if (ElRecreo.TCPApn != null && ElRecreo.coverageTCP) {

                                ElRecreo.baseURL = _Url;
                                _UrlBuscar = getTCPurl();
                                _tipoConexion = "TCP";
                        }
                        boolean Wifi = ElRecreo.isWifiActive();
                        System.err.println(_Url + " " + ElRecreo.isWifiActive() + Wifi);
                        Wifi = isWifiActive1();

                        if (ElRecreo.srWiFi != null && ElRecreo.coverageWiFi && Wifi) {

                                ElRecreo.baseURL = _Url;
                                _UrlBuscar = getWifiUrl();
                                _tipoConexion = "WIFI";

                        }

 

tks in advance and happy new year

r.Soler

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

Re: Connection Methods depending signal strength

What is ElRecreo?

 

Remember Internet will probably use BIS-B or BES, so it can connect to that gateway when you might not have the Direct TCP, WAP 2 or WiFi gateways present.

 

I'm pretty sure that the same wireless signal is used for BIS-B, WAP and Direct TCP, so the signal strength will effect all these methods the same.

Developer
Posts: 144
Registered: ‎12-20-2008
My Device: Not Specified

Re: Connection Methods depending signal strength

ElRecreo is the main application it will be the same that  app.Url

tks

Developer
Posts: 144
Registered: ‎12-20-2008
My Device: Not Specified

Re: Connection Methods depending signal strength

What i am trying to do is see all year production in one table 2209 2010 of several trucks here is the complete code i use still having some issues

 

/**
 * Spreadsheet
 *
 */
package ElRecreo;

import net.rim.device.api.ui.*;
import net.rim.device.api.ui.component.*;
import net.rim.device.api.ui.container.*;
import net.rim.device.api.system.*;
import java.io.*;
import java.util.*;
import net.rim.device.api.servicebook.ServiceBook;
import net.rim.device.api.servicebook.ServiceRecord;
import net.rim.device.api.synchronization.ConverterUtilities;
import net.rim.device.api.system.CoverageInfo;
import net.rim.device.api.system.WLANInfo;
import net.rim.device.api.util.DataBuffer;
import net.rim.device.api.system.RadioStatusListener;
import net.rim.device.api.system.RadioInfo;
import javax.microedition.io.HttpConnection;
import javax.microedition.io.StreamConnection;
import net.rim.device.api.util.DataBuffer;
import net.rim.device.api.io.*;
import javax.microedition.io.*;
import javax.microedition.io.file.FileConnection;
import javax.microedition.io.HttpConnection;
import javax.microedition.io.StreamConnection;
import net.rim.device.api.util.DataBuffer;
import java.io.IOException;

public final class SpreadSheet extends MainScreen {
        public String _Url;
        public String _tipoConexion;
        public String resultado;

        public SpreadSheet() {

                initializeTransportAvailability();

                HorizontalFieldManager columnHeadingHolder = new HorizontalFieldManager(
                                HorizontalFieldManager.NO_HORIZONTAL_SCROLL
                                                | HorizontalFieldManager.NO_VERTICAL_SCROLL
                                                | HorizontalFieldManager.USE_ALL_WIDTH);

                StringBuffer sb = new StringBuffer();

                String titulo[] = new String[3];
                String DatosPlaca[] = new String[21];
                String DatosAñoAnt[] = new String[21];
                String DatosAñoAct[] = new String[21];
                titulo[0] = "    Placa";
                titulo[1] = "        2009";
                titulo[2] = "        2010";
                DatosPlaca[1] = "17H-DBB";
                DatosPlaca[2] = "67W-DAY";
                DatosPlaca[3] = "68W-DAY";
                DatosPlaca[4] = "33Y-DAT";
                DatosPlaca[5] = "460-DAY";
                DatosPlaca[6] = "95B-DBB";
                DatosPlaca[7] = "19E-NAI";
                DatosPlaca[8] = "A29AG0D";
                DatosPlaca[9] = "A29AG1D";
                DatosPlaca[10] = "A29AG2D";
                DatosPlaca[11] = "A29AG3D";
                DatosPlaca[12] = "A35AG1D";
                resultado = null;
                for (int c = 1; c < 24; c++) {
                        if (c < 13) {
                                _Url = " http://coopelrecreo.com/choferes/PlacaViajesTotal.php?campo=cdpla&buscar="
                                                + DatosPlaca[c] + "&ano=2009";
                                System.err.println(DatosPlaca[c]);
                        } else {
                                _Url = " http://coopelrecreo.com/choferes/PlacaViajesTotal.php?campo=cdpla&buscar="
                                                + DatosPlaca[c - 12] + "&ano=2010";
                                System.err.println(DatosPlaca[c - 12]);
                        }

                        System.err.println(_Url);
                        MyThreadRead leer = new MyThreadRead(_Url);
                        leer.start();
                        int i = 0;
                        try {
                                while (resultado == null && i < 100) {
                                        leer.sleep(100);
                                        i++;
                                }
                                if (c < 13) {
                                        DatosAñoAnt[c] = resultado;
                                } else {
                                        DatosAñoAct[c-12] = resultado;
                                }
                                resultado = null;
                        } catch (IllegalMonitorStateException e) {
                                System.err.println("Running() :" + e);
                        } catch (InterruptedException e) {
                                System.err.println("Running() :" + e);
                        }

                }

                LabelField headings[] = new LabelField[2];

                for (int count = 0; count < 2; ++count) {

                        headings[count] = new LabelField(titulo[count],
                                        LabelField.NON_FOCUSABLE) {

                                public void paint(Graphics graphics) {

                                        graphics.setColor(Color.RED);

                                        super.paint(graphics);
                                }
                        };

                        columnHeadingHolder.add(headings[count]);
                }

                add(columnHeadingHolder);

                VerticalFieldManager dataScroller = new VerticalFieldManager(
                                VerticalFieldManager.VERTICAL_SCROLL
                                                | VerticalFieldManager.VERTICAL_SCROLLBAR);

                HorizontalFieldManager rowHolder = new HorizontalFieldManager(
                                HorizontalFieldManager.NO_VERTICAL_SCROLL
                                                | HorizontalFieldManager.NO_HORIZONTAL_SCROLL);

                VerticalFieldManager rows[] = new VerticalFieldManager[2];
                for (int count = 0; count < 2; ++count) {
                        rows[count] = new VerticalFieldManager(
                                        VerticalFieldManager.NO_HORIZONTAL_SCROLL
                                                        | VerticalFieldManager.NO_VERTICAL_SCROLL);
                       for (int rowCount =1; rowCount < 13; ++rowCount) {
                                if (count == 0) {
                                        rows[count].add(new LabelField(DatosPlaca[rowCount],
                                                        LabelField.FOCUSABLE));
                                }
                                if (count == 1) {
                                        rows[count].add(new LabelField(DatosAñoAnt[rowCount],
                                                        LabelField.FOCUSABLE));
                                }
                                if (count == 2) {
                                        rows[count].add(new LabelField(DatosAñoAct[rowCount],
                                                        LabelField.FOCUSABLE));
                                }
                        }

                        rowHolder.add(rows[count]);

                }

                dataScroller.add(rowHolder);

                add(dataScroller);

        }

        public static String getWifiUrl() {
                return ElRecreo.baseURL + ";deviceside=true;interface=wifi";

        }

        public static String getWAP2URL() {
                return ElRecreo.baseURL + ";deviceside=true" + ";ConnectionUID="
                                + ElRecreo.srWAP2.getUid();
        }

        public static String getTCPurl() {

                String url = ElRecreo.baseURL + ";deviceside=true";

                url += ";apn=" + ElRecreo.TCPApn;

                url += ";TunnelAuthUsername=" + ElRecreo.TCPUser;

                url += ";TunnelAuthPassword=" + ElRecreo.TCPPass;
                return url;
        }

        public void initializeTransportAvailability() {
                ServiceBook sb = ServiceBook.getSB();
                ServiceRecord[] records = sb.getRecords();

                for (int i = 0; i < records.length; i++) {
                        ServiceRecord myRecord = records[i];
                        String cid, uid;

                        if (myRecord.isValid() && !myRecord.isDisabled()) {
                                cid = myRecord.getCid().toLowerCase();
                                uid = myRecord.getUid().toLowerCase();
                                // BIS
                                if (cid.indexOf("ippp") != -1 && uid.indexOf("gpmds") != -1) {
                                        ElRecreo.srBIS = myRecord;
                                }
                                // WAP1.0: Not implemented.

                                // BES
                                if (cid.indexOf("ippp") != -1 && uid.indexOf("gpmds") == -1) {
                                        ElRecreo.srMDS = myRecord;
                                }
                                // WiFi
                                if (cid.indexOf("wptcp") != -1 && uid.indexOf("wifi") != -1) {
                                        ElRecreo.srWiFi = myRecord;
                                }
                                // Wap2
                                if (cid.indexOf("wptcp") != -1 && uid.indexOf("wap2") != -1) {
                                        ElRecreo.srWAP2 = myRecord;

                                }
                                // Unite
                                if (getConfigType(myRecord) == ElRecreo.CONFIG_TYPE_BES
                                                && myRecord.getName().equals(ElRecreo.UNITE_NAME)) {
                                        ElRecreo.srUnite = myRecord;
                                }
                        }
                }
                if (CoverageInfo.isCoverageSufficient(CoverageInfo.COVERAGE_BIS_B)) {
                        ElRecreo.coverageBIS = true;
                                        }
                if (CoverageInfo.isCoverageSufficient(CoverageInfo.COVERAGE_DIRECT)) {
                        ElRecreo.coverageTCP = true;

                        ElRecreo.coverageWAP = true;

                        ElRecreo.coverageWAP2 = true;

                }
                if (CoverageInfo.isCoverageSufficient(CoverageInfo.COVERAGE_MDS)) {
                        ElRecreo.coverageMDS = true;

                        ElRecreo.coverageUnite = true;

                }

                if (WLANInfo.getWLANState() == WLANInfo.WLAN_STATE_CONNECTED) {
                        ElRecreo.coverageWiFi = true;

                }

        }

        private DataBuffer getDataBuffer(ServiceRecord record, int type) {
                byte[] data = record.getApplicationData();
                if (data != null) {
                        DataBuffer buffer = new DataBuffer(data, 0, data.length, true);
                        try {
                                buffer.readByte();
                        } catch (EOFException e1) {
                                return null;
                        }
                        if (ConverterUtilities.findType(buffer, type)) {
                                return buffer;
                        }
                }
                return null;
        }

        private int getDataInt(ServiceRecord record, int type) {
                DataBuffer buffer = null;
                buffer = getDataBuffer(record, type);

                if (buffer != null) {
                        try {
                                return ConverterUtilities.readInt(buffer);
                        } catch (EOFException e) {
                                return -1;
                        }
                }
                return -1;
        }

        public int getConfigType(ServiceRecord record) {
                return getDataInt(record, 12);
        }

        private String read(InputStream is) throws Throwable {
                String result = "";
                byte[] buffer = new byte[250];
                long cursor = 0;
                int offset = 0;
                try {
                        while ((offset = is.read(buffer)) != -1) {
                                result += new String(buffer, 0, offset);
                                cursor += offset;
                        }
                        is.close();

                } catch (Throwable e) {
                        System.err.println(e.getMessage());
                }
                return result;
        }

        public static boolean isWifiActive1() {
                return CoverageInfo.isCoverageSufficient(CoverageInfo.COVERAGE_CARRIER,
                                RadioInfo.WAF_WLAN, false);
        }

        public class MyThreadRead extends Thread {
                public MyThreadRead(String _Url) {
                        super();
                }

                public void run() {
                        boolean running = true;

                        ElRecreo.isRunning = true;

                        HttpConnection hconn;
                        SocketConnection sconn;
                        String _UrlBuscar = "";

                        _UrlBuscar = _Url;
                        System.err.println(_Url);
                        ElRecreo.baseURL = "";
                        _tipoConexion = "ninguna";

                        if (ElRecreo.srWAP2 != null && ElRecreo.coverageWAP2) {
                                ElRecreo.baseURL = _Url;
                                _UrlBuscar = getWAP2URL();
                                _tipoConexion = "WAP2";
                        }
                        if (ElRecreo.TCPApn != null && ElRecreo.coverageTCP) {

                                ElRecreo.baseURL = _Url;
                                _UrlBuscar = getTCPurl();
                                _tipoConexion = "TCP";
                        }
                        boolean Wifi = ElRecreo.isWifiActive();
                        System.err.println(_Url + " " + ElRecreo.isWifiActive() + Wifi);
                        Wifi = isWifiActive1();

                        if (ElRecreo.srWiFi != null && ElRecreo.coverageWiFi && Wifi) {

                                ElRecreo.baseURL = _Url;
                                _UrlBuscar = getWifiUrl();
                                _tipoConexion = "WIFI";

                        }
                        System.err.println(_UrlBuscar);
                        System.err.println("WAP2 " + ElRecreo.srWAP2 + " waptcp "
                                        + ElRecreo.coverageWAP2 + " covtcp " + ElRecreo.coverageTCP
                                        + " wifi " + Wifi + " srwi " + ElRecreo.srWiFi + " wi "
                                        + ElRecreo.coverageWiFi);
                        setTitle(_tipoConexion + "-" + _UrlBuscar);
                        while (running && _tipoConexion != "ninguna") {

                                try {
                                        hconn = (HttpConnection) Connector.open(_UrlBuscar.trim());
                                        int response = hconn.getResponseCode();
                                        if (response == HttpConnection.HTTP_OK) {
                                                InputStream is = hconn.openInputStream();
                                                resultado = read(is);
                                                System.err.println(resultado);
                                        }
                                        hconn.close();
                                        running = false;

                                } catch (Throwable e) {
                                        System.err.println(e.getMessage());
                                }
                        }
                        this.interrupt();
                }
        }
}

 

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

Re: Connection Methods depending signal strength

Before we start attempting to determine what is wrong with your connection code, we need to address the first problem I suspect you have, and that is, you are blocking the event Thread.

 

I'd guess that you are creating and then pushing this screen from some user action.  So the constructor is running with the Event Thread.  So can you explain what this code is actually trying to do?

 

                        leer.start();
                        int i = 0;
                        try {
                                while (resultado == null && i < 100) {
                                        leer.sleep(100);
                                        i++;
                                }
                                if (c < 13) {
                                        DatosAñoAnt[c] = resultado;
                                } else {
                                        DatosAñoAct[c-12] = resultado;
                                }
                                resultado = null;
                        } catch (IllegalMonitorStateException e) {
                                System.err.println("Running() :" + e);
                        } catch (InterruptedException e) {
                                System.err.println("Running() :" + e);
                        }

 

It seems you are correctly starting the Background Thread to process the URL request, and then waiting for it.  So you are blocking the Event Thread.  You need to process this in another way, by starting the networking Thread, displaying your screen and asking the user to wait for the response.  When it comes,you need to tell the user it is there.  This is a little complicated, and there are lots of forum entries with people who have had problems with doing this.  Search around.  You might find searching for 'progress bar' quite useful - because the most common thing people want to do is display a progress bar while the connection is taking place.

 

Note that the fact that this code runs on the Simulator is not a guarantee that it will run on the device, the response on the Simulator is so fast that the blocking might not be noticed.  In addition, you have attempted to mitigate the problem by only waiting in 100 millisecond lots, however this is not an appropriate way to do this.  Do it correctly now and you will be able to use your networking code in loads of other places. 

 

Developer
Posts: 144
Registered: ‎12-20-2008
My Device: Not Specified

Re: Connection Methods depending signal strength

What i am tryng to do is give some time to execute the request what really should do is interrupting the execution until response is achieved but really i do not know hoew to do it some help with this in my code

tks

r.soler

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

Re: Connection Methods depending signal strength

"what really should do is interrupting the execution until response is achieved"

 

This is not possible when running on the Event Thread in an easy way, Event processing is an asynchronous process, which means that you have to provide this co-ordination yourself. 

 

The last two paragraphs of my last post were intended to provide some help with this.  Unfortunately it is not something that can easily be explained.  I would review the httpdemo included in the JDE for an example that processes the http request on a different Thread and has the UI "waiting" for this Thread to complete.

 

Here are some other links that might help:

 

http://supportforums.blackberry.com/t5/Java-Development/Connecting-your-BlackBerry-http-and-socket-c...

 

http://www.thinkingblackberry.com/archives/182

Developer
Posts: 144
Registered: ‎12-20-2008
My Device: Not Specified

Re: Connection Methods depending signal strength

thanks for your reply unfortunatedly i try to remove the sleep(100) and replace for sleep(1) AND ALL MY HTTP REQUESTS FAILS REALLY I WOULD LIKE THET YOU GIVE ME SOME APPRAOACH IN MY CODE TO DO IT IN A PROPÈR WAY