大橙子网站建设,新征程启航

为企业提供网站建设、域名注册、服务器等服务

Android开发实践:WIFI扫描功能的封装

   网上关于Android WIFI模块开发的博文已经很多,这里我也想给出我对WIFI模块编程的一些消化结果,将Android的一些API封装类,方便在各个Android APP中复用,简化程序的开发。

创新互联是一家集网站建设,苏仙企业网站建设,苏仙品牌网站建设,网站定制,苏仙网站建设报价,网络营销,网络优化,苏仙网站推广为一体的创新建站企业,帮助传统企业提升企业形象加强企业竞争力。可充分满足这一群体相比中小企业更为丰富、高端、多元的互联网需求。同时我们时刻保持专业、时尚、前沿,时刻以成就客户成长自我,坚持不断学习、思考、沉淀、净化自己,让我们为更多的企业打造出实用型网站。

   本文先给出一个扫描WIFI功能的封装成类,创建该类对象后,当你简单地调用search()函数后,后台会自动扫描当前的WIFI SSID,并以回调的形式通知你扫描结果,代码如下,需要注意的地方都用注释标注出来了。

package com.example.testwifi;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiManager;
                                  
public class WifiSearcher {
                                                                                                                                                                                             
    private static final int WIFI_SEARCH_TIMEOUT = 20; //扫描WIFI的超时时间
                                                                                                                                                                                             
    private Context mContext;
    private WifiManager mWifiManager;   
    private WiFiScanReceiver mWifiReceiver;      
    private Lock mLock;
    private Condition mCondition;
    private SearchWifiListener mSearchWifiListener;
    private boolean mIsWifiScanCompleted = false;
                                                                                                                                                                                             
    public static enum ErrorType {
        SEARCH_WIFI_TIMEOUT, //扫描WIFI超时(一直搜不到结果)
        NO_WIFI_FOUND,       //扫描WIFI结束,没有找到任何WIFI信号
    }
                                                                                                                                                                                             
    //扫描结果通过该接口返回给Caller
    public interface SearchWifiListener {
        public void onSearchWifiFailed(ErrorType errorType);
        public void onSearchWifiSuccess(List results);
    }
                                                                                                                                                                                             
    public WifiSearcher( Context context, SearchWifiListener listener ) {
                                                                                                                                                                                                 
        mContext = context;
        mSearchWifiListener = listener;
                                                                                                                                                                                                 
        mLock = new ReentrantLock();
        mCondition = mLock.newCondition();
        mWifiManager=(WifiManager)mContext.getSystemService(Context.WIFI_SERVICE);     
                                                                                                                                                                                                 
        mWifiReceiver = new WiFiScanReceiver();     
    }
                                                                                                                                                                                   
    public void search() {
                                                                                                                                                                                                 
        new Thread(new Runnable() {
                                                                                                                                                                                                     
            @Override
            public void run() {
                //如果WIFI没有打开,则打开WIFI
                if( !mWifiManager.isWifiEnabled() ) {
                    mWifiManager.setWifiEnabled(true);
                }
                                                                                                                                                                                                                 
                //注册接收WIFI扫描结果的监听类对象
                mContext.registerReceiver(mWifiReceiver, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
                                                                                                                                                                                                         
                //开始扫描
                mWifiManager.startScan();
                                                                                                                                                                                                                        
                mLock.lock();
                                                                                                                                                                                                         
                //阻塞等待扫描结果
                try {                
                    mIsWifiScanCompleted = false;                
                    mCondition.await(WIFI_SEARCH_TIMEOUT, TimeUnit.SECONDS);
                    if( !mIsWifiScanCompleted ) {
                        mSearchWifiListener.onSearchWifiFailed(ErrorType.SEARCH_WIFI_TIMEOUT);
                    }
                }
                catch (InterruptedException e) {
                    e.printStackTrace();
                }
                                                                                                                                                                                                         
                mLock.unlock();
                                                                                                                                                                                                         
                //删除注册的监听类对象
                mContext.unregisterReceiver(mWifiReceiver);                          
            }
        }).start();  
    }
                                                                                                                                                                                     
    //系统WIFI扫描结果消息的接收者      
    protected class WiFiScanReceiver extends BroadcastReceiver {
                                                                                                                                                                                                 
        public void onReceive(Context c, Intent intent) {
                                                                                                                                                                                             
            //提取扫描结果
            List ssidResults = new ArrayList();
            List scanResults = mWifiManager.getScanResults();              
            for(ScanResult result : scanResults ) {
                ssidResults.add(result.SSID);    
            }
                                                                                                                                                                                                     
            //检测扫描结果
            if( ssidResults.isEmpty() ) {
                mSearchWifiListener.onSearchWifiFailed(ErrorType.NO_WIFI_FOUND);
            }
            else {
                mSearchWifiListener.onSearchWifiSuccess(ssidResults);
            }
                                                                                                                                                                                                     
            mLock.lock();
            mIsWifiScanCompleted = true;
            mCondition.signalAll();  
            mLock.unlock();
        }
    }
}

   代码中用到了Lock和Condition,就是为了阻塞地等待WIFI扫描的结果,以来可以在search函数内部直接完成监听对象的registerReceiver和unregisterReceiver,防止由于某些原因忘记unregisterReceiver导致程序crash,另一方面,针对某些Android系统可能不一定能够及时收到WIFI扫描结果而设置一个超时时间,这样,不至于UI一直长时间地等待WIFI扫描结果而“假死”。

   对了,AndroidManifest.xml文件中记得添加权限支持哦:


      

   这个WIFI扫描类就分享到这里啦,希望对初学者有帮助,java文件见博文后面的附件,有任何疑问欢迎留言或者来信lujun.hust@gmail.com交流,或者关注我的新浪微博 @卢_俊 获取最新的文章和资讯。

附件:http://down.51cto.com/data/2364295

文章名称:Android开发实践:WIFI扫描功能的封装
当前地址:http://dzwzjz.com/article/iidssp.html
在线咨询
服务热线
服务热线:028-86922220
TOP