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
New Contributor
hey_nold
Posts: 8
Registered: ‎12-20-2011
My Device: 9700

Scanning wifi network in app

I think i have read dozens of threads discussing about using HotSpotClient to scan wifi network in app.

The best solution so far is http://supportforums.blackberry.com/t5/Java-Development/How-to-scan-for-available-networks-for-devic....

 

Here is the code :

public class HotspotCredentialsAgentEnabler extends HotspotCredentialsAgent {
        
        private String _userName;
        private String _password;
        private int _crAgent_Preference;
        
        
        public int getCredentialsControlPreference() {
            
            return _crAgent_Preference;
            
        }
        public String getPassword() {
            
            return _password;
            
        }
        public String getUsername() {
            
            return _userName;
            
        }
        protected  void setCredentialsControlPreference(int control) {
            
            this._crAgent_Preference = control;
        }
        public void setPassword(String password) {
            
            this._password = password;
            
        }
        public void setUsername(String userName) {
            
            this._userName = userName;
                    
        }
        public boolean validateCredentials(String username, String password) {
            //for cross verification
            return true;
        }
        
    }
     


    public class HotspotClientEnabler extends HotspotClient {

        private StringBuffer wLanInfoBuff;
         
        public HotspotClientEnabler(HotspotCredentialsAgent hotspotCrAgent, HotSpotAuthAgentEnabler hotSpotAuthAgent, int supportednetworkTypes) {

            super(hotspotCrAgent, hotSpotAuthAgent, supportednetworkTypes);
            
        }
        public String getClientName() {      
            return "Any Name";      
        }
        
        public  HotspotInfo[] getSupportedNetworks(WLANAPInfo[] networks) {            
            // here u can read the WLANInfo.WLANAPInfo[], and try to get the open n/w, the one which you want to connect
            // and return this as a first element of the HotspotInfo array
            // you can read all properties using getHotSpotInfo(...) and can set additional properties to
            // the HotspotInfo
        	String text = "Called \n";
			HotspotInfo[] hotspotInfo = new HotspotInfo[networks.length];
			for(int i = 0; i<networks.length; i++)
			{
				text += networks[i].getBSSID() + "\n";
				text += networks[i].getSignalLevel() + "\n";
				text += networks[i].toString() + "\n ";
				hotspotInfo[i]=new HotspotInfo(networks[i], false);
			}
			
			final String shownText = text;
			UiApplication.getUiApplication().invokeLater(new Runnable() {
				public void run() {
					resultText.setText(shownText);
				}
			});
			return hotspotInfo; //return only open networks        
        }
        

        public void getHotSpotInfo(WLANAPInfo wLanInfo) {
            wLanInfoBuff = new StringBuffer();
            String ssid = "";
            if(wLanInfo != null) {
                wLanInfoBuff.append("APChannel*****"+wLanInfo.getAPChannel());
                wLanInfoBuff.append("**BSSID*****"+wLanInfo.getBSSID());
                wLanInfoBuff.append("**DataRate*****"+wLanInfo.getDataRate());
                wLanInfoBuff.append("**Profile Name*****"+wLanInfo.getProfileName());
                wLanInfoBuff.append("**RadioBand*****"+wLanInfo.getRadioBand());
                wLanInfoBuff.append("**SecurityCategory*****"+wLanInfo.getSecurityCategory());
                wLanInfoBuff.append("**SignalLevel*****"+wLanInfo.getSignalLevel());
                ssid = wLanInfo.getSSID();
                wLanInfoBuff.append("**SSID*****"+ssid.toString());    
            }   
        }
        
    }

public class HotSpotAuthAgentEnabler extends HotspotAuthenticationAgent {
        
        public void login(HotspotInfo hotspot) {
           
        }
        public void logout(HotspotInfo hotspot) {
           
        }
        public void cancelLogin(HotspotInfo hotspot) {
           
        }
        public int getSessionState() {
          
            int sessionState = this.getSessionState();
            return sessionState;
        }
        
        public void probeNetwork(HotspotInfo hotspot){
           
        }
    }

//In screen constructor :
try {

            HotspotCredentialsAgentEnabler hotSpotCredentialsAgent = new HotspotCredentialsAgentEnabler();
            hotSpotCredentialsAgent
                    .setCredentialsControlPreference(HotspotCredentialsAgentEnabler.PREFERENCE_DEFAULT);
           

            HotspotStatusListener listener = new HotspotStatusListener() {
				
				public void updateStatus(AuthenticationStatusEvent event) {
					System.out.print(event.getStatus()+"/n");
				}
			};
			
            HotSpotAuthAgentEnabler hotSpotAuthAgentEnabler = new HotSpotAuthAgentEnabler();
            hotSpotAuthAgentEnabler.addListener(listener);

           
             hotspotClientEnabler = new HotspotClientEnabler(HotspotCredentialsAgent.getSystemHotspotCredentialsAgent(),
             hotSpotAuthAgentEnabler, HotspotClient.NETWORK_TYPE_MANUAL );

            HotspotClientRegistry.add(hotspotClientEnabler,
                    ApplicationDescriptor.currentApplicationDescriptor());
            
        } catch (HotspotException hotspotExc) {
            hotspotExc.printStackTrace();
        }

 

The code works very nice in simulator. However, in device, this become so complicated.

 

First time I run the app, i don't see any wifi network is listed. I need to put my test app in background and went to Manage Connection->setup wifi network.

 

There I see prompt for the registration. So I need to allow it register and it brings me to the screen that has the "Scan for network", "manually Add Network" ... buttons.

 

So I pressed "Scan for Network" and after a few seconds I hit break point in my getSupportedNetworks() and WLANInfo.WLANAPInfo[] networks has 5 networks.

 

My question is :

Is there a way to bypass the approval dialog?

Is there a way to force a wifi scan programmatically without user interaction?

Or is there any way to get list of available open network without using this HotspotClient class? I just need these list, no need to connect to anything.

 

Please use plain text.
New Contributor
hey_nold
Posts: 8
Registered: ‎12-20-2011
My Device: 9700

Re: Scanning wifi network in app

Anybody can help me? Maybe my post is too long.

Let's make it clear.

 

All i want to do is  to get list of available wifi network that can be detected.

Until now, i find that there's only 1 way to do that which is using the HotspotClientRegistry (http://www.blackberry.com/developers/docs/5.0.0api/net/rim/device/api/wlan/hotspot/HotspotClientRegi...).

 

However, when it's used on the real device, the user need to put the app on the background and there will be a notification to grant permission through the menu Manage Connection->setup wifi network.

 

Is there any way to avoid this? Or is there any way to get the list of wifi network without using this HotspotClientRegistry?


As a note, when i try to run it on the simulator, i don't need to go to the menu to grant permission and get the wifi list easily.

Please use plain text.
New Contributor
electrocompay
Posts: 2
Registered: ‎08-05-2012
My Device: 8520

Re: Scanning wifi network in app

Hi,

 

This is the solution:

 

HotspotClientRegistry.remove(hotspotClientEnabler);       
HotspotClientRegistry.add(hotspotClientEnabler, ApplicationDescriptor.currentApplicationDescriptor());


This will trigger a new scan.

 

Regards,

A. M.

Please use plain text.