android連接指定wifi
① Android Wifi 如何實現:在程序中把SSID和密碼都確定了,點擊連接就連接上指定的wifi
點擊打開設定,
點擊WLAN連接,
點擊添加WLAN。
② android怎麼自動連接以zd開頭的wifi,知道密碼,想用代碼直接連接
public class WifiAutoConnectManager {
private static final String TAG = WifiAutoConnectManager.class.getSimpleName();
WifiManager wifiManager;
// 定義幾種加密方式,一種是WEP,一種是WPA,還有沒有密碼的情況 public enum WifiCipherType { WIFICIPHER_WEP, WIFICIPHER_WPA, WIFICIPHER_NOPASS, WIFICIPHER_INVALID }
// 構造函數 public WifiAutoConnectManager(WifiManager wifiManager) { this.wifiManager = wifiManager; }
// 提供一個外部介面,傳入要連接的無線網 public void connect(String ssid, String password, WifiCipherType type) { Thread thread = new Thread(new ConnectRunnable(ssid, password, type)); thread.start(); }
// 查看以前是否也配置過這個網路 private WifiConfiguration isExsits(String SSID) { List<WifiConfiguration> existingConfigs = wifiManager.getConfiguredNetworks(); for (WifiConfiguration existingConfig : existingConfigs) { if (existingConfig.SSID.equals("\"" + SSID + "\"")) { return existingConfig; } } return null; }
private WifiConfiguration createWifiInfo(String SSID, String Password, WifiCipherType Type) { WifiConfiguration config = new WifiConfiguration(); config.allowedAuthAlgorithms.clear(); config.allowedGroupCiphers.clear(); config.allowedKeyManagement.clear(); config.allowedPairwiseCiphers.clear(); config.allowedProtocols.clear(); config.SSID = "\"" + SSID + "\""; // nopass if (Type == WifiCipherType.WIFICIPHER_NOPASS) { config.wepKeys[0] = ""; config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE); config.wepTxKeyIndex = 0; } // wep if (Type == WifiCipherType.WIFICIPHER_WEP) { if (!TextUtils.isEmpty(Password)) { if (isHexWepKey(Password)) { config.wepKeys[0] = Password; } else { config.wepKeys[0] = "\"" + Password + "\""; } } config.allowedAuthAlgorithms.set(AuthAlgorithm.OPEN); config.allowedAuthAlgorithms.set(AuthAlgorithm.SHARED); config.allowedKeyManagement.set(KeyMgmt.NONE); config.wepTxKeyIndex = 0; } // wpa if (Type == WifiCipherType.WIFICIPHER_WPA) { config.preSharedKey = "\"" + Password + "\""; config.hiddenSSID = true; config.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN); config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP); config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK); config.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP); // 此處需要修改否則不能自動重聯 // config.allowedProtocols.set(WifiConfiguration.Protocol.WPA); config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP); config.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP); config.status = WifiConfiguration.Status.ENABLED; } return config; }
// 打開wifi功能 private boolean openWifi() { boolean bRet = true; if (!wifiManager.isWifiEnabled()) { bRet = wifiManager.setWifiEnabled(true); } return bRet; }
class ConnectRunnable implements Runnable { private String ssid;
private String password;
private WifiCipherType type;
public ConnectRunnable(String ssid, String password, WifiCipherType type) { this.ssid = ssid; this.password = password; this.type = type; }
@Override public void run() { // 打開wifi openWifi(); // 開啟wifi功能需要一段時間(我在手機上測試一般需要1-3秒左右),所以要等到wifi // 狀態變成WIFI_STATE_ENABLED的時候才能執行下面的語句 while (wifiManager.getWifiState() == WifiManager.WIFI_STATE_ENABLING) { try { // 為了避免程序一直while循環,讓它睡個100毫秒檢測…… Thread.sleep(100); } catch (InterruptedException ie) { } }
WifiConfiguration wifiConfig = createWifiInfo(ssid, password, type); // if (wifiConfig == null) { Log.d(TAG, "wifiConfig is null!"); return; }
WifiConfiguration tempConfig = isExsits(ssid);
if (tempConfig != null) { wifiManager.removeNetwork(tempConfig.networkId); }
int netID = wifiManager.addNetwork(wifiConfig); boolean enabled = wifiManager.enableNetwork(netID, true); Log.d(TAG, "enableNetwork status enable=" + enabled); boolean connected = wifiManager.reconnect(); Log.d(TAG, "enableNetwork connected=" + connected); } }
private static boolean isHexWepKey(String wepKey) { final int len = wepKey.length();
// WEP-40, WEP-104, and some vendors using 256-bit WEP (WEP-232?) if (len != 10 && len != 26 && len != 58) { return false; }
return isHex(wepKey); }
private static boolean isHex(String key) { for (int i = key.length() - 1; i >= 0; i--) { final char c = key.charAt(i); if (!(c >= '0' && c <= '9' || c >= 'A' && c <= 'F' || c >= 'a' && c <= 'f')) { return false; } }
return true; }}
③ android 命令怎麼連接wifi點
1234567891011121314151617/** * 檢測網路是否連接 * @return */ private boolean checkNetworkState() { boolean flag = false; //得到網路連接信息 manager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); //去進行判斷網路是否連接 if (manager.getActiveNetworkInfo() != null) { flag = manager.getActiveNetworkInfo().isAvailable(); } if (!flag) { setNetwork(); } else { isNetworkAvailable(); }12 return flag; } /** * 網路未連接時,調用設置方法 */ private void setNetwork(){ Toast.makeText(this, "wifi is closed!", Toast.LENGTH_SHORT).show(); AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setIcon(R.drawable.ic_launcher); builder.setTitle("網路提示信息"); builder.setMessage("網路不可用,如果繼續,請先設置網路!"); builder.setPositiveButton("設置", new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Intent intent = null; /** * 判斷手機系統的版本!如果<a href="https:///s?wd=API&tn=44039180_cpr&fenlei=-EUBt1njRLPWfkn1D4PWm1njmkPWf" target="_blank" class="-highlight">API</a>大於10 就是3.0+ * 因為3.0以上的版本的設置和3.0以下的設置不一樣,調用的方法不同 */ if (android.os.Build.VERSION.SDK_INT > 10) { intent = new Intent(android.provider.Settings.ACTION_WIFI_SETTINGS); } else { intent = new Intent(); ComponentName component = new ComponentName( "com.android.settings", "com.android.settings.WirelessSettings"); intent.setComponent(component); intent.setAction("android.intent.action.VIEW"); } startActivity(intent); } });123 builder.setNegativeButton("取消", new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) {3233343536 } }); builder.create(); builder.show(); } /** * 網路已經連接,然後去判斷是wifi連接還是<a href="https:///s?wd=GPRS&tn=44039180_cpr&fenlei=-EUBt1njRLPWfkn1D4PWm1njmkPWf" target="_blank" class="-highlight">GPRS</a>連接 * 設置一些自己的邏輯調用 */ private void isNetworkAvailable(){ State <a href="https:///s?wd=gprs&tn=44039180_cpr&fenlei=-EUBt1njRLPWfkn1D4PWm1njmkPWf" target="_blank" class="-highlight">gprs</a> = manager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState(); State wifi = manager.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState(); if(<a href="https:///s?wd=gprs&tn=44039180_cpr&fenlei=-EUBt1njRLPWfkn1D4PWm1njmkPWf" target="_blank" class="-highlight">gprs</a> == State.CONNECTED gprs == State.CONNECTING){ Toast.makeText(this, "wifi is open! gprs", Toast.LENGTH_SHORT).show(); } //判斷為wifi狀態下才載入廣告,如果是GPRS手機網路則不載入! if(wifi == State.CONNECTED wifi == State.CONNECTING){ Toast.makeText(this, "wifi is open! wifi", Toast.LENGTH_SHORT).show(); loadAdmob(); } } /** * 在wifi狀態下 載入admob廣告 */ private void loadAdmob(){ ll = (LinearLayout) findViewById(R.id.load_ads); ll.removeAllViews(); adsView = new AdView(this, AdSize.BANNER, "a15194a1ac9505d"); ll.addView(adsView); adsView.loadAd(new AdRequest()); }
④ 安卓手機怎麼一鍵連接到指定WIFI網路
三星手機的部分ROM非常奇葩,不能保存WIFI密碼,每次連接WIFI都得重新輸入密碼。2.X的版本如此,5.X的版本也是如此。必須修改系統文件才能自動連接指定的WIFI。解決這個問題的方法如下:
一、root手機:雖然現在很多軟體自稱可以一鍵root手機,但有的無法root,有的root不完全,有的root後不穩定。最正規的root方法,是卡刷SuperSU_Pro_v2.46。
二、下載安裝Root Explorer。
三、打開Root Explorer,進入data/misc/wifi/文件夾,找到wpa_supplicant.conf 文件。
.
⑤ android如何控制只能連接固定的wifi
方法步驟:
一、依次點擊「設定」>「WLAN」
⑥ 如何通過WIFI連接Android手機調試
1.首先讓android手機來監聽指定的埠:
這一步需要使用shell,因此手機上要有終端模擬器,不過網上很多,隨便找個就行了,依次敲入下列幾行:
su//獲取root許可權
setprop service.adb.tcp.port 5555//設置監聽的埠,埠可以自定義,如5554,5555是默認的
stop adbd//關閉adbd
start adbd//重新啟動adbd2.
手機連接wifi並確保手機和電腦連接同一個網路
,記下手機的ip地址,假設為a.b.c.d
3.電腦上打開命令提示符,敲入以下命令:
⑦ android wifi 開發中 如何判斷成功連接上了指定的wifi熱點
首先通過WifiManager對象獲取系統服務的WIFI_SERVICE,然後調用getConnectionInfo()獲得WifiInfo對象,再通過WifiInfo對象獲取已連上wifi的SSID(實際上是housyunhin說的WifiInfo.getSSID()和其他wifi信息),之後判斷是否匹配即可
⑧ android開發,怎麼自動鏈接熱點
參考如下內容,可以實現沒有密碼的wifi自動連接:
實現自動連接到指定的WiFi熱點,我們需要熟悉Android里的wifimanager類,下面是結合網路資料和本人實踐給出的方法,供參考:
1. 自定義一個wifiAdmin操作類:
[java] view plain
import java.util.List;
import android.content.Context;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.net.wifi.WifiManager.WifiLock;
public class WifiAdmin {
// 定義WifiManager對象
private WifiManager mWifiManager;
// 定義WifiInfo對象
private WifiInfo mWifiInfo;
// 掃描出的網路連接列表
private List<ScanResult> mWifiList;
// 網路連接列表
private List<WifiConfiguration> mWifiConfiguration;
// 定義一個WifiLock
WifiLock mWifiLock;
// 構造器
public WifiAdmin(Context context) {
// 取得WifiManager對象
mWifiManager = (WifiManager) context
.getSystemService(Context.WIFI_SERVICE);
// 取得WifiInfo對象
mWifiInfo = mWifiManager.getConnectionInfo();
}
// 打開WIFI
public void openWifi() {
if (!mWifiManager.isWifiEnabled()) {
mWifiManager.setWifiEnabled(true);
}
}
// 關閉WIFI
public void closeWifi() {
if (mWifiManager.isWifiEnabled()) {
mWifiManager.setWifiEnabled(false);
}
}
// 檢查當前WIFI狀態
public int checkState() {
return mWifiManager.getWifiState();
}
// 鎖定WifiLock
public void acquireWifiLock() {
mWifiLock.acquire();
}
// 解鎖WifiLock
public void releaseWifiLock() {
// 判斷時候鎖定
if (mWifiLock.isHeld()) {
mWifiLock.acquire();
}
}
// 創建一個WifiLock
public void creatWifiLock() {
mWifiLock = mWifiManager.createWifiLock("Test");
}
// 得到配置好的網路
public List<WifiConfiguration> getConfiguration() {
return mWifiConfiguration;
}
// 指定配置好的網路進行連接
public void connectConfiguration(int index) {
// 索引大於配置好的網路索引返回
if (index > mWifiConfiguration.size()) {
return;
}
// 連接配置好的指定ID的網路
mWifiManager.enableNetwork(mWifiConfiguration.get(index).networkId,
true);
}
public void startScan() {
mWifiManager.startScan();
// 得到掃描結果
mWifiList = mWifiManager.getScanResults();
// 得到配置好的網路連接
mWifiConfiguration = mWifiManager.getConfiguredNetworks();
}
// 得到網路列表
public List<ScanResult> getWifiList() {
return mWifiList;
}
// 查看掃描結果
public StringBuilder lookUpScan() {
StringBuilder stringBuilder = new StringBuilder();
for (int i = 0; i < mWifiList.size(); i++) {
stringBuilder
.append("Index_" + new Integer(i + 1).toString() + ":");
// 將ScanResult信息轉換成一個字元串包
// 其中把包括:BSSID、SSID、capabilities、frequency、level
stringBuilder.append((mWifiList.get(i)).toString());
stringBuilder.append("/n");
}
return stringBuilder;
}
// 得到MAC地址
public String getMacAddress() {
return (mWifiInfo == null) ? "NULL" : mWifiInfo.getMacAddress();
}
// 得到接入點的BSSID
public String getBSSID() {
return (mWifiInfo == null) ? "NULL" : mWifiInfo.getBSSID();
}
// 得到IP地址
public int getIPAddress() {
return (mWifiInfo == null) ? 0 : mWifiInfo.getIpAddress();
}
// 得到連接的ID
public int getNetworkId() {
return (mWifiInfo == null) ? 0 : mWifiInfo.getNetworkId();
}
// 得到WifiInfo的所有信息包
public String getWifiInfo() {
return (mWifiInfo == null) ? "NULL" : mWifiInfo.toString();
}
// 添加一個網路並連接
public boolean addNetwork(WifiConfiguration wcg) {
int wcgID = mWifiManager.addNetwork(wcg);
boolean b = mWifiManager.enableNetwork(wcgID, true);
System.out.println("a--" + wcgID);
System.out.println("b--" + b);
return b;
}
// 斷開指定ID的網路
public void disconnectWifi(int netId) {
mWifiManager.disableNetwork(netId);
mWifiManager.disconnect();
}
//然後是一個實際應用方法,只驗證過沒有密碼的情況:
public WifiConfiguration CreateWifiInfo(String SSID, String Password, int Type)
{
WifiConfiguration config = new WifiConfiguration();
config.allowedAuthAlgorithms.clear();
config.allowedGroupCiphers.clear();
config.allowedKeyManagement.clear();
config.allowedPairwiseCiphers.clear();
config.allowedProtocols.clear();
config.SSID = "\"" + SSID + "\"";
WifiConfiguration tempConfig = this.IsExsits(SSID);
if(tempConfig != null) {
mWifiManager.removeNetwork(tempConfig.networkId);
}
if(Type == 1) //WIFICIPHER_NOPASS
{
config.wepKeys[0] = "";
config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
config.wepTxKeyIndex = 0;
}
if(Type == 2) //WIFICIPHER_WEP
{
config.hiddenSSID = true;
config.wepKeys[0]= "\""+Password+"\"";
config.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.SHARED);
config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);
config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP104);
config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
config.wepTxKeyIndex = 0;
}
if(Type == 3) //WIFICIPHER_WPA
{
config.preSharedKey = "\""+Password+"\"";
config.hiddenSSID = true;
config.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN);
config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
config.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
//config.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
config.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
config.status = WifiConfiguration.Status.ENABLED;
}
return config;
}
private WifiConfiguration IsExsits(String SSID)
{
List<WifiConfiguration> existingConfigs = mWifiManager.getConfiguredNetworks();
for (WifiConfiguration existingConfig : existingConfigs)
{
if (existingConfig.SSID.equals("\""+SSID+"\""))
{
return existingConfig;
}
}
return null;
}
}
//分為三種情況:1沒有密碼2用wep加密3用wpa加密
2.調用時是這樣的:
[java] view plain
WifiAdmin wifiAdmin = new WifiAdmin(wifiTest.this); //wifiTest.this is a context for sample;
wifiAdmin.openWifi();
WifiConfiguration wcg=wifiAdmin.CreateWifiInfo(wifiName, wifiPass,3); //wifiName is the SSID; wifiPass is the password;
wifiAdmin.addNetwork(wcg);
3.稍等片刻,便會連接上wifi了