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

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

Android开发实践:WIFI扫描功能的封装-创新互联

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

成都创新互联公司主要从事成都做网站、成都网站设计、成都外贸网站建设、网页设计、企业做网站、公司建网站等业务。立足成都服务栾城,10余年网站建设经验,价格优惠、服务专业,欢迎来电咨询建站服务:18982081108

  本文先给出一个扫描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

另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


新闻名称:Android开发实践:WIFI扫描功能的封装-创新互联
本文链接:http://dzwzjz.com/article/codpoc.html
在线咨询
服务热线
服务热线:028-86922220
TOP