import android.content.Context;
import android.net.Uri;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import com.google.android.gms.appindexing.Action;
import com.google.android.gms.appindexing.AppIndex;
import com.google.android.gms.appindexing.Thing;
import com.google.android.gms.common.api.GoogleApiClient;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private Button open, close, shaomiao, info,gone;
private TextView tv;
private WifiManager wm;
/**
* ATTENTION: This was auto-generated to implement the App Indexing API.
* See https://g.co/AppIndexing/AndroidStudio for more information.
*/
private GoogleApiClient client;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//查找ID
open = (Button) findViewById(R.id.open);
close = (Button) findViewById(R.id.close);
shaomiao = (Button) findViewById(R.id.shaomiao);
info = (Button) findViewById(R.id.info);
gone = (Button) findViewById(R.id.gone);
tv = (TextView) findViewById(R.id.tv);
//得到管理器
wm = (WifiManager) this.getSystemService(Context.WIFI_SERVICE);
//打开wifi
open.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//如果WiFi没打开,则设置打开WiFi
if (!wm.isWifiEnabled()) {
wm.setWifiEnabled(true);
}
}
});
//关闭wifi
close.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//如果WiFi打开,则设置关闭WiFi
if (wm.isWifiEnabled()) {
wm.setWifiEnabled(false);
}
}
});
//扫描wifi
shaomiao.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//开始扫描
wm.startScan();
//得到扫描的结果
List<ScanResult> scanResults = wm.getScanResults();
StringBuffer sb = new StringBuffer();
for (ScanResult sr:scanResults){
sb.append(sr.BSSID+",,"+sr.SSID+",,"+"\r\n");
}
tv.setText(scanResults.toString());
}
});
//得到链接WiFi的信息
info.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
WifiInfo connectionInfo = wm.getConnectionInfo();
String bssid = connectionInfo.getBSSID();
int rssi = connectionInfo.getRssi();
int ipAddress = connectionInfo.getIpAddress();
String ssid = connectionInfo.getSSID();
tv.setText(bssid+"\n"+rssi+"\n"+ipAddress+"\n"+ssid);
}
});
//获取以前链接过的WiFi的信息
gone.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
List<WifiConfiguration> configuredNetworks = wm.getConfiguredNetworks();
StringBuffer sb = new StringBuffer();
for (WifiConfiguration cw:configuredNetworks){
sb.append(cw.BSSID+",,"+cw.SSID+",,"+cw.status);
}
tv.setText(sb.toString());
}
});
// ATTENTION: This was auto-generated to implement the App Indexing API.
// See https://g.co/AppIndexing/AndroidStudio for more information.
client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();
}
/**
* ATTENTION: This was auto-generated to implement the App Indexing API.
* See https://g.co/AppIndexing/AndroidStudio for more information.
*/
public Action getIndexApiAction() {
Thing object = new Thing.Builder()
.setName("Main Page") // TODO: Define a title for the content shown.
// TODO: Make sure this auto-generated URL is correct.
.setUrl(Uri.parse("http://[ENTER-YOUR-URL-HERE]"))
.build();
return new Action.Builder(Action.TYPE_VIEW)
.setObject(object)
.setActionStatus(Action.STATUS_TYPE_COMPLETED)
.build();
}
@Override
public void onStart() {
super.onStart();
// ATTENTION: This was auto-generated to implement the App Indexing API.
// See https://g.co/AppIndexing/AndroidStudio for more information.
client.connect();
AppIndex.AppIndexApi.start(client, getIndexApiAction());
}
@Override
public void onStop() {
super.onStop();
// ATTENTION: This was auto-generated to implement the App Indexing API.
// See https://g.co/AppIndexing/AndroidStudio for more information.
AppIndex.AppIndexApi.end(client, getIndexApiAction());
client.disconnect();
}
}
安卓的WIFI
最新推荐文章于 2022-10-13 07:06:12 发布