Android kotlin jumps to the mobile hotspot switch page and determines whether the hotspot is turned on

Insert image description here

Determine whether the hotspot is open

There are many methods online. I used the getWifiApState method that uses reflection through WifiManager to obtain the judgment. It is available for personal testing. Recently, I have become obsessed with the extension method [dog].

fun Activity.isWifiApOpen(): Boolean {
    
    
    try {
    
    
        val manager = this.getSystemService(Context.WIFI_SERVICE) as WifiManager
        //通过放射获取 getWifiApState()方法
        val method = manager.javaClass.getDeclaredMethod("getWifiApState")
        //调用getWifiApState() ,获取返回值
        val state = method.invoke(manager) as Int
        //通过放射获取 WIFI_AP的开启状态属性
        val field: Field = manager.javaClass.getDeclaredField("WIFI_AP_STATE_ENABLED")
        //获取属性值
        val value = field.get(manager) as Int
        //判断是否开启
        return state == value
    } catch (e: NoSuchMethodException) {
    
    
        e.printStackTrace()
    } catch (e: IllegalAccessException) {
    
    
        e.printStackTrace()
    } catch (e: InvocationTargetException) {
    
    
        e.printStackTrace()
    } catch (e: NoSuchFieldException) {
    
    
        e.printStackTrace()
    }
    return false
}


Jump to mobile hotspot switch page

To jump to the mobile hotspot page, you must use intent. The key point is that I don’t know what the action is. The most common one on the Internet is Settings.ACTION_WIFI_SETTINGS to jump to the wifi settings page. It took a lot of effort and was also written as an extension method. I tried it and it worked. Jump to the hotspot setting page

It is worth noting that this activity may not be available on some models or under special circumstances, because the framework layer may change the name, so be prepared.

fun Activity.startWifiActivity(){
    
    
    val intent = Intent(Settings.ACTION_WIRELESS_SETTINGS)
    this.startActivity(intent)
}

By the way, we will introduce some other commonly used settings page jumps.

Also, this activity may not be available on some models or under special circumstances, so you have to try it yourself.

  • ACTION_SETTINGS
    jumps to the system settings page as the name suggests.

  • ACTION_APN_SETTINGS
    jump to APN setting interface

  • ACTION_LOCATION_SOURCE_SETTINGS
    Jump to location services interface (manage installed applications)

  • ACTION_AIRPLANE_MODE_SETTINGS
    Jump to the airplane mode settings page

  • ACTION_APPLICATION_DEVELOPMENT_SETTINGS
    Jump to developer options page

  • ACTION_APPLICATION_SETTINGS
    Jump to the application list interface

  • ACTION_BLUETOOTH_SETTINGS
    Jump to Bluetooth settings page

Some relevant knowledge about other hot spots

If your hotspot does not require Internet access, you can use Local-only hotspot

Local-only hotspot

Local-only hotspot By configuring a local-only hotspot, applications on devices connected to Wi-Fi hotspots can communicate with each other. Networks created this way will not be able to access the Internet. Each application can make only one request to the hotspot, but multiple applications can request the hotspot simultaneously. When multiple applications successfully register concurrently, they share the underlying hotspot. onstarted (localonlyhotspotrevation) is called when the hotspot is ready.

If our app targets Android 13 (API level 33) or higher, we must request NEARBY_WIFI_DEVICES to use local-only hotspots, as shown in the code snippet below. Apps targeting older versions of Android must request ACCESS_FINE_LOCATION.

<manifest ...>
    <<!-- If your app targets Android 13 (API level 33)
          or higher, you must declare the NEARBY_WIFI_DEVICES permission. -->
    <uses-permission android:name="android.permission.NEARBY_WIFI_DEVICES"
                     <!-- If your app derives location information from
                          Wi-Fi APIs, don't include the "usesPermissionFlags"
                          attribute. -->
                     android:usesPermissionFlags="neverForLocation" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"
                     <!-- If any feature in your app relies on
                          precise location information, don't include the
                          "maxSdkVersion" attribute. -->
                     android:maxSdkVersion="32" />
    <application ...>
        ...
    </application>
</manifest>

Corresponding method

in WifiManager

public void startLocalOnlyHotspot (WifiManager.LocalOnlyHotspotCallback callback, 
                Handler handler)

Multiple connections can be made at the same time. If the startup fails, there will be a callback in the callback.

reference

Android official website WifiManager

Guess you like

Origin blog.csdn.net/shop_and_sleep/article/details/132404271
Recommended