1

In my android application i am creating(code is shown below) a hot spot(access point) on load of the application for some purpose which has to remain active throughout the life cycle of the application(i know it has some disadvantages), now, how can i make sure that it remains active throughout ? The only idea that i have is to run a timer which checks for method.getName().equals("setWifiApEnabled") status and determines if it is inactive ? is there a better way to get some indication when AP is disabled ?

  IntentFilter filter = new IntentFilter();
    filter.addAction(WifiManager.SUPPLICANT_CONNECTION_CHANGE_ACTION);
    filter.addAction(WifiManager.SUPPLICANT_STATE_CHANGED_ACTION);
    filter.addAction(WifiManager.EXTRA_NEW_STATE);
    filter.addAction(WifiManager.EXTRA_SUPPLICANT_CONNECTED);
    registerReceiver(wifiEventReceiver, filter);

 private BroadcastReceiver wifiEventReceiver = new BroadcastReceiver() {
    private boolean enabled = false;

    @Override
    public void onReceive(Context context, Intent intent) {
        System.out.println ("WiFi Event BroadReceiver:onReceive()=======================================");
        if(intent.getAction().equals(WifiManager.SUPPLICANT_STATE_CHANGED_ACTION)) {
            System.out.println("===== SUPPLICANT_STATE_CHANGE_ACTION event happened....");
            intent.getBooleanExtra(WifiManager.EXTRA_SUPPLICANT_CONNECTED, false);
        }
    }
};
 private void createWifiAccessPoint() {
    if(wifiManager.isWifiEnabled())
    {
        wifiManager.setWifiEnabled(false);          
    }       
    Method[] wmMethods = wifiManager.getClass().getDeclaredMethods();   //Get all declared methods in WifiManager class     
    boolean methodFound=false;
    for(Method method: wmMethods){
        if(method.getName().equals("setWifiApEnabled")){
            methodFound=true;
            WifiConfiguration netConfig = new WifiConfiguration();
            netConfig.SSID = ssid; 
            netConfig.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN);
            try {
                boolean apstatus=(Boolean) method.invoke(wifiManager, netConfig,true);          
                for (Method isWifiApEnabledmethod: wmMethods)
                {
                    if(isWifiApEnabledmethod.getName().equals("isWifiApEnabled")){
                        while(!(Boolean)isWifiApEnabledmethod.invoke(wifiManager)){
                        };
                        for(Method method1: wmMethods){
                            if(method1.getName().equals("getWifiApState")){
                                int apstate;
                                apstate=(Integer)method1.invoke(wifiManager);
                            }
                        }
                    }
                }
                if(apstatus)
                {
                    Log.d("Splash Activity", "Access Point created");   
                }else
                {
                    Log.d("Splash Activity", "Access Point creation failed");   
                }

            } catch (IllegalArgumentException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
        }      
    }
    if(!methodFound){
        Log.d("Splash Activity", "Your phone's API does not contain setWifiApEnabled method to configure an access point");
    }
}        
Abraham
  • 191
  • 1
  • 2
  • 12

3 Answers3

0

It isn't clear exactly what you are trying to achieve here given that it is impossible to guarantee WiFi connectivity. The best you can do is to fail "gracefully" if it is lost.

To monitor the state of the WiFi network you need to register a listener for NETWORK_STATE_CHANGED_ACTION. Forget about the suppliant state.

To keep the WiFi radio awake use the WiFiLock.

Rupert Rawnsley
  • 2,622
  • 1
  • 29
  • 40
0

I thinky you can establish a TCP connection and check its status, or you can send UDP packet and check its status.

M. Usman Khan
  • 3,689
  • 1
  • 59
  • 69
0

It seems that you have to register a BroadcastReceiver for intent SUPPLICANT_STATE_CHANGED_ACTION or SUPPLICANT_CONNECTION_CHANGE_ACTION

I assume you know about registering receivers.

ernazm
  • 9,208
  • 4
  • 44
  • 51
  • i have added the code above..it did not work for me .. after running the application..i disabled the AP manually and i did not get any callback.. – Abraham Apr 29 '11 at 10:45
  • have you tried to obtain `EXTRA_NEW_STATE` instead of `EXTRA_SUPPLICANT_CONNECTED`? Btw, you can see all extras by debugging or calling `intent.getExtras.keySet()` – ernazm Apr 29 '11 at 10:53
  • hmmm...i have nt tried that but the onReceive() itself does not get called...i guess i can use the extras only if the onRecieve() function is invoked right... – Abraham Apr 29 '11 at 11:30