机器人连接到由名编程方式打开无线网络 - 这是最好的解决办法? [英] Android connect to Open WiFi programmatically by name - which is Best solution?
本文介绍了机器人连接到由名编程方式打开无线网络 - 这是最好的解决办法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我一直在尝试使用两种方法以编程方式连接到开wifi(结门户)的名字。
我用OP1,但它仅约80%的时间。
_____________________________________________________
OP1:
公共静态无效connectToWifi(上下文的背景下,字符串networkSSID){
WifiConfiguration的conf =新WifiConfiguration();
conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
WifiManager wifiManager =(WifiManager)context.getSystemService(Context.WIFI_SERVICE);
wifiManager.addNetwork(CONF);
名单< WifiConfiguration>表= wifiManager.getConfiguredNetworks();
对于(WifiConfiguration我:名单){
Log.d(TAG +| connectToWifi(),i.SSID =>中+ i.SSID);
如果(i.SSID = NULL和放大器;!&安培; i.SSID.equals(\+ networkSSID +\)){
wifiManager.disconnect();
wifiManager.enableNetwork(i.networkId,真正的);
wifiManager.reconnect();
Log.d(TAG +| connectToWifi(),IF和GT; i.SSID =>中+ i.SSID);
打破;
}其他{Log.e(TAG +| connectToWifi(),ELSE - 无法连接> i.SSID =>中+ i.SSID);}
}
} //结束connectToWifi
OP2:从 http://stackoverflow.com/a/8818490/3792198
conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
WifiManager wifiManager =(WifiManager)context.getSystemService(Context.WIFI_SERVICE);
wifiManager.addNetwork(CONF);
名单< WifiConfiguration>表= wifiManager.getConfiguredNetworks();
对于(WifiConfiguration我:名单){
如果(i.SSID = NULL和放大器;!&安培; i.SSID.equals(\+ networkSSID +\)){
wifiManager.disconnect();
wifiManager.enableNetwork(i.networkId,真正的);
wifiManager.reconnect();
打破;
}
}
OP3:
WifiConfiguration wifiConfig =新WifiConfiguration();
wifiConfig.SSID =的String.Format(\%s \的SSID);
。wifiConfig preSharedKey =的String.Format(\%s \的,关键);
WifiManager wifiManager =(WifiManager)getSystemService(WIFI_SERVICE);
//记住ID
INT NETID = wifiManager.addNetwork(wifiConfig);
wifiManager.disconnect();
wifiManager.enableNetwork(网络标识符号,真正的);
wifiManager.reconnect();
或者,你有什么建议吗? 谢谢
解决方案
进口android.app.Activity;
进口android.content.Context;
进口android.net.ConnectivityManager;
进口android.net.NetworkInfo;
进口android.net.wifi.ScanResult;
进口android.net.wifi.WifiConfiguration;
进口android.net.wifi.WifiInfo;
进口android.net.wifi.WifiManager;
进口android.text.TextUtils;
进口android.util.Log;
进口的java.util.List;
/ **
*的ConnectionManager
* /
公共类的ConnectionManager {
私人上下文的背景下;
私人活动的活动;
私有静态最后弦乐WPA =WPA;
私有静态最后弦乐WEP =WEP;
私有静态最后弦乐OPEN =打开;
私人最终静态字符串变量=WiFiConnector;
公众的ConnectionManager(上下文的背景下){
this.context =背景;
this.activity =(活动)范围内;
}
公共无效enableWifi(){
WifiManager wifiManager =(WifiManager)context.getSystemService(Context.WIFI_SERVICE);
如果(!wifiManager.isWifiEnabled()){
wifiManager.setWifiEnabled(真正的);
新ShowToast(背景下,无线已打开);
}
}
公众诠释requestWIFIConnection(字符串networkSSID,字符串networkPass){
尝试 {
WifiManager wifiManager =(WifiManager)context.getSystemService(Context.WIFI_SERVICE);
//检查SSID存在
如果(scanWifi(wifiManager,networkSSID)){
如果(getCurrentSSID(wifiManager)= NULL和放大器;!&安培; getCurrentSSID(wifiManager).equals(\+ networkSSID +\)){
新ShowToast(背景下,已经连接通过+ networkSSID);
返回Constants.ALREADY_CONNECTED;
}
//安全型检测
字符串SECURE_TYPE = checkSecurity(wifiManager,networkSSID);
如果(SECURE_TYPE == NULL){
新ShowToast(背景下,无法找到安全类型+ networkSSID);
返回Constants.UNABLE_TO_FIND_SECURITY_TYPE;
}
如果(SECURE_TYPE.equals(WPA)){
WPA(networkSSID,networkPass,wifiManager);
}否则,如果(SECURE_TYPE.equals(WEP)){
WEP(networkSSID,networkPass);
} 其他 {
OPEN(wifiManager,networkSSID);
}
返回Constants.CONNECTION_REQUESTED;
}
/ * connectME(); * /
}赶上(例外五){
新ShowToast(背景下,错误连接WIFI+ E);
}
返回Constants.SSID_NOT_FOUND;
}
私人无效WPA(字符串networkSSID,字符串networkPass,WifiManager wifiManager){
WifiConfiguration WC =新WifiConfiguration();
wc.SSID =\+ networkSSID +\;
。WC preSharedKey =\+ networkPass +\;
wc.status = WifiConfiguration.Status.ENABLED;
wc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
wc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
wc.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
wc.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
wc.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
wc.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
INT ID = wifiManager.addNetwork(WC);
wifiManager.disconnect();
wifiManager.enableNetwork(ID,真);
wifiManager.reconnect();
}
私人无效WEP(字符串networkSSID,字符串networkPass){
}
私人无效OPEN(WifiManager wifiManager,串networkSSID){
WifiConfiguration WC =新WifiConfiguration();
wc.SSID =\+ networkSSID +\;
wc.hiddenSSID = TRUE;
wc.priority = 0xBADBAD;
wc.status = WifiConfiguration.Status.ENABLED;
wc.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
INT ID = wifiManager.addNetwork(WC);
wifiManager.disconnect();
wifiManager.enableNetwork(ID,真);
wifiManager.reconnect();
}
布尔scanWifi(WifiManager wifiManager,串networkSSID){
Log.e(TAG,scanWifi开始);
名单< ScanResult>扫描列表= wifiManager.getScanResults();
对于(ScanResult我:扫描列表){
如果(i.SSID!= NULL){
Log.e(TAG,SSID:+ i.SSID);
}
如果(i.SSID = NULL和放大器;!&安培; i.SSID.equals(networkSSID)){
Log.e(TAG,找到SSID:+ i.SSID);
返回true;
}
}
新ShowToast(背景下,SSID+ networkSSID +未找到);
返回false;
}
公共字符串getCurrentSSID(WifiManager wifiManager){
字符串SSID = NULL;
ConnectivityManager connManager =(ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
的NetworkInfo的NetworkInfo = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
如果(networkInfo.isConnected()){
最终WifiInfo connectionInfo = wifiManager.getConnectionInfo();
如果(connectionInfo = NULL和放大器;!&安培;!TextUtils.isEmpty(connectionInfo.getSSID())){
SSID = connectionInfo.getSSID();
}
}
返回SSID;
}
私人字符串checkSecurity(WifiManager wifiManager,串SSID){
名单< ScanResult> networkList = wifiManager.getScanResults();
对于(ScanResult网络:networkList){
如果(network.SSID.equals(SSID)){
字符串能力= network.capabilities;
如果(Capabilities.contains(WPA)){
返回WPA;
}否则,如果(Capabilities.contains(WEP)){
返回WEP;
} 其他 {
回程OPEN;
}
}
}
返回null;
}
}
调用方法
的ConnectionManager的ConnectionManager =新的ConnectionManager(上下文);
connectionManager.enableWifi();
connectionManager.requestWIFIConnection(YOUR_SSID,密码);
I been trying to use two methods to programmatically connect to a open wifi (connective portal) by name.
I used op1, but it only works about 80% of times.
_____________________________________________________
op1 :
public static void connectToWifi(Context context, String networkSSID) {
WifiConfiguration conf = new WifiConfiguration();
conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
wifiManager.addNetwork(conf);
List<WifiConfiguration> list = wifiManager.getConfiguredNetworks();
for (WifiConfiguration i : list) {
Log.d(TAG + " |connectToWifi()", "i.SSID=> " + i.SSID);
if (i.SSID != null && i.SSID.equals("\"" + networkSSID + "\"")) {
wifiManager.disconnect();
wifiManager.enableNetwork(i.networkId, true);
wifiManager.reconnect();
Log.d(TAG + " |connectToWifi()", "IF > i.SSID=> " + i.SSID);
break;
}else {Log.e(TAG + " |connectToWifi()", "ELSE - FAILED TO CONNECT > i.SSID=> " + i.SSID);}
}
}//end connectToWifi
op2: from http://stackoverflow.com/a/8818490/3792198
conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
WifiManager wifiManager = (WifiManager)context.getSystemService(Context.WIFI_SERVICE);
wifiManager.addNetwork(conf);
List<WifiConfiguration> list = wifiManager.getConfiguredNetworks();
for( WifiConfiguration i : list ) {
if(i.SSID != null && i.SSID.equals("\"" + networkSSID + "\"")) {
wifiManager.disconnect();
wifiManager.enableNetwork(i.networkId, true);
wifiManager.reconnect();
break;
}
}
op3:
WifiConfiguration wifiConfig = new WifiConfiguration();
wifiConfig.SSID = String.format("\"%s\"", ssid);
wifiConfig.preSharedKey = String.format("\"%s\"", key);
WifiManager wifiManager = (WifiManager)getSystemService(WIFI_SERVICE);
//remember id
int netId = wifiManager.addNetwork(wifiConfig);
wifiManager.disconnect();
wifiManager.enableNetwork(netId, true);
wifiManager.reconnect();
Or, do you have any suggestions ? thank you
解决方案
import android.app.Activity;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.text.TextUtils;
import android.util.Log;
import java.util.List;
/**
* ConnectionManager
*/
public class ConnectionManager {
private Context context;
private Activity activity;
private static final String WPA = "WPA";
private static final String WEP = "WEP";
private static final String OPEN = "Open";
private final static String TAG = "WiFiConnector";
public ConnectionManager(Context context) {
this.context = context;
this.activity = (Activity) context;
}
public void enableWifi() {
WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
if (!wifiManager.isWifiEnabled()) {
wifiManager.setWifiEnabled(true);
new ShowToast(context, "Wifi Turned On");
}
}
public int requestWIFIConnection(String networkSSID, String networkPass) {
try {
WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
//Check ssid exists
if (scanWifi(wifiManager, networkSSID)) {
if (getCurrentSSID(wifiManager) != null && getCurrentSSID(wifiManager).equals("\"" + networkSSID + "\"")) {
new ShowToast(context, "Already Connected With " + networkSSID);
return Constants.ALREADY_CONNECTED;
}
//Security type detection
String SECURE_TYPE = checkSecurity(wifiManager, networkSSID);
if (SECURE_TYPE == null) {
new ShowToast(context, "Unable to find Security type for " + networkSSID);
return Constants.UNABLE_TO_FIND_SECURITY_TYPE;
}
if (SECURE_TYPE.equals(WPA)) {
WPA(networkSSID, networkPass, wifiManager);
} else if (SECURE_TYPE.equals(WEP)) {
WEP(networkSSID, networkPass);
} else {
OPEN(wifiManager, networkSSID);
}
return Constants.CONNECTION_REQUESTED;
}
/*connectME();*/
} catch (Exception e) {
new ShowToast(context, "Error Connecting WIFI " + e);
}
return Constants.SSID_NOT_FOUND;
}
private void WPA(String networkSSID, String networkPass, WifiManager wifiManager) {
WifiConfiguration wc = new WifiConfiguration();
wc.SSID = "\"" + networkSSID + "\"";
wc.preSharedKey = "\"" + networkPass + "\"";
wc.status = WifiConfiguration.Status.ENABLED;
wc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
wc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
wc.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
wc.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
wc.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
wc.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
int id = wifiManager.addNetwork(wc);
wifiManager.disconnect();
wifiManager.enableNetwork(id, true);
wifiManager.reconnect();
}
private void WEP(String networkSSID, String networkPass) {
}
private void OPEN(WifiManager wifiManager, String networkSSID) {
WifiConfiguration wc = new WifiConfiguration();
wc.SSID = "\"" + networkSSID + "\"";
wc.hiddenSSID = true;
wc.priority = 0xBADBAD;
wc.status = WifiConfiguration.Status.ENABLED;
wc.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
int id = wifiManager.addNetwork(wc);
wifiManager.disconnect();
wifiManager.enableNetwork(id, true);
wifiManager.reconnect();
}
boolean scanWifi(WifiManager wifiManager, String networkSSID) {
Log.e(TAG, "scanWifi starts");
List<ScanResult> scanList = wifiManager.getScanResults();
for (ScanResult i : scanList) {
if (i.SSID != null) {
Log.e(TAG, "SSID: " + i.SSID);
}
if (i.SSID != null && i.SSID.equals(networkSSID)) {
Log.e(TAG, "Found SSID: " + i.SSID);
return true;
}
}
new ShowToast(context, "SSID " + networkSSID + " Not Found");
return false;
}
public String getCurrentSSID(WifiManager wifiManager) {
String ssid = null;
ConnectivityManager connManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
if (networkInfo.isConnected()) {
final WifiInfo connectionInfo = wifiManager.getConnectionInfo();
if (connectionInfo != null && !TextUtils.isEmpty(connectionInfo.getSSID())) {
ssid = connectionInfo.getSSID();
}
}
return ssid;
}
private String checkSecurity(WifiManager wifiManager, String ssid) {
List<ScanResult> networkList = wifiManager.getScanResults();
for (ScanResult network : networkList) {
if (network.SSID.equals(ssid)) {
String Capabilities = network.capabilities;
if (Capabilities.contains("WPA")) {
return WPA;
} else if (Capabilities.contains("WEP")) {
return WEP;
} else {
return OPEN;
}
}
}
return null;
}
}
Call the method
ConnectionManager connectionManager=new ConnectionManager(context);
connectionManager.enableWifi();
connectionManager.requestWIFIConnection("YOUR_SSID","PASSWORD");
这篇关于机器人连接到由名编程方式打开无线网络 - 这是最好的解决办法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文