Back to project page WifiHotspotBTEnabler.
The source code is released under:
GNU General Public License
If you think the Android project WifiHotspotBTEnabler listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package ca.diogosoares.android.utilities; //from w ww. j a v a2s.c o m import java.lang.reflect.Method; import java.util.ArrayList; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.net.wifi.WifiConfiguration; import android.net.wifi.WifiManager; import android.util.Log; import ca.diogosoares.android.listeners.HotspotUtilitiesListener; /** * * Require permissions for : * android.permission.ACCESS_WIFI_STATE * android.permission.CHANGE_WIFI_STATE * android.permission.UPDATE_DEVICE_STATE * android.net.wifi.WIFI_AP_STATE_CHANGED * * @author Diogo Soares, 02/2014 */ public class HotspotUtilities { public static final String WIFI_AP_STATE_CHANGED_ACTION = "android.net.wifi.WIFI_AP_STATE_CHANGED"; public static final String EXTRA_WIFI_AP_STATE = "wifi_state"; public static final String EXTRA_PREVIOUS_WIFI_AP_STATE = "previous_wifi_state"; public static final int WIFI_AP_STATE_DISABLING = 10; public static final int WIFI_AP_STATE_DISABLED = 11; public static final int WIFI_AP_STATE_ENABLING = 12; public static final int WIFI_AP_STATE_ENABLED = 13; public static final int WIFI_AP_STATE_FAILED = 14; private Context mContext = null; private ArrayList<HotspotUtilitiesListener> mListeners = null; private int mWifiState = WIFI_AP_STATE_DISABLED; private int mPreviousWifiState = WIFI_AP_STATE_DISABLED; private final BroadcastReceiver mReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); if (WIFI_AP_STATE_CHANGED_ACTION.equals(action)) { mWifiState = intent.getIntExtra(EXTRA_WIFI_AP_STATE, WIFI_AP_STATE_FAILED); mPreviousWifiState = intent.getIntExtra(EXTRA_PREVIOUS_WIFI_AP_STATE, WIFI_AP_STATE_FAILED); for(HotspotUtilitiesListener listener:mListeners) listener.onWifiAPStateChanged(); } } }; public HotspotUtilities(Context context) { mContext = context; mContext.registerReceiver(mReceiver, new IntentFilter(WIFI_AP_STATE_CHANGED_ACTION)); mListeners = new ArrayList<HotspotUtilitiesListener>(); } public void setListener(HotspotUtilitiesListener listener) { this.mListeners.add(listener); } public void removeListener(HotspotUtilitiesListener listener) { this.mListeners.remove(listener); } /** * Activate or deactivate the Wifi hotspot * * @param enable */ public void setEnableWifiHotspot(boolean enable) { WifiManager wifiManager = (WifiManager) mContext.getSystemService(Context.WIFI_SERVICE); try { Method method = wifiManager.getClass().getMethod("setWifiApEnabled", WifiConfiguration.class, boolean.class); // keep this order.. Wifi cannot be turned on when AP is on if (enable) { //wifiManager.setWifiEnabled(false); method.invoke(wifiManager, null, true); } else { method.invoke(wifiManager, null, false); //wifiManager.setWifiEnabled(true); } } catch (Exception e) { Log.e("TetherSettings::setWifiHotspotTetheringEnabled", e.getCause().getMessage()); } } public int getWifiAPState() { return this.mWifiState; } public int getPreviousWifiState() { return this.mPreviousWifiState; } public static String getWifiAPStateString(int state) { switch (state) { case HotspotUtilities.WIFI_AP_STATE_DISABLED: return "Disabled"; case HotspotUtilities.WIFI_AP_STATE_DISABLING: return "Disabling"; case HotspotUtilities.WIFI_AP_STATE_ENABLED: return "Enabled"; case HotspotUtilities.WIFI_AP_STATE_ENABLING: return "Enabling"; case HotspotUtilities.WIFI_AP_STATE_FAILED: return "Failed"; default: return "Unknown"; } } }