Reputation: 63
I'm developing an app which connect to Wifi automatically. You will find my code below, which works well !
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
boutonConnecter.setOnClickListener {
val wifiNetworkSpecifier = WifiNetworkSpecifier.Builder()
.setSsid("MYSSID")
.setWpa2Passphrase("MyPassphrase")
.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()
)
}
}
But it seems that with Android Q (android 10), before connecting, the app needs the user approval ! It opens a pop-up and the user needs to select the network (approval).
Is it possible to "bypass" the user approval ? I'd like to connect directly to wifi without asking the user's approval...
Is it possible ?
Upvotes: 3
Views: 1131
Reputation: 9294
Actually, there is a way to bypass the user approval process, but it requires that you specify an exact SSID and BSSID in your specifier. If you know the BSSID (usually the mac address) of the device you can add that to the specifier and that will allow you to connect without user approval. The app will bring up a dialog showing the connection but it should automatically complete without the user having to press anything to do so.
val wifiNetworkSpecifier = WifiNetworkSpecifier.Builder()
.setSsid("MYSSID")
.setWpa2Passphrase("MyPassphrase")
.setBssid(MacAddress.fromString(bssid))
.build()
Upvotes: 4
Reputation:
No, It is not possible from Android 10. This change was bought to Android 10 for user privacy. See official documentation here : https://developer.android.com/about/versions/10/privacy/changes#enable-disable-wifi
Upvotes: 6