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");
}
}