wifi自动连接,断开连接,打开和关闭,亲测有效
step1: 导入依赖 D:\workspace\WifiTwo\app\build.gradle
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'com.google.android.material:material:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
testImplementation 'junit:junit:4.+'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
//wifi
implementation 'com.thanosfisherman.wifiutils:wifiutils:1.6.4'
implementation 'com.thanosfisherman.elvis:elvis:3.0'
step2:清单文件 D:\workspace\WifiTwo\app\src\main\AndroidManifest.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_FINE_LOCATION" />
step3: 布局 D:\workspace\WifiTwo\app\src\main\res\layout\activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
tools:context=".MainActivity">
<Button
android:id="@+id/btn_open"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="12dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:text="打开" />
<Button
android:id="@+id/btn_close"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="12dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:text="关闭" />
<Button
android:id="@+id/btn_scan"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="12dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:text="扫描wifi" />
<Button
android:id="@+id/btn_wifiConnect"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="12dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:text="新方法连接wifi" />
<Button
android:id="@+id/btn_cancelWifiConnect"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding