2013年1月30日水曜日

Android テザリング設定アプリを作る

テザリング設定アプリを作ろうとしたが、APIは非公開。

2.3のドキュメントでの記述を見つけた↓
http://www.androidjavadoc.com/2.3/android/net/wifi/WifiManager.html

いつか消えてしまうかもしれないので(^^;;、転記。

isWifiApEnabled (テザリング状態取得)
public boolean isWifiApEnabled()
Return whether Wi-Fi AP is enabled or disabled.
Returns:
true if Wi-Fi AP is enabled

setWifiApEnabled(テザリングのON/OFF切り替え)
public boolean setWifiApEnabled(WifiConfiguration wifiConfig,
                                boolean enabled)
Start AccessPoint mode with the specified configuration. If the radio is already running in AP mode, update the new configuration Note that starting in access point mode disables station mode operation
Parameters:
wifiConfig - SSID, security and channel details as part of WifiConfiguration
Returns:
true if the operation succeeds, false otherwise

以下、Javaのリフレクションを使ったサンプル

(テザリング状態取得)
    private String getTetheringStatus(){
    String status = "UNSUPPORTED";
        try {
    Method method = wifiManager.getClass().getMethod("isWifiApEnabled");
    if("true".equals(method.invoke(wifiManager).toString())){
    status ="ON";
    }else{
    status ="OFF";
    }
    } catch (Exception e) {
    e.printStackTrace();
    }
        return status;
    }


(テザリングのON/OFF切り替え)
    private void setTetheringEnabled(boolean toBeEnabled) { 
        Method method;
        try {
           method = wifiManager.getClass().getMethod("setWifiApEnabled",WifiConfiguration.class, boolean.class);
           method.invoke(wifiManager, null,toBeEnabled);
        } catch (Exception e) {
         e.printStackTrace();
}
        if(toBeEnabled){
           tetheringButton.setEnabled(false);
           tetheringStatus.setText(ON);
        }else{            
           tetheringButton.setEnabled(true);    
           tetheringStatus.setText(OFF);
        }
   }

あと、AndroidManifest.xmlでパーミッションの設定も必要。

    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
    <uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>



参考URL





0 件のコメント:

コメントを投稿