编程实现 Android 手机上 WiFi 操作,实现WiFi 管理界面及功能
时间: 2024-05-16 16:16:58 浏览: 136
要实现 Android 手机上 WiFi 操作,首先需要在 Android 应用程序中添加 WiFi 权限:
```xml
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
```
然后,可以通过以下代码获取 WiFiManager 对象:
```java
WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
```
使用 WiFiManager 对象可以完成 WiFi 相关的操作,例如获取当前连接的 WiFi 热点信息:
```java
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
String ssid = wifiInfo.getSSID();
int signalStrength = wifiInfo.getRssi();
```
还可以获取当前可用的 WiFi 热点列表:
```java
List<ScanResult> scanResults = wifiManager.getScanResults();
```
接下来,可以创建一个 WiFi 管理界面,显示当前连接的 WiFi 热点信息和可用的 WiFi 热点列表,并提供连接、断开连接等操作。
具体实现可以参考以下代码示例:
```java
public class WifiActivity extends Activity {
private WifiManager wifiManager;
private ListView listView;
private List<ScanResult> scanResults;
private ScanResultAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_wifi);
wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
listView = findViewById(R.id.list_view);
adapter = new ScanResultAdapter(this);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
ScanResult scanResult = scanResults.get(position);
WifiConfiguration wifiConfiguration = new WifiConfiguration();
wifiConfiguration.SSID = "\"" + scanResult.SSID + "\"";
wifiConfiguration.preSharedKey = "\"" + "password" + "\"";
int networkId = wifiManager.addNetwork(wifiConfiguration);
wifiManager.disconnect();
wifiManager.enableNetwork(networkId, true);
wifiManager.reconnect();
}
});
Button connectButton = findViewById(R.id.connect_button);
connectButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
EditText ssidEditText = findViewById(R.id.ssid_edit_text);
EditText passwordEditText = findViewById(R.id.password_edit_text);
String ssid = ssidEditText.getText().toString();
String password = passwordEditText.getText().toString();
WifiConfiguration wifiConfiguration = new WifiConfiguration();
wifiConfiguration.SSID = "\"" + ssid + "\"";
wifiConfiguration.preSharedKey = "\"" + password + "\"";
int networkId = wifiManager.addNetwork(wifiConfiguration);
wifiManager.disconnect();
wifiManager.enableNetwork(networkId, true);
wifiManager.reconnect();
}
});
Button disconnectButton = findViewById(R.id.disconnect_button);
disconnectButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
wifiManager.disconnect();
}
});
Button refreshButton = findViewById(R.id.refresh_button);
refreshButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
scanResults = wifiManager.getScanResults();
adapter.setScanResults(scanResults);
adapter.notifyDataSetChanged();
}
});
}
private static class ScanResultAdapter extends BaseAdapter {
private Context context;
private List<ScanResult> scanResults;
public ScanResultAdapter(Context context) {
this.context = context;
scanResults = new ArrayList<>();
}
public void setScanResults(List<ScanResult> scanResults) {
this.scanResults = scanResults;
}
@Override
public int getCount() {
return scanResults.size();
}
@Override
public Object getItem(int position) {
return scanResults.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = LayoutInflater.from(context).inflate(R.layout.item_scan_result, parent, false);
}
TextView ssidTextView = convertView.findViewById(R.id.ssid_text_view);
TextView signalStrengthTextView = convertView.findViewById(R.id.signal_strength_text_view);
ScanResult scanResult = scanResults.get(position);
ssidTextView.setText(scanResult.SSID);
signalStrengthTextView.setText(String.valueOf(scanResult.level));
return convertView;
}
}
}
```
在 layout 目录下创建 activity_wifi.xml 文件,定义 WiFi 管理界面的布局:
```xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<EditText
android:id="@+id/ssid_edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="SSID"/>
<EditText
android:id="@+id/password_edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Password"/>
<Button
android:id="@+id/connect_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Connect"/>
<Button
android:id="@+id/disconnect_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Disconnect"/>
<Button
android:id="@+id/refresh_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Refresh"/>
<ListView
android:id="@+id/list_view"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
```
在 layout 目录下创建 item_scan_result.xml 文件,定义 WiFi 热点列表项的布局:
```xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/ssid_text_view"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="SSID"/>
<TextView
android:id="@+id/signal_strength_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Signal Strength"/>
</LinearLayout>
```
这样就完成了 Android 手机上 WiFi 操作的实现。
阅读全文