Android学习笔记之WI-FI(1)

jopen 13年前
     <1>什么是Wi-Fi?    <p>Wi-Fi是一种可以将个人电脑、手持设备(如PDA、手机)等终端以无线方式互相连接的技术。Wi-Fi是一个无线网路通信技术的品牌,由Wi-Fi联盟(Wi-FiAlliance)所持有。</p>    <p>Wi-Fi 原先是无线保真的缩写,Wi-Fi 的英文全称为wireless fidelity,在无线局域网的范畴是指“无线相容性认证”,实质上是一种商业认证,同时也是一种无线联网的技术,以前通过网线连接电脑,而现在则是通过无线电波来连网;常见的就是一个无线路由器,那么在这个无线路由器的电波覆盖的有效范围都可以采用WIFI连接方式进行联网,如果无线路由器连接了一条ADSL线路或者别的上网线路,则又被称为“热点”。</p>    <p><2>获取WI-FI网卡的状态(常量)</p>    <p>public    static    final    int    WIFI_STATE_DISABLED</p>    <div class="jd-details-descr">     <div class="jd-tagdata jd-tagdescr">      <p>Wi-Fi is disabled.      WI-FI网卡不可用</p>     </div>     <div class="jd-tagdata">      <span class="jd-tagtitle">Constant Value(常量值): </span>      <span>       1         (0x00000001)                   </span>     </div>    </div>    <p> </p>    <p><span class="normal"> public    static    final   int    </span> WIFI_STATE_DISABLING   </p>    <div class="jd-details api apilevel-1">     <div class="jd-details-descr">      <div class="jd-tagdata jd-tagdescr">       <p>Wi-Fi is currently being disabled.   WI-FI网卡正在关闭</p>       <p>The state will change to <code>WIFI_STATE_DISABLED</code> if it finishes successfully. </p>       <p> 最终状态会变成WI-FI网卡不可用</p>      </div>      <div class="jd-tagdata">       <span class="jd-tagtitle">Constant Value(常量值): </span>       <span>        0          (0x00000000)                   </span>      </div>     </div>    </div>    <p> </p>    <p><span class="normal"> public   static    final      int     </span>WIFI_STATE_ENABLED   </p>    <div class="jd-details api apilevel-1">     <div class="jd-details-descr">      <div class="jd-tagdata jd-tagdescr">       <p>Wi-Fi is enabled.  WI-FI网卡可用</p>       <p><span class="jd-tagtitle">Constant Value(常量值): </span><span>       3         (0x00000003)                   </span></p>      </div>     </div>    </div>    <p> </p>    <p><span class="normal"> public   static    final    int      </span>WIFI_STATE_ENABLING   </p>    <div class="jd-details api apilevel-1">     <div class="jd-details-descr">      <div class="jd-tagdata jd-tagdescr">       <p>Wi-Fi is currently being enabled.   WI-FI网卡正在打开</p>       <p>The state will change to <code>WIFI_STATE_ENABLED</code> if it finishes successfully.</p>       <p>最终状态会变成WI-FI网卡可用</p>      </div>      <div class="jd-tagdata">       <span class="jd-tagtitle">Constant Value(常量值): </span>       <span>        2         (0x00000002)                   </span>      </div>     </div>    </div>    <p> </p>    <p><span class="normal"> public    static    final    int      </span> WIFI_STATE_UNKNOWN   </p>    <div class="jd-details api apilevel-1">     <div class="jd-details-descr">      <div class="jd-tagdata jd-tagdescr">       <p>Wi-Fi is in an unknown state.  未知 网卡状态</p>       <p>This state will occur when an error happens while enabling or disabling.</p>      </div>      <div class="jd-tagdata">       <span class="jd-tagtitle">Constant Value(常量值): </span>       <span>        4          (0x00000004)                   </span>      </div>     </div>    </div>    <p> </p>    <p><3>操作WI-FI的所需的权限</p>    <p>可以在帮助文档里找到详细的权限,这里只列出一部分。</p>    <p>Manifest.permission(权限)</p>    <p>public         static         final         String        CHANGE_WIFI_MULTICAST_STATE    </p>    <div class="jd-details-descr">     <div class="jd-tagdata jd-tagdescr">      <p>Allows applications to enter Wi-Fi Multicast mode </p>     </div>     <div class="jd-tagdata">      <span class="jd-tagtitle">Constant Value: </span>      <span>                            "android.permission.CHANGE_WIFI_MULTICAST_STATE"                   </span>     </div>    </div>    <p> </p>    <p><span class="normal">public         static         final         String</span>        CHANGE_WIFI_STATE    </p>    <div class="jd-details api apilevel-1">     <div class="jd-details-descr">      <div class="jd-tagdata jd-tagdescr">       <p>Allows applications to change Wi-Fi connectivity state </p>      </div>      <div class="jd-tagdata">       <span class="jd-tagtitle">Constant Value: </span>       <span>                            "android.permission.CHANGE_WIFI_STATE"                   </span>      </div>     </div>    </div>    <p> </p>    <p>public         static         final         String        CHANGE_NETWORK_STATE    </p>    <div class="jd-details-descr">     <div class="jd-tagdata jd-tagdescr">      <p>Allows applications to change network connectivity state </p>     </div>     <div class="jd-tagdata">      <span class="jd-tagtitle">Constant Value: </span>      <span>                            "android.permission.CHANGE_NETWORK_STATE</span>     </div>    </div>    <p> </p>    <p><4>改变WI-FI网卡的状态</p>    <p>对WI-FI网卡进行操作需要通过WifiManager对象进行</p>    <p><span>WifiManager  wifiManager = (WifiManager) <span class="keyword">this</span>.getSystemService(Context.WIFI_SERVICE);</span></p>    <p><span>打开WI-FI网卡</span></p>    <p><span>wifiManager.setWifiEnabled(true);    </span></p>    <p><span>关闭</span><span><span>WI-FI网卡</span></span></p>    <p><span><span>wifiManager.setWifiEnabled(false);    </span></span></p>    <p><span><span>获取网卡当前状态</span></span></p>    <p><span><span><span><span>wifiManager..getWifiState()</span></span></span></span></p>    <pre class="brush:java; toolbar: true; auto-links: false;">package mars.wifi;  import android.app.Activity; import android.content.Context; import android.net.wifi.WifiManager; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.Toast;  public class WifiActivity extends Activity {     /** Called when the activity is first created. */  private Button startButton = null;  private Button stopButton = null;  private Button checkButton = null;  private WifiManager wifiManager = null;     @Override     public void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.main);         startButton = (Button)findViewById(R.id.startWifi);         stopButton = (Button)findViewById(R.id.stopWifi);         checkButton = (Button)findViewById(R.id.checkWifi);         startButton.setOnClickListener(new StartWifiListener());         stopButton.setOnClickListener(new StopWifiListener());         checkButton.setOnClickListener(new CheckWifiListener());     }     class StartWifiListener implements OnClickListener{    public void onClick(View v) {    wifiManager = (WifiManager)WifiActivity.this.getSystemService(Context.WIFI_SERVICE);    wifiManager.setWifiEnabled(true);    System.out.println("wifi state --->" + wifiManager.getWifiState());    Toast.makeText(WifiActivity.this, "当前Wifi网卡状态为" + wifiManager.getWifiState(), Toast.LENGTH_SHORT).show();   }     }     class StopWifiListener implements OnClickListener{    public void onClick(View arg0) {    // TODO Auto-generated method stub    wifiManager = (WifiManager)WifiActivity.this.getSystemService(Context.WIFI_SERVICE);    wifiManager.setWifiEnabled(false);    switch (wifiManager.getWifiState())              {                  case WifiManager.WIFI_STATE_DISABLED:                   System.out.println("wifi state --->" + "WIFI已关闭");                     break;                   case WifiManager.WIFI_STATE_DISABLING:                   System.out.println("wifi state --->" + "WIFI正在关闭中");                      break;                  case WifiManager.WIFI_STATE_ENABLED:                   System.out.println("wifi state --->" + "WIFI已启用");                      break;                  case WifiManager.WIFI_STATE_ENABLING:                   System.out.println("wifi state --->" + "WIFI正在启动中");                      break;                  case WifiManager.WIFI_STATE_UNKNOWN:                   System.out.println("wifi state --->" + "未知WIFI状态");                      break;              }     Toast.makeText(WifiActivity.this, "当前Wifi网卡状态为" + wifiManager.getWifiState(), Toast.LENGTH_SHORT).show();   }          }          class CheckWifiListener implements OnClickListener{    public void onClick(View v) {    wifiManager = (WifiManager)WifiActivity.this.getSystemService(Context.WIFI_SERVICE);    System.out.println("wifi state --->" + wifiManager.getWifiState());    Toast.makeText(WifiActivity.this, "当前Wifi网卡状态为" + wifiManager.getWifiState(), Toast.LENGTH_SHORT).show();   }           } }</pre>    <pre class="brush:xml; toolbar: true; auto-links: false;"><?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android"  package="mars.wifi" android:versionCode="1" android:versionName="1.0">  <application android:icon="@drawable/icon" android:label="@string/app_name">   <activity android:name=".WifiActivity" android:label="@string/app_name">    <intent-filter>     <action android:name="android.intent.action.MAIN" />     <category android:name="android.intent.category.LAUNCHER" />    </intent-filter>   </activity>   </application>  <uses-sdk android:minSdkVersion="4" />  <!-- 以下是使用wifi访问网络所需要的权限 -->  <uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"></uses-permission>  <uses-permission android:name="android.permission.CHANGE_WIFI_STATE"></uses-permission>  <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>  <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission> </manifest> </pre>    <p></p>