i'm trying to check the connectivity and do some actions in each state but when i turn of the connection in my mobile the app crash and it give me this error 'cnxManager .activeNetworkInfo must not be null'
My code
private fun isConnected(webView: WebView){
val cnxManager : ConnectivityManager = baseContext.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
val netInfo : NetworkInfo = cnxManager.activeNetworkInfo
if (netInfo.isConnected && netInfo.isConnectedOrConnecting){
cnx_failed.visibility=View.INVISIBLE
webView.visibility=View.VISIBLE
}else{
webView.visibility=View.INVISIBLE
cnx_failed.visibility=View.VISIBLE
}
}
This is due to what is called a "platform type" in Kotlin. Since getActiveNetworkInfo() is not annotated as #Nullable or #NonNull you can choose to declare it as either type. So both:
val netInfo: NetworkInfo = cnxManager.activeNetworkInfo
and
val netInfo: NetworkInfo? = cnxManager.activeNetworkInfo
are valid declarations. However, as Markus mentioned this method can return null, so you should declare it as the latter (nullable type). Any time you're doing interop with Java platform types, you have to be sure you declare it correctly.
With this in mind, you could rewrite your code as:
private fun updateConnectionStatus(webView: WebView) {
val cnxManager = baseContext.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
// Declare as a nullable type
val netInfo : NetworkInfo? = cnxManager.activeNetworkInfo
// Safe check -- assigns to false if netInfo is null
val connected = netInfo?.isConnectedOrConnecting ?: false
cnx_failed.visibility = if (connected) View.INVISIBLE else View.VISIBLE
webView.visibility = if (connected) View.VISIBLE else View.INVISIBLE
}
Related
I need to check if the device have internet connection, I search for some examples but when i copy and paste the code i always get errors or deprecated function. I also not understand where I have to put the method that check the connection, because I need to check the internet connection in the viewModel to make some request, and all the methods that i found have Context in the parameters, and I can't get Context in viewModel.
I try this code but I don't understand where I have to put it and I get
'TYPE_WIFI, TYPE_MOBILE, TYPE_ETHERNET: Int' is deprecated. Deprecated in Java
private fun isInternetAvailable(context: Context): Boolean {
var result = false
val connectivityManager =
context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
val networkCapabilities = connectivityManager.activeNetwork ?: return false
val actNw =
connectivityManager.getNetworkCapabilities(networkCapabilities) ?: return false
result = when {
actNw.hasTransport(NetworkCapabilities.TRANSPORT_WIFI) -> true
actNw.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR) -> true
actNw.hasTransport(NetworkCapabilities.TRANSPORT_ETHERNET) -> true
else -> false
}
} else {
connectivityManager.run {
connectivityManager.activeNetworkInfo?.run {
result = when (type) {
ConnectivityManager.TYPE_WIFI -> true
ConnectivityManager.TYPE_MOBILE -> true
ConnectivityManager.TYPE_ETHERNET -> true
else -> false
}
}
}
}
return result
}
Someone can explain me how to make this check?
I created a helper class.
Network.kt
object Network {
private const val NETWORK_STATUS_NOT_CONNECTED = 0
private const val NETWORK_STATUS_WIFI = 1
private const val NETWORK_STATUS_MOBILE = 2
private const val TYPE_WIFI = 1
private const val TYPE_MOBILE = 2
private const val TYPE_NOT_CONNECTED = 0
private fun connectivityStatus(context: Context): Int {
val connectivityManager = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
val activeNetwork = connectivityManager.activeNetworkInfo
if (null != activeNetwork) {
if (activeNetwork.type == ConnectivityManager.TYPE_WIFI) return TYPE_WIFI
if (activeNetwork.type == ConnectivityManager.TYPE_MOBILE) return TYPE_MOBILE
}
return TYPE_NOT_CONNECTED
}
private fun connectivityStatusString(context: Context): Int {
val connection = connectivityStatus(context)
var status = -1
if (connection == TYPE_WIFI) status = NETWORK_STATUS_WIFI else if (connection == TYPE_MOBILE) status = NETWORK_STATUS_MOBILE else if (connection == TYPE_NOT_CONNECTED) status = NETWORK_STATUS_NOT_CONNECTED
return status
}
fun checkConnectivity(context : Context):Boolean{
val status = connectivityStatusString(context)
return status == NETWORK_STATUS_WIFI || status == NETWORK_STATUS_MOBILE
}
}
and to access it you need to use it like this
if (Network.checkConnectivity(this#MainActivity))
\\Internet is working
else
\\No internet connectivity
You have to extend AndroidViewModel() class. After that you can reach application context in your viewModel.
class viewModel(app: Application): AndroidViewModel(app) {}
My application seems to keep crashing giving me an E/AndroidRuntime: FATAL EXCEPTION: main error when i try to make a Get request to a server and there is no internet. I expected the app to run but no data be displayed.
Log.i("getStoreData()" , "Inside the coroutine before getData")
this is the last log that I have put myself gets printed before the app crashes.
private fun getStoreData() {
Log.i("getStoreData()", " inside getStoreData")
val job = coroutineScope.launch {
Log.i("getStoreData()" , "Inside the coroutine before getData")
var data = StoreAPI.retrofitService.getData()
Log.i("getStoreData()" , "Inside the coroutine after getData")
try {
var storeData = data.stores
_status.value = "Success: ${storeData.size} Stores received"
if(storeData.size > 0){
_stores.value = storeData
}
} catch (t: Throwable) {
Log.i("Retrofit catch block", _status.value)
_status.value = "Failure: " + t.message
t.printStackTrace()
}
}
}
StoreAPIService.kt
private const val URL = "http://sandbox.bottlerocketapps.com/BR_Android_CodingExam_2015_Server/"
private val moshi = Moshi.Builder().add(KotlinJsonAdapterFactory()).build()
private val retrofit = Retrofit.Builder()
.addConverterFactory(MoshiConverterFactory.create(moshi))
.baseUrl(URL)
.build()
interface StoreAPIService{
//Initially was using Jake Wharton's library for retrofit2 kotlin coroutines support but it has been deprecated since the support
// addition of the suspend keyword in retrofit 2.6.0
//Suspend does all the task of coroutines for us by just adding it before the function declaration
#GET("stores.json")
suspend fun getData():
Data //return Data object because Data has access to the Store JSON Object/Array
}
object StoreAPI{
val retrofitService: StoreAPIService by lazy {
retrofit.create(StoreAPIService::class.java)
}
}
Any idea why?
EDIT:
I cannot use these network connectivity functions because I my fragment is not connected to any activity and the fragment is connected to a viewModel. Therefore this line of code doesn't work as there is no context to bound it to. If you have a work around for this that would be great too.
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NB: before making any Network call or sending any requesting you must ensure that the device is connected to internet. I entice you to write a simple function to check if you're connected, if you're connected then you can send the request or make a network call.
Try using this
Create Class For NetworkConnectionDetection
Manifest
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
NetworkConnection Class
class NetworkConnection(val context: Context) : LiveData<Boolean>() {
var connectionManger: ConnectivityManager =
context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
lateinit var netwrokCallback: ConnectivityManager.NetworkCallback
override fun onActive() {
super.onActive()
updateConnection()
when {
Build.VERSION.SDK_INT >= Build.VERSION_CODES.N -> {
connectionManger.registerDefaultNetworkCallback(NetworkConnectioncallback())
}
Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP -> {
lollipopNetworkRequest()
}
else -> {
context.registerReceiver(
networkReciever(),
IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION)
)
}
}
}
/*override fun onInactive() {
super.onInactive()
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
connectionManger.unregisterNetworkCallback(NetworkConnectioncallback())
} else {
context.unregisterReceiver(networkReciever())
}
}*/
#TargetApi(Build.VERSION_CODES.LOLLIPOP)
fun lollipopNetworkRequest() {
val requestBuilder = NetworkRequest.Builder()
.addTransportType(NetworkCapabilities.TRANSPORT_CELLULAR)
.addTransportType(NetworkCapabilities.TRANSPORT_WIFI)
.addTransportType(NetworkCapabilities.TRANSPORT_ETHERNET)
connectionManger.registerNetworkCallback(
requestBuilder.build(),
NetworkConnectioncallback()
)
}
fun NetworkConnectioncallback(): ConnectivityManager.NetworkCallback {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
netwrokCallback = object : ConnectivityManager.NetworkCallback() {
override fun onLost(network: Network) {
super.onLost(network)
postValue(false)
}
override fun onAvailable(network: Network) {
super.onAvailable(network)
postValue(true)
}
}
return netwrokCallback
} else {
throw IllegalAccessError("Error!")
}
}
fun networkReciever() = object : BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent?) {
updateConnection()
}
}
fun updateConnection() {
val activeNetwork: NetworkInfo? = connectionManger.activeNetworkInfo
postValue((activeNetwork?.isConnected == true))
}
}
Now Inside your Activity/Fragment Check the connection either it is connected or not. Here is how it will be achieved
val networkConnection = NetworkConnection(requireContext())
networkConnection.observe(viewLifecycleOwner, { isConnected ->
if (isConnected) {
// Do what ever you want to do
} else {
// Show No internet connection message
}
})
You need to add internet checks before calling your retrofit service because to get some data from server, internet connectivity is mandatory
This method checks whether mobile is connected to internet and returns true
if connected:
private boolean isNetworkConnected() {
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
return cm.getActiveNetworkInfo() != null && cm.getActiveNetworkInfo().isConnected();
}
in manifest,
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
Edit: This method actually checks if device is connected to internet(There is
a possibility it's connected to a network but not to internet).
public boolean isInternetAvailable() {
try {
InetAddress ipAddr = InetAddress.getByName("google.com");
//You can replace it with your name
return !ipAddr.equals("");
}catch (Exception e) {
return false;
}
}
This will tell you if you're connected to a network:
boolean connected = false;
ConnectivityManager connectivityManager = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
if(connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState() == NetworkInfo.State.CONNECTED ||
connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState() == NetworkInfo.State.CONNECTED) {
//we are connected to a network
connected = true;
}
else
connected = false;
I also followed that tutorial, but I don't remember it having an offline mode. That is an option that you have to integrate on yourself.
When you create the viewModel, because it has an init block, it makes the call to the API and if you don't have an Internet connection, it crashes.
So you should write the init viewModel some code that checks whether you have an Internet connection or not. Or in the method that makes the API call to get the data.
In the next lesson from that tutorial, "Behind the scenes", they talk about offline mode.
I have tried so many solutions to get to the answer but nothing helped,
I want your help.
I am developing android app which targets the minimum sdk of (4.4.4)
and when I am checking the status of the connection it's not working because of deprecated functions.
On the official site of android developers the solution is the same and I could not fix my problem.
Any help ( with code if it is possible )
Thanks.
private fun isOnLine(): Boolean {
val connectivityManager = appContext.getSystemService(Context.CONNECTIVITY_SERVICE)
as ConnectivityManager
val networkInfo = connectivityManager.activeNetworkInfo
return networkInfo != null && networkInfo.isConnected
}
You can use ConnectivityManager if your app targets api 24 or later.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
val connectivityManager = it.getSystemService(Context.CONNECTIVITY_SERVICE) as
ConnectivityManager
connectivityManager.registerDefaultNetworkCallback(object : NetworkCallback() {
override fun onAvailable(network: Network) {
//take action when network connection is gained
}
override fun onLost(network: Network?) {
//take action when network connection is lost
}
})
}
This is good enough:
final ConnectivityManager connectivity = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
final boolean isNetworkAvailable = connectivity.getActiveNetwork() == null;
I have written some code to enable a network with the given networkId, and to handle the asynchronous response via a BroadcastReceiver. However, even though enableNetwork returns true (indicating the OS successfully issue the command) my BroadcastReceiver never receives a NetworkInfo with CONNECTED state, it receives 2 events: DISCONNECTED and then DISCONNECTED/SCANNING.
From all the official docs and various SO questions I have read, if enableNetwork returns true then the BroadcastReceiver registered for handling NETWORK_STATE_CHANGED_ACTION intents should always receive a NetworkInfo object with state CONNECTED.
Here is the code:
/**
* Connects to the wifi access point at specified [ssid] with specified [networkId]
* And returns the [WifiInfo] of the network that has been connected to
*/
private fun connect(context: Context,
wifiManager: WifiManager,
ssid: String,
networkId: Int) = Single.create<WifiInfo> { emitter ->
val wifiConnectionReceiver = object : BroadcastReceiver() {
var oldSupplicantState: SupplicantState? = null
override fun onReceive(context: Context, intent: Intent) {
if (intent.action == WifiManager.NETWORK_STATE_CHANGED_ACTION) {
val networkInfo = intent.getParcelableExtra<NetworkInfo>(WifiManager.EXTRA_NETWORK_INFO) ?: return
if (networkInfo.detailedState == NetworkInfo.DetailedState.DISCONNECTED) {
context.applicationContext.unregisterReceiver(this)
emitter.onError(WiFiException("Failed to connect to wifi network"))
}
else if (networkInfo.detailedState == NetworkInfo.DetailedState.CONNECTED) {
val wifiInfo = intent.getParcelableExtra<WifiInfo>(WifiManager.EXTRA_WIFI_INFO) ?: return
if (ssid == wifiInfo.ssid.unescape()) {
context.applicationContext.unregisterReceiver(this)
emitter.onSuccess(wifiInfo)
}
}
} else if (intent.action == WifiManager.SUPPLICANT_STATE_CHANGED_ACTION) {
val supplicantState = intent.getParcelableExtra<SupplicantState>(WifiManager.EXTRA_NEW_STATE)
val oldSupplicantState = this.oldSupplicantState
this.oldSupplicantState = supplicantState
if (supplicantState == SupplicantState.DISCONNECTED) {
if (oldSupplicantState == null || oldSupplicantState == SupplicantState.COMPLETED) {
return
}
val possibleError = intent.getIntExtra(WifiManager.EXTRA_SUPPLICANT_ERROR, -1)
if (possibleError == WifiManager.ERROR_AUTHENTICATING) {
context.applicationContext.unregisterReceiver(this)
emitter.onError(WiFiException("Wifi authentication failed"))
}
} else if (supplicantState == SupplicantState.SCANNING && oldSupplicantState == SupplicantState.DISCONNECTED) {
context.applicationContext.unregisterReceiver(this)
emitter.onError(WiFiException("Failed to connect to wifi network"))
}
}
}
}
val networkStateChangedFilter = IntentFilter(WifiManager.NETWORK_STATE_CHANGED_ACTION)
networkStateChangedFilter.addAction(WifiManager.SUPPLICANT_STATE_CHANGED_ACTION)
context.applicationContext.registerReceiver(wifiConnectionReceiver, networkStateChangedFilter)
emitter.setCancellable {
if (!emitter.isDisposed)
context.applicationContext.unregisterReceiver(wifiConnectionReceiver)
}
wifiManager.enableNetwork(networkId, true)
}
Can anyone help? I'm really stumped. The networkId I am passing is valid as it has been created from addNetwork which is succeeding as it's not returning -1.
Ok, I've finally figured this out and I hope that my answer here sheds some light for anyone in the future who encounters a similar problem, because this was nasty and caused me quite the headache.
The code in my question wasn't completely correct, but, it also wasn't the root cause of my problem. The root cause of the issue was that I had incorrectly configure the WiFiConfig object which was registered in the WiFiConfig table via WiFiConfigManager.addNetwork().
I had made a massive assumption about the contract of WifiConfigManager.addNetwork(). I had assumed that if that operation succeeded (i.e. did NOT return -1) then the passed WiFiConfig was configured correctly. This assumption is incorrect, the allowedAuthAlgorithms, allowedProtocols, allowedKeyManagers and allowedPairwiseCipher BitSet on the WiFiConfig I was creating were incorrect, yet the call to addNetwork() succeeded. I believe this is because the call to addNetwork() does not actually do anything other than validate that the config is valid to put in the WiFiConfig table, which is quite different than validating if it is the correct config for a given WiFi access point. This is backed up by the comments in the source code for addNetwork() which do NOT state the delivery of asynchronous state like a lot of the other WiFiManager functions, indicating (to me at least) that no attempt to communicate with the access point was made by the OS as a result of calling addNetwork().
Due to a very helpful suggestion by a colleague to connect to the access point in question via the OS, and then to compare the OS created WiFiConfig object for that access point with the one generated by my own code for discrepancies I noticed that my WiFiConfig was being configured incorrectly. It was shortly after this that I resolved the original question.
Now, why was my WiFiConfig object being created incorrectly? That is because I had little knowledge of how to configure WiFi (i.e. the various terminology and the meaning behind all the protocols, algorithms and key managers). So, after reading the official docs and not gleaning much helpful information I turned to StackOverflow questions and answers and found a recurring pattern for setting the WiFiConfig up correctly, they all appeared to use BitWise operators to create an Int value which was ultimately passed to the WiFiConfig.allowedProtocols.set(), WiFiConfig.allowedPairwiseCiphers.set(), WiFiConfig.allowedKeyManagement.set() and WiFiConfig.allowedAuthAlgorithm.set() functions.
It turns out that the underlying BitSet for each of those configuration options is a data structure which maintains a dynamically resizing vector of bits, where the index of a bit in a given BitSet instance in the WiFiConfig object corresponded to the index of an element in a String array which was implicitly associated to the aforementioned BitSet within the WiFiConfig object. Therefore, if you wished to provide multiple protocols, keyManagements, pairwiseCiphers or authAlgorithms you would need to call set on the underlying BitSet, passing in the correct index which would correspond to the element of the String array which matched the chosen protocol.
After re-writing my WiFiConfig creation code, the issue resolved itself. Although there was a bug in my code in the original post which has also been fixed.
Here is the new WiFiConfig creation code:
/**
* Emits a single of the [WifiConfiguration] created from the passed [scanResult] and [preSharedKey]
*/
private fun createWifiConfiguration(scanResult: WiFiScanResult, preSharedKey: String) = Single.fromCallable<WifiConfiguration> {
val auth = scanResult.auth
val keyManagement = scanResult.keyManagement
val pairwiseCipher = scanResult.pairwiseCipher
val config = WifiConfiguration()
config.SSID = "\"" + scanResult.ssid + "\""
config.BSSID = scanResult.bssid
if (auth.contains("WPA") || auth.contains("WPA2")) {
config.allowedProtocols.set(WifiConfiguration.Protocol.WPA)
config.allowedProtocols.set(WifiConfiguration.Protocol.RSN)
}
if (auth.contains("EAP"))
config.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.LEAP)
else if (auth.contains("WPA") || auth.contains("WPA2"))
config.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN)
else if (auth.contains("WEP"))
config.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.SHARED)
if (keyManagement.contains("IEEE802.1X"))
config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.IEEE8021X)
else if (auth.contains("WPA") && keyManagement.contains("EAP"))
config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_EAP)
else if (auth.contains("WPA") && keyManagement.contains("PSK"))
config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK)
else if (auth.contains("WPA2") && keyManagement.contains("PSK"))
config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK)
if (pairwiseCipher.contains("CCMP") || pairwiseCipher.contains("TKIP")) {
config.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP)
config.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP)
}
if (preSharedKey.isNotEmpty()) {
if (auth.contains("WEP")) {
if (preSharedKey.matches("\\p{XDigit}+".toRegex())) {
config.wepKeys[0] = preSharedKey
} else {
config.wepKeys[0] = "\"" + preSharedKey + "\""
}
config.wepTxKeyIndex = 0
} else {
config.preSharedKey = "\"" + preSharedKey + "\""
}
}
config
}
And here is the new connect code:
/**
* Connects to the wifi access point at specified [ssid] with specified [networkId]
* And returns the [WifiInfo] of the network that has been connected to
*/
private fun connect(context: Context,
wifiManager: WifiManager,
ssid: String,
networkId: Int) = Single.create<WifiInfo> { emitter ->
val wifiConnectionReceiver = object : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
if (intent.action == WifiManager.NETWORK_STATE_CHANGED_ACTION) {
val networkInfo = intent.getParcelableExtra<NetworkInfo>(WifiManager.EXTRA_NETWORK_INFO) ?: return
if (networkInfo.detailedState == NetworkInfo.DetailedState.CONNECTED) {
val wifiInfo = intent.getParcelableExtra<WifiInfo>(WifiManager.EXTRA_WIFI_INFO) ?: return
if (ssid.unescape() == wifiInfo.ssid.unescape()) {
context.applicationContext.unregisterReceiver(this)
emitter.onSuccess(wifiInfo)
}
}
}
}
}
val networkStateChangedFilter = IntentFilter(WifiManager.NETWORK_STATE_CHANGED_ACTION)
networkStateChangedFilter.addAction(WifiManager.SUPPLICANT_STATE_CHANGED_ACTION)
context.applicationContext.registerReceiver(wifiConnectionReceiver, networkStateChangedFilter)
emitter.setCancellable {
if (!emitter.isDisposed)
context.applicationContext.unregisterReceiver(wifiConnectionReceiver)
}
wifiManager.enableNetwork(networkId, true)
}
I am building an android app that needs to communicate over a WiFi network that will not have any internet access. The problem is that even when the WiFi is connected android chooses to use cellular/mobile data when no connection internet is present on the wifi network.
I have read many posts on the issue many of which involve rooting the device but that is not possible with a production app (rooting devices is not an option). other solution (like my code bellow) suggest using bindProcessToNetwork() which works perfectly on my Sony Z2 but not on other devices I have tested on (all running 6.0.1)
private void bindToNetwork() {
final ConnectivityManager connectivityManager = (ConnectivityManager) mActivity.getSystemService(Context.CONNECTIVITY_SERVICE);
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) {
connectivityManager.bindProcessToNetwork(null);
if (barCodeData.getSsid().contains("ap-name")) {
connectivityManager.bindProcessToNetwork(network);
}
} else {
//This method was deprecated in API level 23
ConnectivityManager.setProcessDefaultNetwork(null);
if (barCodeData.getSsid().contains("ap-name")) {
ConnectivityManager.setProcessDefaultNetwork(network);
}
}
connectivityManager.unregisterNetworkCallback(this);
}
});
}
}
You can solve this by setting captive_portal_detection_enabled to 0 (false).
What's actually happening is that by default, everytime you connect to a wifi, the FW will test against a server (typically google) to see if it's a captive wifi (needs login). So if your wifi is not connected to google, this check will fail. After that, the device knows that wifi has no internet connection and simply will not autoconnect to it.
Setting this setting to 0 will avoid this check.
Programatically:
Settings.Global.putInt(getContentResolver(), Settings.Global.CAPTIVE_PORTAL_DETECTION_ENABLED, 0);
Edit: You may need to use the string "captive_portal_detection_enabled" directly, instead of the constant that's not visible depending on Android version.
you'd need to disable mobile data in the Settings (not certain, if this can be done programmatically, which might be a possible option) - or take out the USIM;
else the common behavior is, that it will always fall back to the best available connection (while a connection with internet gateway might be preferred, because it is used by most application).
also see this answer.
Solution on Kotlin
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)
}
You're in the right path, the solution is indeed with ConnectivityManager.bindProcessToNetwork(network). This information was posted on the Android Developers Blog in this article: Connecting your App to a Wi-Fi Device.
Looking into your code this barCodeData.getSsid() doesn't look that is getting the SSID of the currently connected wifi network. If that's the case your code will never successfully bind to the network.
Try replace your if statement
if (barCodeData.getSsid().contains("ap-name"))
With
if (getNetworkSsid(context).equals("ap-name"))
Helper method in kotlin to retrieve the SSID of the connected wifi network
private fun getNetworkSsid(context: Context?): String {
// WiFiManager must use application context (not activity context) otherwise a memory leak can occur
val mWifiManager = context?.applicationContext?.getSystemService(Context.WIFI_SERVICE) as WifiManager
val wifiInfo: WifiInfo? = mWifiManager.connectionInfo
if (wifiInfo?.supplicantState == SupplicantState.COMPLETED) {
return wifiInfo.ssid.removeSurrounding("\"")
}
return ""
}
If still doesn't work please follow my complete solution where I used the same method but with some extra checks. I tested it in the Android versions 5.1.1, 6.0, 6.0.1, 7.1.1 and 8.1.0.
You're on the right path, you only need to tweak what you have a bit. One of the main issues I see is that you unregister the network callback on onAvailable() which is not a good idea as networks tend to switch between available/unavailable from time-to-time which would cause issues on your device.
The other thing to consider is requesting a network that's Wi-Fi and clearing the other network capabilities as they may cause you issues depending on your network's setup.
Here is another version of how to do that which is hopefully a bit simpler.
final NetworkRequest requestForWifi =
new NetworkRequest.Builder()
.clearCapabilities() // We only care about Wi-Fi
.addTransportType(NetworkCapabilities.TRANSPORT_WIFI)
.build();
final ConnectivityManager connectivityManager = (ConnectivityManager)
context.getSystemService(Context.CONNECTIVITY_SERVICE);
final NetworkCallback networkCallbackWifi = new NetworkCallback() {
#Override
void onAvailable(Network network) {
// Once this callback triggers, a Wi-Fi network is available
WifiInfo wifiInfo = connectivity.getNetworkCapabilities(network).TransportInfo;
string ssid = wifiInfo.SSID.Trim(new char[] {'"', '\"' });
if (!ssid.contains("ap-name")) {
return;
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
connectivityManager.bindProcessToNetwork(network);
} else {
ConnectivityManager.setProcessDefaultNetwork(network);
}
}
};
// Last thing is to actually request a network.
connectivityManager.requestNetwork(requestForWifi, networkCallbackWifi);
You can check if wifi is connected then proceed else show a dialog to user asking him to connect to a wifi network
Since the method NetworkInfo.isConnected() is now deprecated in API-23, here is a method which detects if the Wi-Fi adapter is on and also connected to an access point using WifiManager instead:
private boolean checkWifiOnAndConnected() {
WifiManager wifiMgr = (WifiManager) getSystemService(Context.WIFI_SERVICE);
if (wifiMgr.isWifiEnabled()) { // Wi-Fi adapter is ON
WifiInfo wifiInfo = wifiMgr.getConnectionInfo();
if( wifiInfo.getNetworkId() == -1 ){
return false; // Not connected to an access point
}
return true; // Connected to an access point
}
else {
return false; // Wi-Fi adapter is OFF
}
}