我是学生,我必须创建一个连接到wifi的应用程序。 我可以查看列表wifi并创建一个按钮" connected"和"断开"。 但我不知道如何从我创建的列表中连接到wifi。 这是我的代码。
WifiItem:
public class WifiItem {
private String APName;
private String AdresseMac;
private int ForceSignal;
public String getAPName() {
return APName;
}
public void setAPName(String aPName) {
APName = aPName;
}
public String getAdresseMac() {
return AdresseMac;
}
public void setAdresseMac(String adresseMac) {
AdresseMac = adresseMac;
}
public int getForceSignal() {
return ForceSignal;
}
public void setForceSignal(int forceSignal) {
ForceSignal = forceSignal;
}
}
WifiBroadcastReceiver
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiManager;
import android.widget.Toast;
import android.util.Log;
import java.util.List;
public class WifiBroadcastReceiver extends BroadcastReceiver {
private WifiManager wifiManager;
private WifiAdapter wifiAdapter;
private List<WifiItem> listeWifiItem;
@Override
public void onReceive(Context context, Intent intent) {
wifiManager = ((MainActivity) context).getCurrentWifiManager();
wifiAdapter = ((MainActivity) context).getWifiAdapter();
listeWifiItem = ((MainActivity) context).getListeWifiItem();
if (wifiManager != null) {
if (wifiManager.isWifiEnabled()) {
List<ScanResult> listeScan = wifiManager.getScanResults();
listeWifiItem.clear();
for (ScanResult scanResult : listeScan) {
WifiItem item = new WifiItem();
item.setAdresseMac(scanResult.BSSID);
item.setAPName(scanResult.SSID);
item.setForceSignal(scanResult.level);
Log.d("Formation Wifi", scanResult.SSID + " LEVEL "
+ scanResult.level);
listeWifiItem.add(item);
}
wifiAdapter.notifyDataSetChanged();
} else {
Toast.makeText(context, "Vous devez activer votre WiFi",
Toast.LENGTH_SHORT);
}
}
}
}
WifiAdapter:
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.content.Context;
import android.widget.TextView;
import android.widget.BaseAdapter;
import android.graphics.Color;
import java.util.List;
public class WifiAdapter extends BaseAdapter {
private List<WifiItem> listeWifiItem;
private LayoutInflater layoutInflater;
public WifiAdapter(Context context, List<WifiItem> objects) {
listeWifiItem = objects;
layoutInflater = LayoutInflater.from(context);
}
public int getCount() {
return listeWifiItem.size();
}
public Object getItem(int position) {
return listeWifiItem.get(position);
}
public long getItemId(int position) {
return position;
}
private class ViewWifiHolder {
TextView tvApName;
TextView tvAdresseMac;
TextView ForceSignal;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewWifiHolder viewHolder;
if (convertView == null) {
viewHolder = new ViewWifiHolder();
convertView = layoutInflater.inflate(R.layout.content_main, null);
viewHolder.tvApName = (TextView) convertView.findViewById(R.id.tvWifiName);
viewHolder.tvAdresseMac = (TextView) convertView.findViewById(R.id.tvWifiMac);
viewHolder.ForceSignal = (TextView) convertView.findViewById(R.id.ForceSignal);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewWifiHolder) convertView.getTag();
}
viewHolder.tvApName.setText(listeWifiItem.get(position).getAPName());
viewHolder.tvAdresseMac.setText(listeWifiItem.get(position).getAdresseMac());
// color is changed depending on the signal strength
if (listeWifiItem.get(position).getForceSignal() <= -80) {
viewHolder.ForceSignal.setBackgroundColor(Color.GREEN);
} else if (listeWifiItem.get(position).getForceSignal() <= -50) {
viewHolder.ForceSignal.setBackgroundColor(Color.YELLOW);
} else {
viewHolder.ForceSignal.setBackgroundColor(Color.RED);
}
return convertView;
}
}
我的MainActivity:
import android.content.IntentFilter;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.widget.Button;
import android.widget.ListView;
import android.view.View;
import android.content.Context;
import android.app.Activity;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends Activity {
private Button activer;
private Button desactiver;
private Button boutonRechercher;
private ListView listeViewWifi;
private List<WifiItem> listeWifiItem;
private WifiAdapter wifiAdapter;
private WifiManager wifiManager;
private WifiBroadcastReceiver broadcastReceiver;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
activer = (Button) findViewById(R.id.activer);
activer.setOnClickListener(new View.OnClickListener()
{
public void onClick (View v)
{
startWifi();
}
});
desactiver = (Button) findViewById(R.id.desactiver);
desactiver.setOnClickListener(new View.OnClickListener()
{
public void onClick (View v)
{
stopWifi();
}
});
listeViewWifi = (ListView) findViewById(R.id.listViewWifi);
boutonRechercher = (Button) findViewById(R.id.buttonRefresh);
boutonRechercher.setOnClickListener (new View.OnClickListener()
{
public void onClick(View v)
{
if(wifiManager != null)
wifiManager.startScan();
}
});
wifiManager = (WifiManager) this.getSystemService(Context.WIFI_SERVICE);
listeWifiItem = new ArrayList<WifiItem>();
wifiAdapter = new WifiAdapter(this, listeWifiItem);
listeViewWifi.setAdapter(wifiAdapter);
// Creating the broadcast Receiver
broadcastReceiver = new WifiBroadcastReceiver();
// The receiver is attached to the scan result
registerReceiver(broadcastReceiver, new IntentFilter(
WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
}
// We stop the receiver when you pause the application
@Override
protected void onPause() {
unregisterReceiver(broadcastReceiver);
super.onPause();
}
// Started It puts the receiver back on when the application
@Override
protected void onResume() {
registerReceiver(broadcastReceiver, new IntentFilter(
WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
super.onResume();
}
public void startWifi() {
WifiManager wM = (WifiManager) getSystemService(WIFI_SERVICE);
wM.setWifiEnabled(true);
}
public void stopWifi(){
WifiManager wM = (WifiManager) getSystemService(WIFI_SERVICE);
wM.setWifiEnabled(false);
}
public WifiManager getCurrentWifiManager() {
return wifiManager;
}
public WifiAdapter getWifiAdapter() {
return wifiAdapter;
}
public List<WifiItem> getListeWifiItem() {
return listeWifiItem;
}
}
谢谢你的回复,我很抱歉我的英语,我是法语:)