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


Thank you for visiting the BlackBerry Support Community Forums.

BlackBerry will be closing the BlackBerry Support Community Forums Device Forums on April 1st (Developers, see below)

BlackBerry remains committed to providing excellent customer support to our customers. We are delighted to direct you to the CrackBerry Forums, a well-established and thorough support channel, for continued BlackBerry support. Please visit http://forums.crackberry.com or http://crackberry.com/ask. You can also continue to visit BlackBerry Support or the BlackBerry Knowledge Base for official support options available for your BlackBerry Smartphone.

"When we launched CrackBerry.com 10 years ago, we set out to make it a fun and useful destination where BlackBerry Smartphone owners could share their excitement and learn to unleash the full potential of their BlackBerry. A decade later, the CrackBerry community is as active and passionate as ever and I know our knowledgeable members and volunteers will be excited to welcome and assist more BlackBerry owners with their questions."

- Kevin Michaluk, Founder, CrackBerry.com

Developers, for more information about the BlackBerry Developer Community please review Join the Conversation on the BlackBerry Developer Community Forums found on Inside BlackBerry.


Reply
New Developer
Posts: 10
Registered: ‎03-09-2009
My Device: Not Specified

How to detect weither the MDS service is available

Hello, I've been working on blackberry app that connects to a server application through sockets. If everything is online, it will work fine, but now I got to prevent the app from freezing when the MDS service is not online. I'm using a class that extends Thread for all communication. I have been able to handle it device side when the server application it needs to connect to is not running(as that will generate an IOException, which I catch with a try catch in the run method of my connection class). When trying to read something from an outputstream while the MDS server is not running, the app will freeze. Therefore, I want to check before reading or writing anything weither the MDS server is up or not, so I can stop the class from trying to read and freeze.

 

To get this done, I tried to use the CoverageInfo.getCoverageStatus(), in the connection thread, right after establishing a socket connection, but before doing any write or reads to the input and outputstreams of the connection. My problem with this is that I expect the method to return COVERAGE_MDS or COVERAGE_NONE, so I can act upon that. However, no matter weither I have my MDS server running or not, it will always return COVERAGE_DIRECT.

 

Any idea or example as to what I should use to check weither the MDS server is available for my application?

New Developer
Posts: 10
Registered: ‎03-09-2009
My Device: Not Specified

Re: How to detect weither the MDS service is available

Anyone? I could really use the help, I just can't figure it out why it doesn't work...

Developer
Posts: 4,764
Registered: ‎07-21-2008
My Device: Not Specified

Re: How to detect weither the MDS service is available

Those are bit-flags - so you have to 'AND' them with the result to determine if you have coverage.

 

Have you tried:

 

 CoverageInfo.isCoverageSufficient(CoverageInfo.COVERAGE_MDS)

 

 

 

 

Highlighted
Developer
Posts: 174
Registered: ‎08-20-2008
My Device: Not Specified

Re: How to detect weither the MDS service is available

[ Edited ]

The problem you may have is that you may go out of coverage at any point and the user will have to wait the default 2 minutes for the connection to timeout also you may not have anything in place to stop the user making the same request time after time causing multiple threads to be started.

 

I personaly use a pop window with a time gauge on it as the code below. This also monitors for coverage change.

 

 

import java.lang.*;
import net.rim.device.api.ui.component.*;
import net.rim.device.api.ui.*;
import net.rim.device.api.ui.container.*;
import net.rim.device.api.system.*;

/**
*
*/


public class WSConnectMonitor extends Thread implements CoverageStatusListener
{
private WSConnectMonitorInterface wsConnectMonitorInterface;
private int timeOut; // in 10th of a second
private PopupScreen waitScreen;
private GaugeField gaugeField;
private boolean interrupted;
private int runLaterEventID;
public WSConnectMonitor(WSConnectMonitorInterface wsConnectMonitorInterface)
{
this.wsConnectMonitorInterface = wsConnectMonitorInterface;
this.interrupted = false;
this.timeOut = 350;
}
public WSConnectMonitor(WSConnectMonitorInterface wsConnectMonitorInterface, int timeOut)
{
this.wsConnectMonitorInterface = wsConnectMonitorInterface;
this.interrupted = false;
this.timeOut = timeOut;
}
public void interrupt() // This thread is interrupted when a timeout DOSE NOT occur or device went out of coverage.
{ // The thread is interrupted by a connection class
closeWaitScreen();
System.out.println("WS connection did not time out or device went out of coverage");
this.interrupted = true;
return;
}
public void run()
{
CoverageInfo.addListener(this);
displayWaitScreen(); // display the waitscreen with the progress bar.
int i=0;
for (i=0; i!=timeOut; i++)
{
try {
this.sleep(100);
gaugeField.setValue(i+1); // add a little to the progress bar, base can't be zero.
} catch (java.lang.InterruptedException e) {
break;
}
if (this.interrupted) { // check if thread has been interrupted
break;
}
}
closeWaitScreen();
if (this.wsConnectMonitorInterface.isAlive() && !this.interrupted) {
this.wsConnectMonitorInterface.interrupt();
System.out.println("WS connection took too long, timing out now");
this.wsConnectMonitorInterface.connectionTimeOut();
}
return;
}
protected void displayWaitScreen() // Creates a popscreen with a progress bar.
{
DialogFieldManager manager = new DialogFieldManager();
waitScreen = new PopupScreen(manager, PopupScreen.DEFAULT_CLOSE){
public void close()
{
// User has hit escape so close the wait screen and cancel the request.
UiApplication.getUiApplication().invokeLater(new Runnable() {
public void run() {
if (wsConnectMonitorInterface.isAlive()) {
wsConnectMonitorInterface.interrupt();
}
closeWaitScreen();
wsConnectMonitorInterface.requestCancelled();
}
});
}
};
gaugeField = new GaugeField(null, 1, timeOut+1, 1, GaugeField.NO_TEXT);
manager.addCustomField(new LabelField("Please wait..."));
manager.addCustomField(gaugeField);
synchronized(UiApplication.getEventLock()) {
this.runLaterEventID = UiApplication.getUiApplication().invokeLater(new Runnable() {
public void run() {
UiApplication.getUiApplication().pushScreen(waitScreen);
}
}, 1, false);
}
}
protected void closeWaitScreen() // Close the wait screen.
{
// Prevent memory leaks, remove the listener
CoverageInfo.removeListener(this); // This listener is added in the run method

if (waitScreen == null) {
return;
}
UiApplication.getUiApplication().cancelInvokeLater(this.runLaterEventID); // Cancel the push of the wait screen if it already hasn't happened.
if (waitScreen.isDisplayed()) {
UiApplication.getUiApplication().invokeLater(new Runnable() {
public void run() {
if (waitScreen.isDisplayed()) {
UiApplication.getUiApplication().popScreen(waitScreen);
}
}
});
}
}
public void coverageStatusChanged(int newCoverage)
{
if (CoverageInfo.isCoverageSufficient(CoverageInfo.COVERAGE_MDS)) {
System.out.println("In MDS coverage area");
} else {
System.out.println("Out of MDS coverage area");
if (this.wsConnectMonitorInterface.isAlive() && this.wsConnectMonitorInterface.isStreamConnectionOpen()) {
this.wsConnectMonitorInterface.interrupt();
this.wsConnectMonitorInterface.outOfCoverage();
}
if (this.isAlive()) {
this.interrupt();
}
}
}
}

 

 And the interface

 

 

public interface WSConnectMonitorInterface
{
public abstract boolean isAlive();
public abstract void interrupt();
public abstract void outOfCoverage();
public abstract void connectionTimeOut();
public abstract void requestCancelled();
public abstract boolean isStreamConnectionOpen();
}

 

 So your connection class should look something like this....

 

 

class WSConnection extends Thread implements WSConnectMonitorInterface
{
private WSConnectMonitor wsConnectMonitor;
private boolean isConnectionOpen;
WSConnection()
{
// Prep some parameters here for your connection
}
public void run()
{
if (!CoverageInfo.isCoverageSufficient(CoverageInfo.COVERAGE_MDS) && !DeviceInfo.isSimulator()) {
noCover();
return;
}
this.wsConnectMonitor = new WSConnectMonitor(this);
this.wsConnectMonitor.start();
this.isConnectionOpen = true;
// Start your connection as normal

// At the end
this.isConnectionOpen = false;
this.wsConnectMonitor.interrupt();
}
protected void noCover()
{
UiApplication.getUiApplication().invokeLater(new Runnable() // Run in UI thread when this thread is complete
{
public void run()
{
Dialog.alert("No coverage found.");
}
});
}
public void interrupt() // This thread is interrupted when a timeout occurs or the user cancels.
{
if (this.isConnectionOpen) {
try {
streamConnection.close();
} catch (java.io.IOException e) {
System.err.println("Error on closing connection on timeout " + e);
connectionError("Error on closing connection on timeout " + e);
}
}
return;
}
public void outOfCoverage()
{
// Invoked by monitor
UiApplication.getUiApplication().invokeLater(new Runnable() // Run in UI thread when this thread is complete
{
public void run()
{
Dialog.alert("You are out of signal coverage area.");
}
});
}
public void connectionTimeOut()
{
// Invoked by Monitor
UiApplication.getUiApplication().invokeLater(new Runnable() // Run in UI thread when this thread is complete
{
public void run()
{
Dialog.alert("Error: Connection timed out.");
}
});
}
public void requestCancelled()
{
// Invoked by Monitor
UiApplication.getUiApplication().invokeLater(new Runnable() // Run in UI thread when this thread is complete
{
public void run()
{
Dialog.inform("Request cancelled");
}
});
}
public boolean isStreamConnectionOpen()
{
// Invoked by Monitor
return this.isConnectionOpen;
}
}

 

 If the user closes this popuop window it cancels the request or times out if going on to long. You may want to tweak your time out using the alternative constructor if need be.

 

 

 

 

Message Edited by BeMor on 05-27-2009 09:28 PM
New Developer
Posts: 10
Registered: ‎03-09-2009
My Device: Not Specified

Re: How to detect weither the MDS service is available

First of all, thank you both for your replies.

 

to RexDoug: I assumed they used such a principle, yes. I tried the method with the MDS simulator and a blackberry simulator, calling the method on various points in the code(before calling the connection thread, in the constructor of the connection thread, in the run() method of the connection thread right after a connection is opened, and finally after the thread is done) In all of these cases, it returned false, with and without the MDS simulator turned on. Maybe using the MDS simulator is the problem? Does CoverageInfo work for the MDS simulator as well? I can't test this right now,as I don't have a BlackBerry available for that at this moment.

 

Thanks for the big code example, Bemor. The problem might indeed lie in the points you mentioned. I shall take an extensive look at your code later, compare it with my own and see if the solution can be found with this.

Developer
Posts: 17,026
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Re: How to detect weither the MDS service is available

coverage_mds does not work on the simulator. maybe if it is enterprise activated, but i never tried.
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Developer
Posts: 174
Registered: ‎08-20-2008
My Device: Not Specified

Re: How to detect weither the MDS service is available

Yeah it does work if it's enterprise activated but its a pain as you need to simulate a usb connection and have either Desktop Manager or Blackberry Manager running to make the connection not to mention the task of getting it back up and working with MDS sim again once you've finished. I tend to just get round this by checking to see if the device is a simulator and ignore this check if it is as per the code shown. Hope this helps.
New Developer
Posts: 10
Registered: ‎03-09-2009
My Device: Not Specified

Re: How to detect weither the MDS service is available

Here's an update of this: I tested this function today on a BlackBerry with a simple application that simply checks the MDS coverage, and displays weither it's true or false(This is a blackberry behind a BES server) I tried it out, and the function indeed gave back a true on the MDS coverage part. I then asked the System admin to close the MDS service for a few minutes, to see if the function would return false. This was not the case. He even went as far as to close of all the services for the BlackBerry Router, meaning that a BlackBerry shouldn't be able to connect through it at all. Still, the method keeps giving back true. The only moment that this method gives back a false is right after the BlackBerry has started, and hasn't made any kind of connection at all...Does anyone have any item why it's doing this? 
Developer
Posts: 174
Registered: ‎08-20-2008
My Device: Not Specified

Re: How to detect weither the MDS service is available

[ Edited ]
I think even if all the services are closed you would still get an MDS coverage coming back as positive. I think this may be due to your provider confirming the connection is a BES one rather than your physical server. You normally get a 503 http connection error or something along those lines if the user has MDS disable on their account. I have used this method on a number of applications with great success. I think the coverage check is literally just to check for coverage on a device not if the service is up on the BES. You may want to perform your test in a black coverage spot. If you can't find one I have placed my device in a microwave oven (don't switch it on Smiley Wink ) to perform this test in the past with some success.
Message Edited by BeMor on 05-29-2009 12:34 PM
New Developer
Posts: 10
Registered: ‎03-09-2009
My Device: Not Specified

Re: How to detect weither the MDS service is available

Ah, I see, so that's how it works.

 

You've helped so much already, but I was wondering if you could give an example of this 503 error check. As really, my goal is to somehow check weither the MDS is there for me to use before communication with an application behind it is done. I thought that the Coverage MDS function would do exactly that, but as you said, it only confirms that the connection is BES compatible, not weither it can actually connect to it or not.