7月 25, 2017

『Android』用程式碼控制Andorid裝置的Wifi或熱點


當你想要在你的應用程式裡面幫使用者開啟Wifi或熱點時

需要以下幾個步驟方可幫使用者開啟


步驟一:
要先到Manifest.xml中加入權限
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>
<uses-permission android:name="android.permission.WRITE_SETTINGS"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

這裡我是將可能會用到的權限全部加入!

步驟二:
在使用Wifi控制之前要先加入WifiManager才能開始申請控制Wifi:
public WifiManager mWifiManager;
private WifiInfo mWifiInfo;
mWifiManager = (WifiManager) "你的Activity".this.getSystemService(context.WIFI_SERVICE);

接著就可以開始做你想要做的事情囉!

將Wifi開啟:
mWifiManager.setWifiEnabled(true);

將wifi關閉:
mWifiManager.setWifiEnabled(false);

檢查Wifi是否打開:
public boolean isWifiOn(){
        if(mWifiManager.isWifiEnabled()){
            Log.i(TAG,"Wifi is already On.");
            return true;
        }else {
            Log.i(TAG,"Wifi is already Off.");
            return false;
        }
}

檢查熱點是否打開:
public boolean isWifiHotSpotOn() {
        try {
            Method method = mWifiManager.getClass().getDeclaredMethod("isWifiApEnabled");
            method.setAccessible(true);
            return (Boolean) method.invoke(mWifiManager);
        }
        catch (Throwable ignored) {}
        return false;
}

熱點切換開/關:
public boolean switchApState() {
        try {
            WifiConfiguration wificonfiguration = null;
            // if WiFi is on, turn it off
            if(isWifiOn()) {
                mWifiManager.setWifiEnabled(false);
            }
            Method method = mWifiManager.getClass().getMethod("setWifiApEnabled",
            WifiConfiguration.class, boolean.class);
            method.invoke(mWifiManager, wificonfiguration, !isWifiHotSpotOn());
            return true;
        }
        catch (Exception e) {
            e.printStackTrace();
        }
        return false;
}

設定熱點名稱為newName:
public boolean setHotspotName(String newName) {
        try {
            Method getConfigMethod = mWifiManager.getClass().getMethod("getWifiApConfiguration");
            WifiConfiguration wifiConfig = (WifiConfiguration) getConfigMethod.invoke(mWifiManager);

            wifiConfig.allowedAuthAlgorithms.clear();
            wifiConfig.allowedGroupCiphers.clear();
            wifiConfig.allowedKeyManagement.clear();
            wifiConfig.allowedPairwiseCiphers.clear();
            wifiConfig.allowedProtocols.clear();

            wifiConfig.SSID = newName;
            wifiConfig.wepKeys[0] = "";
            wifiConfig.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
            wifiConfig.wepTxKeyIndex = 0;

            Method setConfigMethod = mWifiManager.getClass().getMethod("setWifiApConfiguration", WifiConfiguration.class);
            setConfigMethod.invoke(mWifiManager, wifiConfig);

            return true;
        }
        catch (Exception e) {
            e.printStackTrace();
            return false;
        }
}

取得現在連到的Wifi名稱:
public String getCurrentWifi(){
        WifiManager mWifiManager = (WifiManager)"你的Activity".this.getSystemService(Context.WIFI_SERVICE);
        WifiInfo mWifiInfo;

        if(mWifiManager.isWifiEnabled())
        {
            mWifiInfo = mWifiManager.getConnectionInfo();
            Log.e(TAG,"Current Wifi is " + mWifiInfo.getSSID());
            return mWifiInfo.getSSID();
        }else {
            return "getCurrentWifi : Wifi is close";
        }
}



沒有留言:

Related Posts Plugin for WordPress, Blogger...