Is it possible to add a network configuration on Android Q?

前端 未结 4 716
难免孤独
难免孤独 2021-01-02 00:42

Background

I\'ve noticed that in WifiManager class there is a function called addNetwork, that might be useful if I want to restore or save networks information (n

4条回答
  •  孤独总比滥情好
    2021-01-02 01:16

    I wish I had answers to all of your questions because I'm currently struggling with similar issues.

    After many hours I was finally able to connect to the desired network using this approach:

    val wifiNetworkSpecifier = WifiNetworkSpecifier.Builder()
        .setSsid(ssid)
        .setWpa2Passphrase(passphrase)
        .setBssid(mac)
        .build()
    
    val networkRequest = NetworkRequest.Builder()
        .addTransportType(NetworkCapabilities.TRANSPORT_WIFI)
        .setNetworkSpecifier(wifiNetworkSpecifier)
        .build()
    
    val connectivityManager = applicationContext.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager?
    
    connectivityManager?.requestNetwork(networkRequest, ConnectivityManager.NetworkCallback())
    

    You can receive a whole host of events through the ConnectivityManager.NetworkCallback().

提交回复
热议问题