I am trying to send telementary data to App Center on out internal wifi network but it is not sending on this network but it does on any external network. When debugging found that Method onAvailable() is not called when device is connected to internal wifi but it does get called when connected to any external wifi.
Below code is from App Center SDK :
appcenter\utils\NetworkStateHelper.javaNetworkStateHelper.java.
Class NetworkStateHelper -> Method reopen() --> public void onAvailable(Network network) method
Sample Code:
private ConnectivityManager.NetworkCallback mNetworkCallback = new ConnectivityManager.NetworkCallback()
{
@Override
public void onAvailable(Network network) {
onNetworkAvailable(network);
}
@Override
public void onLost(Network network) {
onNetworkLost(network);
}
};
It should call the onAvailable method when connected to an internal wifi network.
You have to restrict the network type to Wifi.
1. Build the NetworkRequest
:
val networkRequest = getNetworkRequest()
private fun getNetworkRequest(): NetworkRequest {
return NetworkRequest.Builder()
.addTransportType(NetworkCapabilities.TRANSPORT_WIFI) //here!!
.build()
}
2. Build the NetworkCallback
:
val networkCallback = getNetworkCallBack()
private fun getNetworkCallBack(): ConnectivityManager.NetworkCallback {
return object : ConnectivityManager.NetworkCallback() {
override fun onAvailable(network: Network) { //when Wifi is on
super.onAvailable(network)
Toast.makeText(requireContext(), "Wifi is on!", Toast.LENGTH_SHORT).show()
}
override fun onLost(network: Network) { //when Wifi 【turns off】
super.onLost(network)
Toast.makeText(requireContext(), "Wifi turns off!", Toast.LENGTH_SHORT).show()
}
}
}
3. Register and Unregister:
fun getConnectivityManager() = requireContext().getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
override fun onResume() { //start monitoring when in the foreground
super.onResume()
getConnectivityManager().registerNetworkCallback(networkRequest, networkCallback)
}
override fun onPause() { //stop monitoring when not fully visible
super.onPause()
getConnectivityManager().unregisterNetworkCallback(networkCallback)
}
4. Don't forget the permission:
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
Demo: https://youtu.be/RPRcGbuB6eI
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With