gpt4 book ai didi

android - 通过 Android 应用程序连接到 WiFi

转载 作者:行者123 更新时间:2023-11-29 00:18:49 25 4
gpt4 key购买 nike

我想创建一个应用程序,检测附近可用的 wifi 连接,然后连接到它们。到目前为止我所做的是,我创建了一个列出可用 wifi 连接的 ListView,然后我创建了一个 LongItemClick 对话框,显示网络的 SSID 和 BSSID,并要求输入密码。现在,我希望连接到其中一个网络,而不管它是哪种网络,它可能是 WEP、WPA 或 Open。我无法大致了解如何连接到它们。谁能帮我这个 ?我搜索了所有可用的答案,但没有找到任何答案可以帮助我做到这一点!

我用下面的方法尝试上面的事情。我创建了附近所有可用 Wi-Fi 网络的 ListView ,然后尝试在长按时显示连接信息,并提供通过点击连接到点击的 Wi-Fi 网络的选项。

代码:

package com.example.random;



import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.FragmentManager;
import android.app.ListActivity;
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.WifiConfiguration;
import android.net.wifi.WifiConfiguration.AuthAlgorithm;
import android.net.wifi.WifiConfiguration.GroupCipher;
import android.net.wifi.WifiConfiguration.KeyMgmt;
import android.net.wifi.WifiConfiguration.PairwiseCipher;
import android.net.wifi.WifiConfiguration.Protocol;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends FragmentActivity implements OnClickListener
{
WifiManager wifi;
ListView lv;
// TextView textStatus;
Button buttonScan;
int size = 0;
List<ScanResult> results;
final Context context = this;
EditText pass;
String checkPassword = null;

String ITEM_KEY = "key";
ArrayList<HashMap<String, String>> arraylist = new ArrayList<HashMap<String, String>>();
SimpleAdapter adapter;

/* Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

// textStatus = (TextView) findViewById(R.id.textStatus);
buttonScan = (Button) findViewById(R.id.buttonScan);
buttonScan.setOnClickListener(this);
lv = (ListView)findViewById(R.id.list);
lv.setOnItemClickListener(new OnItemClickListener(){

@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
connectToWifi(arg2);
}



private void connectToWifi(final int position)
{
final Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.connect);
dialog.setTitle("Connect to Network");
TextView textSSID = (TextView) dialog.findViewById(R.id.textSSID);
TextView textBSSID = (TextView) dialog.findViewById(R.id.textBSSID);
TextView capabilities = (TextView) dialog.findViewById(R.id.textCapabilities);

Button dialogButton = (Button) dialog.findViewById(R.id.dialogButtonOK);
pass = (EditText) dialog.findViewById(R.id.textPassword);
textSSID.setText(results.get(position).SSID);
textBSSID.setText(results.get(position).BSSID);
capabilities.setText(results.get(position).capabilities);
//
// if button is clicked, connect to the network;
dialogButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
checkPassword = pass.getText().toString();
finallyConnect(checkPassword);
dialog.dismiss();
}

private void finallyConnect(String checkPassword) {
String networkSSID = results.get(position).SSID;
String networkPass = checkPassword;

WifiConfiguration wifiConfig = new WifiConfiguration();
wifiConfig.SSID = String.format("\"%s\"", networkSSID);
wifiConfig.preSharedKey = String.format("\"%s\"", networkPass);

WifiManager wifiManager = (WifiManager) getSystemService(WIFI_SERVICE);
//remember id
int netId = wifiManager.addNetwork(wifiConfig);
wifiManager.disconnect();
wifiManager.enableNetwork(netId, true);
wifiManager.reconnect();



WifiConfiguration conf = new WifiConfiguration();
conf.SSID = "\"\"" + networkSSID + "\"\"";
conf.preSharedKey = "\""+ networkPass +"\"";
WifiManager wifiManager1 = (WifiManager)context.getSystemService(Context.WIFI_SERVICE);
wifiManager1.addNetwork(conf);

// List<WifiConfiguration> list = wifiManager1.getConfiguredNetworks();
// for( WifiConfiguration i : list ) {
// if(i.SSID != null && i.SSID.equals("\"" + networkSSID + "\"")) {
// wifiManager1.disconnect();
//
// wifiManager1.enableNetwork(i.networkId, true);
// if(wifiManager1.reconnect()){
//// int ipAddress = wifiManager.getConnectionInfo().getIpAddress();
//// Toast.makeText(getApplicationContext(), ipAddress, Toast.LENGTH_SHORT).show();
// Toast.makeText(getApplicationContext(), networkSSID + " "+ "Connection successful", Toast.LENGTH_SHORT).show();
// }
// else{
// Toast.makeText(getApplicationContext(), "Connection Failed", Toast.LENGTH_SHORT).show();
// }
//
// break;
//
// }
// }

}
});
dialog.show();
}
});

lv.setOnItemLongClickListener(new OnItemLongClickListener(){

@Override
public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
showWifiSettings(arg2);
return true;
}

private void showWifiSettings(int arg2) {
showDialogOfOptions(arg2);
}

private void showDialogOfOptions(int arg2) {
// Create a custom Dialog
final Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.custom);
dialog.setTitle("Network details");
TextView textSSID = (TextView) dialog.findViewById(R.id.textSSID);
TextView textBSSID = (TextView) dialog.findViewById(R.id.textBSSID);
Button dialogButton = (Button) dialog.findViewById(R.id.dialogButtonOK);
textSSID.setText(results.get(arg2).SSID);
textBSSID.setText(results.get(arg2).BSSID);
// if button is clicked, close the custom dialog
dialogButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});

dialog.show();
}
});

wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
if (wifi.isWifiEnabled() == false)
{
Toast.makeText(getApplicationContext(), "wifi is disabled..making it enabled", Toast.LENGTH_LONG).show();
wifi.setWifiEnabled(true);
}

this.adapter = new SimpleAdapter(MainActivity.this, arraylist, R.layout.row, new String[] { ITEM_KEY }, new int[] { R.id.list_value });
lv.setAdapter(this.adapter);

registerReceiver(new BroadcastReceiver()
{
@Override
public void onReceive(Context c, Intent intent)
{
results = wifi.getScanResults();
size = results.size();
}
}, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
}

// protected void connectionStatus(String password) {
// // TODO Auto-generated method stub
//
//
// }

public void onClick(View view)
{


arraylist.clear();
wifi.startScan();

Toast.makeText(this, "Scanning...." + size, Toast.LENGTH_SHORT).show();
try
{
size = size - 1;
while (size >= 0)
{
HashMap<String, String> item = new HashMap<String, String>();
item.put(ITEM_KEY, results.get(size).SSID.toString()+ results.get(size).capabilities.toString());

arraylist.add(item);
size--;
adapter.notifyDataSetChanged();
}


}
catch (Exception e)
{ }
}
}

应用程序运行良好,但未连接到任何 Wifi 网络。有什么帮助吗?

最佳答案

您需要一个 ScanResult 来确定用户选择的 wifi 配置的加密类型。

下面的代码在我的应用程序中运行良好,除了 EAP 加密类型尚未处理。

还有一件事,方法 mWifiManager.reassociate( ) 和 mWifiManager.reconnect( ) 对我来说似乎是一样的。

 private void connectWifi( ScanResult sr , String pwd )
{

WifiConfiguration config = new WifiConfiguration( );
config.allowedAuthAlgorithms.clear( );
config.allowedGroupCiphers.clear( );
config.allowedKeyManagement.clear( );
config.allowedPairwiseCiphers.clear( );
config.allowedProtocols.clear( );
config.SSID = "\"" + sr.SSID + "\"";

if( sr.capabilities.contains( "WEP" ) )
{
config.hiddenSSID = true;

if( pwd.matches( "[0-9A-Fa-f]{64}" ) )
{
config.preSharedKey = pwd;
}
else
{
config.preSharedKey = '"' + pwd + '"';
}

config.allowedKeyManagement.set( KeyMgmt.NONE );
config.allowedAuthAlgorithms.set( WifiConfiguration.AuthAlgorithm.OPEN );
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.wepTxKeyIndex = 0;

}
else if( sr.capabilities.contains( "PSK" ) )
{
config.preSharedKey = "\"" + pwd + "\"";
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.allowedGroupCiphers.set( WifiConfiguration.GroupCipher.CCMP );
config.allowedPairwiseCiphers.set( WifiConfiguration.PairwiseCipher.CCMP );
config.status = WifiConfiguration.Status.ENABLED;
}
else if( sr.capabilities.contains( "EAP" ) )
{

}
else
{
config.allowedKeyManagement.set( KeyMgmt.NONE );
}

int wcgID = mWifiManager.addNetwork( config );
mWifiManager.enableNetwork( wcgID , true );
// mWifiManager.reconnect( );
mWifiManager.reassociate( );
mWifiManager.saveConfiguration( );
}






private final OnItemClickListener mOnItemClickListener = new OnItemClickListener( )
{

@Override
public void onItemClick( AdapterView< ? > parent , View view , int position , long id )
{
final ScanResult sr = sortWifiList.get( position );
if( !mWifiInfo.getSSID( ).equals( "\"" + sr.SSID + "\"" ) )
{
mConnectState = NO_CONNECT;
refreshConnectViews( );
}
connectImage = (ImageView)view.findViewById( R.id.wifi_connected );
connectTextView = (TextView)view.findViewById( R.id.wifi_info_state );
connectTextView.setTag( sr );
// connectTextView.setVisibility( View.VISIBLE );
// connectImage.setVisibility( View.VISIBLE );
// connectTextView.setText( R.string.wifi_connecting );
// connectImage.setBackground( refreshDrawable );
// connectImage.startAnimation( animation );
// TextView wifiSsid = (TextView)view.findViewById( R.id.wifi_ssid );
// wifiSsid.setPadding( 0 , 0 , 0 , 0 );
if( !mWifiInfo.getSSID( ).equals( "\"" + sr.SSID + "\"" ) )
{
if( hasLocked( sr.capabilities ) )
{
mWifiConfiguration = mWifiManager.getConfiguredNetworks( );
WifiConfiguration wc = hasSaved( sr );
// saved wifi , connect directly
if( wc != null )
{
connectWifi( wc );
}
else
{
enterPasswordDialog( view , sr );
}
}
else
{
connectWifi( sr , "" );
}
}
autoRefresh = false;
mScanner.pause( );
}
};


private boolean hasLocked( String capabilities )
{
if( capabilities.contains( "WEP" ) )
{
return true;
}
else if( capabilities.contains( "PSK" ) )
{
return true;
}
else if( capabilities.contains( "EAP" ) )
{
return true;
}
return false;
}

private void connectWifi( WifiConfiguration wc )
{
int wcgID = mWifiManager.addNetwork( wc );
mWifiManager.enableNetwork( wcgID , true );
// mWifiManager.reconnect( );
mWifiManager.reassociate( );
}

关于android - 通过 Android 应用程序连接到 WiFi,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24503076/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com