In Android, while connecting to a wifi connected thing, and with a programmatic connection to this AP, I can't achieve to have a setOnUDPResponseResultListener call, while I have it when I connect "manually" from android preferences.
My UDP response listener:
private void setUDPAccept() {
mSocketManager.setUDPAccept(UDP_PORT, UDP_BROADCAST);
mSocketManager.setOnUDPResponseResultListener(new OnUDPResponseResultListener() {
@Override
public void onUDPResponseResult(String udpIP, String udpPort) {
Log.d(TAG, "onUDPResponseResult");
deviceIP = udpIP;
}
});
}
Way to connect to AP programmatically:
val connectivityManager = mContext.applicationContext.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
val specifier = WifiNetworkSpecifier.Builder().setBssid(MacAddress.fromString(data.getBssid()!!)).build()
val networkRequest = NetworkRequest.Builder()
.addTransportType(NetworkCapabilities.TRANSPORT_WIFI)
.removeCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET) // as not internet connection is required for this device
.setNetworkSpecifier(specifier)
.build()
val networkCallback = object : ConnectivityManager.NetworkCallback() {
override fun onAvailable(network: Network) {
super.onAvailable(network)
Log.d(TAG, "connect to WiFi success. Network is available.")
setUDPAccept()
}
override fun onUnavailable() {
super.onUnavailable()
Log.d(TAG, "connect to WiFi failed. Network is unavailable")
}
}
connectivityManager.requestNetwork(networkRequest, networkCallback)
Do you have any idea on how to make the setOnUDPResponseResultListener triggered even if the wifi AP is programmatically connected?
from Android onUDPResponseResult triggered only with a manual wifi connection
No comments:
Post a Comment