溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Android開發(fā)實(shí)踐:WIFI連接功能的封裝

發(fā)布時間:2020-06-05 11:03:55 來源:網(wǎng)絡(luò) 閱讀:11136 作者:Jhuster 欄目:移動開發(fā)

   在上一篇文章《Android開發(fā)實(shí)踐:WIFI掃描功能的封裝》介紹了如何利用Andriod的API實(shí)現(xiàn)WIFI的掃描,本文則重點(diǎn)講述一下如何連接WIFI吧,在此,也給出一個封裝WIFI連接過程的類,提供簡單的接口以供在各個代碼工程中復(fù)用。


   與WIFI掃描類似,WIFI的連接同樣是一個耗時的過程,所以需要放到線程中執(zhí)行,通過回調(diào)來通知調(diào)用者連接結(jié)果。該回調(diào)接口的定義如下:

public interface WifiConnectListener {
    public void OnWifiConnectCompleted( boolean isConnected );
}


   從Android的WIFI Setting可以看出,一般添加一個新的WIFI連接,需要給出三個信息,一個是WIFI的SSID,一個是WIFI的密碼,另一個是WIFI的加密類型,不同的加密方式,連接時程序中的配置是不同的,這里定義一個枚舉,給出四種常見的加密類型:


public enum SecurityMode {
    OPEN, WEP, WPA, WPA2
}


   Android的WIFI連接過程,總體上分為三步,第一步,添加網(wǎng)絡(luò)配置,第二步,根據(jù)網(wǎng)絡(luò)配置連接WIFI,第三步,監(jiān)聽系統(tǒng)的WIFI連接狀態(tài)消息。下面就直接給出示例代碼,關(guān)鍵的地方都在代碼中注釋了。


package com.example.testwifi;
                     
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.SupplicantState;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
                                                                                                                  
public class WifiConnector {
                                                                                                        
    private static final int WIFI_CONNECT_TIMEOUT = 20; //連接WIFI的超時時間
                                                                                                        
    private Context mContext;
    private WifiManager mWifiManager;           
    private Lock mLock;
    private Condition mCondition;
    private WiFiConncetReceiver mWifiConnectReceiver;
    private WifiConnectListener mWifiConnectListener;
    private boolean mIsConnnected = false;
    private int mNetworkID = -1;
              
    //網(wǎng)絡(luò)加密模式
    public enum SecurityMode {
        OPEN, WEP, WPA, WPA2
    }
                      
   //通知連接結(jié)果的監(jiān)聽接口
    public interface WifiConnectListener {
        public void OnWifiConnectCompleted( boolean isConnected );
    }
                        
    public WifiConnector( Context context , WifiConnectListener listener ) {
                                                                                                            
        mContext = context;   
                                                                                                            
        mLock = new ReentrantLock();
        mCondition = mLock.newCondition();
        mWifiManager=(WifiManager)mContext.getSystemService(Context.WIFI_SERVICE);   
                                                                                                            
        mWifiConnectReceiver = new WiFiConncetReceiver();  
                                                                                                            
        mWifiConnectListener = listener;
    }
                                                                                                                
    public void connect( final String ssid, final String password, final SecurityMode mode ) {
                                                                                                            
        new Thread(new Runnable() {
                                                                                                                
            @Override
            public void run() {
                                                                                                                    
                //如果WIFI沒有打開,則打開WIFI
                if( !mWifiManager.isWifiEnabled() ) {
                    mWifiManager.setWifiEnabled(true);
                }
                                                                                                                            
                //注冊連接結(jié)果監(jiān)聽對象
                mContext.registerReceiver(mWifiConnectReceiver, new IntentFilter(WifiManager.SUPPLICANT_STATE_CHANGED_ACTION));
                                                                                                                    
                //連接指定SSID
                if( !onConnect(ssid,password,mode) ) {
                    mWifiConnectListener.OnWifiConnectCompleted(false);
                }
                else {
                    mWifiConnectListener.OnWifiConnectCompleted(true);
                }
                                                                                                                    
                //刪除注冊的監(jiān)聽類對象
                mContext.unregisterReceiver(mWifiConnectReceiver);         
            }
        }).start();            
    }
                                                                                                        
    protected boolean onConnect( String ssid, String password, SecurityMode mode ) {
                                                                                                            
        //添加新的網(wǎng)絡(luò)配置
        WifiConfiguration cfg = new WifiConfiguration();
        cfg.SSID = "\"" + ssid + "\"";
        if( password !=null && !"".equals(password) ) {
            //這里比較關(guān)鍵,如果是WEP加密方式的網(wǎng)絡(luò),密碼需要放到cfg.wepKeys[0]里面
            if( mode == SecurityMode.WEP ) {
                cfg.wepKeys[0]   = "\"" + password + "\"";
                cfg.wepTxKeyIndex = 0;
            }
            else {
                cfg.preSharedKey = "\"" + password + "\"";
            }                  
        }
        cfg.status = WifiConfiguration.Status.ENABLED;
         
        //添加網(wǎng)絡(luò)配置
        mNetworkID = mWifiManager.addNetwork(cfg);
                                                                                                            
        mLock.lock();
                                                                                 
        mIsConnnected = false;
            
        //連接該網(wǎng)絡(luò)
        if( !mWifiManager.enableNetwork(mNetworkID , true) ) {
            mLock.unlock();
            return false;
        }
                                                                                                            
        try {
            //等待連接結(jié)果
            mCondition.await(WIFI_CONNECT_TIMEOUT, TimeUnit.SECONDS);
        }
        catch (InterruptedException e) {
            e.printStackTrace();       
        }
                                                                                                            
        mLock.unlock();
                                                                                                            
        return mIsConnnected;
    }  
                                                                                                        
    //監(jiān)聽系統(tǒng)的WIFI連接消息
    protected class WiFiConncetReceiver extends BroadcastReceiver {
                                                                                                                    
        @Override
        public void onReceive(Context context, Intent intent) {
                                                                                                                
            if (!WifiManager.SUPPLICANT_STATE_CHANGED_ACTION.equals(intent.getAction())) {
                return;
            }          
                                                                                                                
            mLock.lock();
                                                                                                                
            WifiInfo info = mWifiManager.getConnectionInfo();
            if ( info.getNetworkId()==mNetworkID && info.getSupplicantState() == SupplicantState.COMPLETED ) {
                mIsConnnected = true;
                mCondition.signalAll();    
            }
                                                                                                                
            mLock.unlock();
        }
    }
}

   與WIFI掃描的封裝代碼類似,這里也用到了Lock和Condition,就是為了阻塞地等待WIFI連接的結(jié)果,保證正確的registerReceiver和unregisterReceiver網(wǎng)絡(luò)連接狀態(tài)監(jiān)聽對象,同時,設(shè)置了WIFI連接超時,防止由于WIFI模塊的問題導(dǎo)致界面收不到回調(diào)而長時間“卡死”。


   另外,AndroidManifest.xml文件中記得添加權(quán)限支持哦:


<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>


   這個WIFI連接類的封裝就分享到這里啦,希望對初學(xué)者有幫助,java文件見博文后面的附件,有任何疑問歡迎留言或者來信lujun.hust@gmail.com交流,或者關(guān)注我的新浪微博 @盧_俊 獲取最新的文章和資訊。


附件:http://down.51cto.com/data/2364418
向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI