Advertisement
anta40

ConnectionManager

Aug 28th, 2012
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.84 KB | None | 0 0
  1.  
  2. import java.io.IOException;
  3. import javax.microedition.io.Connection;
  4. import javax.microedition.io.Connector;
  5. import net.rim.device.api.system.*;
  6.  
  7. public class ConnectionManager implements GlobalEventListener, CoverageStatusListener {
  8.  
  9.     private static ConnectionManager _manager;
  10.     private boolean _isSimulator;
  11.     private boolean _mdsSupport;
  12.     private boolean _bisSupport;
  13.     private boolean _wapSupport;
  14.     private boolean _wifiSupport;
  15.    
  16.     private ConnectionManager(){
  17.         setCoverage();
  18.     }
  19.  
  20.     public static ConnectionManager getInstance(){
  21.         if (_manager == null) _manager = new ConnectionManager();
  22.         return _manager;
  23.     }
  24.  
  25.     private boolean isWifi() {
  26.         boolean wifi;
  27.         if (WLANInfo.getWLANState() == 4620) wifi = true;
  28.         else wifi = false;
  29.         return wifi;
  30.     }
  31.  
  32.     public Connection getConnection(String name, int mode, boolean timeouts) throws IOException  {
  33.         if(DeviceInfo.isSimulator()) name = name.concat(";deviceside=true;ConnectionTimeout=20000");
  34.         else if(isWifi()) name = name.concat(";deviceside=true;interface=wifi");
  35.         else if(_mdsSupport) name = name.concat(";deviceside=false");
  36.         else if(_bisSupport) name = name.concat(";deviceside=false;ConnectionType=mds-public;EndToEndRequired;ConnectionTimeout=45000");
  37.         else if(!_wapSupport) name = name.concat(";deviceside=true");
  38.        
  39.         return Connector.open(name, mode, timeouts);
  40.     }
  41.  
  42.     public String getConnectionType() {
  43.         if (_mdsSupport) return "MDS";
  44.         if (_bisSupport) return "BIS-B";
  45.         if (_wapSupport) return "WAP";
  46.         else return "Direct TCP";
  47.     }
  48.  
  49.     private void setCoverage(){
  50.         if(CoverageInfo.isCoverageSufficient(2)) _mdsSupport = true;
  51.         if(CoverageInfo.isCoverageSufficient(4)) _bisSupport = true;
  52.     }
  53.  
  54.     public void coverageStatusChanged(int newCoverage) {
  55.         if((newCoverage & 2) == 2) _mdsSupport = true;
  56.         if((newCoverage & 4) == 4) _bisSupport = true;
  57.     }
  58.  }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement