Board logo

標題: Android App - 監察WiFi 狀態改變 [打印本頁]

作者: admin    時間: 2012-8-26 16:34     標題: Android App - 監察WiFi 狀態改變

當運行的Apps 需要在WiiFi 啟動及關閉時, 作出不同反應, 便需要採用 BroadcastReceiver 去讀取WifiManager 傳過來的訊息, 下便為例子
package sample.AndroidWifiStateChangedDetect;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.widget.TextView;

public class AndroidWifiStateChangedDetect extends Activity {

  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.main);
      this.registerReceiver(this.WifiStateChangedReceiver,  new IntentFilter(WifiManager.WIFI_STATE_CHANGED_ACTION));
  }

  private BroadcastReceiver WifiStateChangedReceiver  = new BroadcastReceiver(){
  @Override
  public void onReceive(Context context, Intent intent) {
  // TODO Auto-generated method stub

  int extraWifiState = intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE ,   WifiManager.WIFI_STATE_UNKNOWN);

  switch(extraWifiState){
  case WifiManager.WIFI_STATE_DISABLED:
      
Toast.makeText(AndroidWifiStateChangedDetect.this, "WIFI DISABLED", Toast.LENGTH_SHORT).show();

      break;
  case WifiManager.WIFI_STATE_DISABLING:
      Toast.makeText(AndroidWifiStateChangedDetect.this, "WIFI DISABLING ", Toast.LENGTH_SHORT).show();
      break;
  case WifiManager.WIFI_STATE_ENABLED:
      Toast.makeText(AndroidWifiStateChangedDetect.this, "WIFI ENABLED ", Toast.LENGTH_SHORT).show();
      break;
  case WifiManager.WIFI_STATE_ENABLING:
      Toast.makeText(AndroidWifiStateChangedDetect.this, "WIFI ENABLING ", Toast.LENGTH_SHORT).show();
      break;
  case WifiManager.WIFI_STATE_UNKNOWN:
      Toast.makeText(AndroidWifiStateChangedDetect.this, "WIFI UNKNOWN ", Toast.LENGTH_SHORT).show();
      break;
  }

}};
}



參考網頁
http://android-er.blogspot.hk/2011/01/detect-wifi-onoff-state.html

作者: admin    時間: 2012-8-26 16:50

如果在監察WiFi的同時要獲得更多資訊如 SSID, BSSID, IP, MAC, RSSI, Speed, 可參考以下例子

package sample.AndroidWifiMonitor;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.widget.TextView;

public class AndroidWifiMonitor extends Activity {

TextView textConnected, textIp, textSsid, textBssid, textMac, textSpeed, textRssi;

   /** Called when the activity is first created. */
   @Override
   public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.main);

       textConnected = (TextView)findViewById(R.id.Connected);
       textIp = (TextView)findViewById(R.id.Ip);      
       textSsid = (TextView)findViewById(R.id.Ssid);
       textBssid = (TextView)findViewById(R.id.Bssid);
       textMac = (TextView)findViewById(R.id.Mac);
       textSpeed = (TextView)findViewById(R.id.Speed);
       textRssi = (TextView)findViewById(R.id.Rssi);
      
       DisplayWifiState();
       this.registerReceiver(this.myWifiReceiver,  new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));
   }
  
   private BroadcastReceiver myWifiReceiver  = new BroadcastReceiver(){
  @Override
  public void onReceive(Context arg0, Intent arg1) {
   // TODO Auto-generated method stub
   NetworkInfo networkInfo = (NetworkInfo) arg1.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO);
   if(networkInfo.getType() == ConnectivityManager.TYPE_WIFI){
    DisplayWifiState();
   }
  }};
  
   private void DisplayWifiState(){
   
    ConnectivityManager myConnManager = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
    NetworkInfo myNetworkInfo = myConnManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
    WifiManager myWifiManager = (WifiManager)getSystemService(Context.WIFI_SERVICE);
  WifiInfo myWifiInfo = myWifiManager.getConnectionInfo();
  
  textMac.setText(myWifiInfo.getMacAddress());
  
    if (myNetworkInfo.isConnected()){
     int myIp = myWifiInfo.getIpAddress();  //測試過程中 myIP 計算出來的IP有點不對, 需要作進一步測試
      
     textConnected.setText("--- CONNECTED ---");
      
     int intMyIp3 = myIp/0x1000000;
     int intMyIp3mod = myIp%0x1000000;
      
     int intMyIp2 = intMyIp3mod/0x10000;
     int intMyIp2mod = intMyIp3mod%0x10000;
      
     int intMyIp1 = intMyIp2mod/0x100;
     int intMyIp0 = intMyIp2mod%0x100;
      
     textIp.setText(String.valueOf(intMyIp0)
       + "." + String.valueOf(intMyIp1)
       + "." + String.valueOf(intMyIp2)
       + "." + String.valueOf(intMyIp3)
       );
      
     textSsid.setText(myWifiInfo.getSSID()); // 己測試過
     textBssid.setText(myWifiInfo.getBSSID());  //未測
     
     textSpeed.setText(String.valueOf(myWifiInfo.getLinkSpeed()) + " " + WifiInfo.LINK_SPEED_UNITS);
//未測
    textRssi.setText(String.valueOf(myWifiInfo.getRssi())); //未測
    }
    else{
     textConnected.setText("--- DIS-CONNECTED! ---");
     textIp.setText("---");
     textSsid.setText("---");
     textBssid.setText("---");
     textSpeed.setText("---");
     textRssi.setText("---");
    }
   }
}


記得在AndroidManifest.xml 中加入以下兩句
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />


參考網貢
http://android-er.blogspot.hk/2011/01/monitor-wifi-status-and-information.html





歡迎光臨 How2Do (http://forum.how2do.com.hk/) Powered by Discuz! 7.2