Wi-Fi connection without internet

I use the following code to connect to a WiFi network without an Internet connection, because it has its own web server on which I want to connect, even if the Internet connection is not available.

WifiConfiguration wifiConfiguration = new WifiConfiguration(); wifiConfiguration.SSID = String.format("\"%s\"", scanResult.SSID); wifiConfiguration.preSharedKey = String.format("\"%s\"", "secret"); int netId = wifiManager.addNetwork(wifiConfiguration) wifiManager.disconnect(); wifiManager.enableNetwork(netId, true); wifiManager.reconnect(); 

This works great on every Android Marshmallow device (am I using CM13, is CyanogenMod probably related?): When I use this code to connect, Android still uses a mobile connection and marks the WiFi symbol with an exclamation mark. After a few seconds, he shows a notification asking if I want to stay in touch, because the network does not have an Internet connection.

Problem. My application is trying to automatically connect to my web server, which is not working, because obviously there is no internet connection. Of course, one could wait until I can get to my web server, but it should work without user interaction.

How does ChromeCast or any other IoT device solve this? I never saw such a notification when I set up my ChromeCast device.

+10
source share
2 answers

I solved this by associating ( connectivityManager.bindProcessToNetwork(network); ) the current process with the connected network. This prevents the "continue to use this network" dialog and allows you to communicate with the device via Wi-Fi.

 NetworkRequest.Builder builder; if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { builder = new NetworkRequest.Builder(); //set the transport type do WIFI builder.addTransportType(NetworkCapabilities.TRANSPORT_WIFI); connectivityManager.requestNetwork(builder.build(), new ConnectivityManager.NetworkCallback() { @Override public void onAvailable(Network network) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { if (Build.VERSION.RELEASE.equalsIgnoreCase("6.0")) { if (!Settings.System.canWrite(mActivity)) { Intent goToSettings = new Intent(Settings.ACTION_MANAGE_WRITE_SETTINGS); goToSettings.setData(Uri.parse("package:" + mActivity.getPackageName())); mActivity.startActivity(goToSettings); } } connectivityManager.bindProcessToNetwork(null); if (mSsid.contains("my_iot_device-xxxxxxxxx")) { connectivityManager.bindProcessToNetwork(network); } else { } } else { //This method was deprecated in API level 23 ConnectivityManager.setProcessDefaultNetwork(null); if (mSsid.contains("my_iot_device-xxxxxxxxx")) { ConnectivityManager.setProcessDefaultNetwork(network); } else { } } try { //do a callback or something else to alert your code that it ok to send the message through socket now } catch (Exception e) { Crashlytics.logException(e); e.printStackTrace(); } connectivityManager.unregisterNetworkCallback(this); } }); } 
+8
source

Kotlin decision

 class ConnectWithoutInternetTest constructor( private val mContext: Context, private val connectivityManager: ConnectivityManager, private val wifiManager: WifiManager ) { private val mWifiBroadcastReceiver = object : BroadcastReceiver() { override fun onReceive(context: Context, intent: Intent) { when (intent.action) { WifiManager.NETWORK_STATE_CHANGED_ACTION -> { val info = intent.getParcelableExtra<NetworkInfo>(WifiManager.EXTRA_NETWORK_INFO) val isConnected = info.isConnected val ssid: String? = normalizeAndroidWifiSsid(wifiManager.connectionInfo?.ssid) if (isConnected) { val builder = NetworkRequest.Builder() builder.addTransportType(NetworkCapabilities.TRANSPORT_WIFI) connectivityManager.registerNetworkCallback( builder.build(), object : ConnectivityManager.NetworkCallback() { override fun onAvailable(network: Network) { super.onAvailable(network) val networkInfo = connectivityManager.getNetworkInfo(network) val networkSsid = networkInfo.extraInfo if (networkSsid == ssid) { connectivityManager.unregisterNetworkCallback(this) } } }) } } } } } private fun init() { val intentFilter = IntentFilter() intentFilter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION) mContext.registerReceiver(mWifiBroadcastReceiver, intentFilter) } private fun destroy() { mContext.unregisterReceiver(mWifiBroadcastReceiver) } private fun normalizeAndroidWifiSsid(ssid: String?): String? { return ssid?.replace("\"", "") ?: ssid } fun connectToWifi(ssidParam: String, password: String?) { init() val ssid = "\"$ssidParam\"" val config = wifiManager.configuredNetworks.find { it.SSID == ssid } val netId = if (config != null) { config.networkId } else { val wifiConfig = WifiConfiguration() wifiConfig.SSID = ssid password?.let { wifiConfig.preSharedKey = "\"$password\"" } wifiConfig.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE) wifiManager.addNetwork(wifiConfig) } wifiManager.disconnect() val successful = wifiManager.enableNetwork(netId, true) } 
0
source

Source: https://habr.com/ru/post/1012572/


All Articles