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了