安卓wifi 相关内容笔记 扫描wifi热点 状态监听

参考资料 :Android wifi打开关闭以及状态监听
   官方api:http://www.android-doc.com/reference/android/net/wifi/WifiConfiguration.html
   Android开发—弹出列表、单选、多选框:
   https://blog.csdn.net/lyp_1020k/article/details/79856454

WifiManager:   wifi状态广播:
/**     
 * Broadcast intent action indicating that Wi-Fi has been enabled, disabled,
 * enabling, disabling, or unknown. One extra provides this state as an int.
 * Another extra provides the previous state, if available.
 *
 * @see #EXTRA_WIFI_STATE
 * @see #EXTRA_PREVIOUS_WIFI_STATE
 */
@SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
public static final String WIFI_STATE_CHANGED_ACTION =
    "android.net.wifi.WIFI_STATE_CHANGED";
/**
 * The lookup key for an int that indicates whether Wi-Fi is enabled,
 * disabled, enabling, disabling, or unknown.  Retrieve it with
 * {@link android.content.Intent#getIntExtra(String,int)}.
 *
 * @see #WIFI_STATE_DISABLED   //关闭
 * @see #WIFI_STATE_DISABLING//关闭中
 * @see #WIFI_STATE_ENABLED// 打开
 * @see #WIFI_STATE_ENABLING //打开中
 * @see #WIFI_STATE_UNKNOWN//位置
 */
public static final String EXTRA_WIFI_STATE = "wifi_state";
/**
 * The previous Wi-Fi state.
 *
 * @see #EXTRA_WIFI_STATE
 */
public static final String EXTRA_PREVIOUS_WIFI_STATE = "previous_wifi_state";

/**
 * Wi-Fi is currently being disabled. The state will change to {@link #WIFI_STATE_DISABLED} if
 * it finishes successfully.
 *
 * @see #WIFI_STATE_CHANGED_ACTION
 * @see #getWifiState()
 */
public static final int WIFI_STATE_DISABLING = 0;
/**
 * Wi-Fi is disabled.
 *
 * @see #WIFI_STATE_CHANGED_ACTION
 * @see #getWifiState()
 */
public static final int WIFI_STATE_DISABLED = 1;
/**
 * Wi-Fi is currently being enabled. The state will change to {@link #WIFI_STATE_ENABLED} if
 * it finishes successfully.
 *
 * @see #WIFI_STATE_CHANGED_ACTION
 * @see #getWifiState()
 */
public static final int WIFI_STATE_ENABLING = 2;
/**
 * Wi-Fi is enabled.
 *
 * @see #WIFI_STATE_CHANGED_ACTION
 * @see #getWifiState()
 */
public static final int WIFI_STATE_ENABLED = 3;
/**
 * Wi-Fi is in an unknown state. This state will occur when an error happens while enabling
 * or disabling.
 *
 * @see #WIFI_STATE_CHANGED_ACTION
 * @see #getWifiState()
 */
public static final int WIFI_STATE_UNKNOWN = 4;

/**
 * Broadcast intent action indicating that Wi-Fi AP has been enabled, disabled,
 * enabling, disabling, or failed.
 *
 * @hide
 */
@SystemApi
public static final String WIFI_AP_STATE_CHANGED_ACTION =
    "android.net.wifi.WIFI_AP_STATE_CHANGED";

/**
 * The lookup key for an int that indicates whether Wi-Fi AP is enabled,
 * disabled, enabling, disabling, or failed.  Retrieve it with
 * {@link android.content.Intent#getIntExtra(String,int)}.
 *
 * @see #WIFI_AP_STATE_DISABLED
 * @see #WIFI_AP_STATE_DISABLING
 * @see #WIFI_AP_STATE_ENABLED
 * @see #WIFI_AP_STATE_ENABLING
 * @see #WIFI_AP_STATE_FAILED
 *
 * @hide
 */
@SystemApi
public static final String EXTRA_WIFI_AP_STATE = "wifi_state";

/**
 * The look up key for an int that indicates why softAP started failed
 * currently support general and no_channel
 * @see #SAP_START_FAILURE_GENERIC
 * @see #SAP_START_FAILURE_NO_CHANNEL
 *

 

 

1.wifi状态监听:

private   WifiManager   wifiManager;
wifiManager =(WifiManager)mContext.getSystemService(Context.WIFI_SERVICE);
//注册广播
myWifiBrReceiver =new MyWifiBrReceiver();
IntentFilter    intentFilter  =new IntentFilter();
String  action =  WifiManager.WIFI_STATE_CHANGED_ACTION;
intentFilter.addAction(action);
registerReceiver(myWifiBrReceiver,intentFilter);

 2.获取状态

public  void  closeWifi(){
    int  state=  wifiManager.getWifiState();
    logView.ShowLog("  wifi state"+ state);
    wifiManager.setWifiEnabled(false);

    logView.ShowLog("  wifi state"+ state);
    wifiManager.setWifiEnabled(true);
    logView.ShowLog("  wifi state"+ state);

}

3.广播接收:

class   MyWifiBrReceiver      extends BroadcastReceiver  {
    @Override
    public void onReceive(Context context, Intent intent) {
      try{
        int   mWifiState=intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE,0);
       switch (mWifiState){
           case   WifiManager.WIFI_STATE_ENABLED://已经打开
            logView.ShowLog("已经打开 ");
           break;
           case  WifiManager.WIFI_STATE_DISABLING://关闭中
               logView.ShowLog("关闭中 ");
               break;
           case WifiManager.WIFI_STATE_DISABLED:   //已关闭
               logView.ShowLog("已关闭 ");
               break;
           case  WifiManager.WIFI_STATE_ENABLING://打开中
               logView.ShowLog("打开中 ");
               break;

           case WifiManager.WIFI_STATE_UNKNOWN:
               //未知
               logView.ShowLog("未知 ");
           break;
       }
      }catch ( NullPointerException  e){


      }

    }
} 

4.销毁

@Override
protected void onDestroy() {
    super.onDestroy();
    unregisterReceiver(myWifiBrReceiver);
}

 5.扫描wifi热点

public   void   getWifi(){

    wifiManager.startScan();
   //获取结果
    List<ScanResult>   list = wifiManager.getScanResults();
    String[]   items=  new   String[list.size()];
    for(int  i=0;i<list.size();i++){
        ScanResult   scanResult  = list.get(i);
        Log.d(TAG, "getWifi: ");
        Log.d(TAG, "ssid: "+scanResult.SSID);
        Log.d(TAG, " BSSID:"+scanResult.BSSID);
        Log.d(TAG, "capabilities :" +scanResult.capabilities);
        items[i]=scanResult.SSID;
    }
    showSingleAlertDialog(null,items );

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值