Android Question Recommended target sdk version stops working

Nitin Joshi

Active Member
Licensed User
Longtime User
Hi,
In my project's Manifest Editor 26 is target sdk version.
Manifest Editor...
AddManifestText(
<uses-sdk android:minSdkVersion="5" android:targetSdkVersion="26"/>

Recommendation by B4A is 29. (From Log)

My APP includes a button to connect to local WiFi. When targetSdkVersion is 26, upon pressing Connect button APP can connects to WiFi, however, when i change 26 to 29 I it stops working.

Can anyone help to explain the reason?
 
Dear Erel,
Let me elaborate more. Please refer attached screenshot of Manifest Editor. I am able to connect WiFi (code as below) when targetSdkVersion in Manifest editor is 26 (circled blue), however, when i change targetSdkVersion to 29 (recommendation by B4A in Logs, another screenshot is attached), i am unable to connect to WiFi.

In fact, when "Connect" button is pressed, as good as nothing happens.

Hope, this information is enough to know my query. Please feel free to let me know if more information is required.

Appreciate your kind explanation.

[CODE lang="b4x" title="WiFi Connect Activity after pressing Connect Button"]
Sub btnConnect_Click
Dim btnConnectHgt As Int
btnConnectHgt=btnConnect.Height
btnConnect.Height=btnConnectHgt * 0.9
If WiFi.isWifiEnabled=False Then
WiFi.EnableWifi(True)
End If
'Sleep(200)
WiFi.disconnectWifiAP
btnConnect.Height=btnConnectHgt
WiFi.saveWifiAP("Smart_Pump",2,"76810812",False)
If connectionSuccess=False Then
Dim i As Int
i=1
Do While i<10
WiFi.connectWifiAP("Smart_Pump")
i=i+1
Loop

Else if connectionSuccess=True And DTS=2 Then
Msgbox ("Already connected.","")
End If
End Sub[/CODE]
 

Attachments

  • Manifest Editor recommendation.JPG
    Manifest Editor recommendation.JPG
    24.2 KB · Views: 456
  • Manifest Editor.JPG
    Manifest Editor.JPG
    49.8 KB · Views: 475
Upvote 0
Sorry for not having enough information. In fact, I was not sure pre-requisite requirement to support my query.
Here are details as you have highlighted in your reply.

ESP8266 works as AP, creating hot spot.
APP is using ML WiFi library.
CONNECT button allows APP to connect ESP8266's hot spot. Upon pressing CONNECT button, APP should disconnect currently connected SSID and should connect to new SSID of ESP8266.

If I am missing any information then sorry for that. Please let me know what is needed additional. Highly appreciate your support, thank you.
 
Upvote 0
Hi Nitin,

One question: YOUR application is or will it be published in the Playstore?

YES: You have to solve the current problem, maybe it is related to permissions or Android WiFiManager, put some "LOG" in the statements to watch the result

No: Forget the problem ... your application will continue to work fine.

Regards,

Edgar
 
Upvote 0
Dear Edgar,
Hope I typed your name correctly. Thanks for your feedback. Please refer below comment from me.

I am not going to upload app on play store but in future I may. Yet, for my curiosity, I would like to comment on both, YES and NO.

YES: B4A does not generate any error logs. So, I believe error is not thrown by APP and No error is occuring.

No: Do you suggest to continue with SDK version 26 in Manifest Editor? No need to change to 29?

[One more observation I would like to state here. While APP connects to ESP8266 SSID, I can see on my mobile that WiFi is Off and again ON (toggle movement) however when SDH version is set to 29, nothing happens.]
 
Upvote 0
Dear Edgar,
Hope I typed your name correctly. Thanks for your feedback.

I am not going to upload app on play store but in future I may. Yet, for my curiosity, I would like to comment on both, YES and NO.

YES: B4A does not generate any error logs. So, I believe error is not thrown by APP and No error is occuring.

No: Do you suggest to continue with SDK version 26 in Manifest Editor? No need to change to 29?

[One more observation I would like to state here. While APP connects to ESP8266 SSID, I can see on my mobile that WiFi is Off and again ON (toggle movement) however when SDH version is set to 29, nothing happens.]

Hi Nitin,

Curiosity is the main characteristic of a creative mind.

Not throwing an error, it doesn't mean it works fine.

The "LOG" help me to identify where it stops working well.

About SDK version 26... I have few applications that work for businesses ... and everything works fine (despite the annoying message that the application was developed for an old operating system).

About your observation... I figure that your problem is related as @agraham comment: " Android WiFiManager class seems to have changed a lot in API 28 and 29 which is probably the reason for your failure".

I recommend doing a small project where you can further investigate the WiFiManager library.

Regards,

Edgar
 
Upvote 0
In both scenarios, 26 and 29 in Manifest Editor, Boolean value is false however when value is 26, WiFi connection is successful and when value is 29, can not connect.
 
Upvote 0
Feels like a permissions request thing. Is there no response value to indicate this?

You can check this in the Tabs in the IDE.
 
Upvote 0
Probably this, the WiFI library uses this in connectWifiAP. See the Compatibility Note
getConfiguredNetworks()

This method was deprecated in API level 29.
a) See WifiNetworkSpecifier.Builder#build() for new mechanism to trigger connection to a Wi-Fi network.
b) See addNetworkSuggestions(java.util.List), removeNetworkSuggestions(java.util.List) for new API to add Wi-Fi networks for consideration when auto-connecting to wifi.

Compatibility Note: For applications targeting Build.VERSION_CODES.Q or above, this API will always fail and return an empty list.
The WiFi library will need a rewrite - in fact it could do with a rewrite anyway. Too many failure paths in some methods return False with no indication of what the problem was.
 
Upvote 0
I reported a similar (possibly the same) issue with this library over 18 months ago (https://www.b4x.com/android/forum/t...-not-working-correctly-with-android-9.102734/) & as far as I can tell, the issue still isn't resolved. In looking at it again this morning, there seems to be 2 issues:

1) With targetSdkVersion=26, the call to saveWifiAP always returns false (even though it does appear to save the AP), so as a result, it doesn't try to connect. You can work around this by adding a call to connectWifiAP directly after.

2) With targetSdkVersion=29, in addition to 1) above, the call to disconnectWifiAP always returns false (ie: fails), so trying to connect after that will also fail.

As @agraham says, this library was always problematic, but now seems to have fallen behind Android SDK updates - so it definitely needs looking at.

On the plus side, if you manually switch your device to the required AP, the library will connect to it.

- Colin.
 
Upvote 0
FWIW - here is the Kotlin code that will work on Android Q+ devices:

Code:
@RequiresApi(Build.VERSION_CODES.Q)
public fun connectToWifi(ssid: String, password: String, context: Context) {
    val specifier = WifiNetworkSpecifier.Builder()
        .setSsid(ssid)
        .setWpa2Passphrase(password)
        .build()

    val request = NetworkRequest.Builder()
        .addTransportType(NetworkCapabilities.TRANSPORT_WIFI)
        .setNetworkSpecifier(specifier)
        .build()

    val cm = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
    val cb = object : ConnectivityManager.NetworkCallback() {
        override fun onAvailable(network: Network) {
            super.onAvailable(network)
            Log.d("WIFIHELPER", "Connected")
        }
    }
    cm.requestNetwork(request, cb)
}

I took a look at the MLWifi library code with a view to getting it working (using Java of course), but the only version that seems to be available is very old & has been updated significantly by @bgsoft since then - so there is a lot missing from it. If I had the source for the current release, I think I could have a pretty good go at getting it to work correctly for Q+.

- Colin.
 
Upvote 0
Back
Top