Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Failure to Switch WiFi Network Using TetheringManager.connectToLocalNetwork() #23

Closed
MagnasiePro opened this issue Aug 9, 2023 · 22 comments

Comments

@MagnasiePro
Copy link

MagnasiePro commented Aug 9, 2023

Hello,

I am encountering a problem similar to the one reported in the react-native-wifi-reborn repository: Issue #303.

My current setup involves using a Google Pixel 6a. However, to eliminate device-specific factors, I plan to test this again on a different device later this week.

The core issue is as follows:

I'm utilizing the TetheringManager.connectToLocalNetwork() function. Although Android indicates a successful connection, my WiFi network does not switch over as expected. It's important to note that the WiFi network in question is not a router; rather, it's a hotspot emitted by a Linux device that lacks internet connectivity (similar to a Raspberry Pi emitting a WiFi signal without internet access).

I've ensured that my AndroidManifest.xml file includes the necessary permissions:

<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />

When attempting to connect, I encounter a location permission prompt and subsequently a network connection prompt. Following these, a small popup at the bottom confirms a "Successful connection." Despite this, I remain connected to my original router rather than the intended alternate device.

Your assistance in resolving this matter would be greatly appreciated.

Thank you.

@aboozaid
Copy link
Owner

if you're trying to connect to a local network (no internet connection network) you have to use connectToLocalNetwork function instead.

@MagnasiePro
Copy link
Author

It seems to be working when I try to connect to my router using the "connectToNetwork" function, but it's not functioning properly when attempting to connect to a local network using the "connectToLocalNetwork" function.

Therefore, it appears that my issue is specific to connecting to local networks.

@MagnasiePro MagnasiePro changed the title Failure to Switch WiFi Network Using TetheringManager.connectToNetwork() Failure to Switch WiFi Network Using TetheringManager.connectToLocalNetwork() Aug 10, 2023
@MagnasiePro
Copy link
Author

Ok I think I found something (but this is weird and I can't find any information about it).

On my Pixel 6a, I can connect to a local network, but it is not shown in my wifi settings, and if I close the app, the connexion seems to be closed.

But if I connect with the app and I retrieve the IP in the app, this is the IP from the local network, so it seems to be working.

I have to dig it more.

PS: Perfectly working on an other device that is not on Android 13 (Huawei Nova Y70)

@aboozaid
Copy link
Owner

That's the normal behavior when you use connectToLocalNetwork if you close your app the connection will be lost this is since android 10 and above If I remember correctly.

@jayadelson
Copy link

@aboozaid I tried this myself and still get the same problem on Android 13. The app, still open, doesn't fully connect to the new SSID even though it reports a successful switch.

connect responds with successful, API responds that current SSID has changed, but no http text is sent. The connection times out and returns to the previous connection.

Permissions are all consistent (usesClearText set, all AndroidManifest.xml set) and this only happen on Android 13.

If I manually switch to the new SSID, http sends fine.

@aboozaid
Copy link
Owner

@aboozaid I tried this myself and still get the same problem on Android 13. The app, still open, doesn't fully connect to the new SSID even though it reports a successful switch.

connect responds with successful, API responds that current SSID has changed, but no http text is sent. The connection times out and returns to the previous connection.

Permissions are all consistent (usesClearText set, all AndroidManifest.xml set) and this only happen on Android 13.

If I manually switch to the new SSID, http sends fine.

Which function you use to establish a connection?

@jayadelson
Copy link

jayadelson commented Oct 11, 2023

Which function you use to establish a connection?

I've tried it with both connectToNetwork and connectToLocalNetwork. connectToNetwork won't switch at all. connectToLocalNetwork will switch according to the status, but there is no sign it actually does so when you view settings, and no http traffic passes between the devices. If a manual authentication/switch happens, traffic passes fine.

@jayadelson
Copy link

@aboozaid also I should add that the AP I'm connecting to is an IoT device, which we develop, and have access to view the detailed status. It functions as a traditional AP for passing credentials. Therefore, I'm able to view as devices connect and any detailed connection issues.

Prior to Android 13, you can see the client connect and send cleartext information after the module reports a successful connection.

After Android 13, the module reports a connection, but the AP never sees the traffic.

This same problem exists with react-native-wifi-reborn.

@aboozaid
Copy link
Owner

@aboozaid also I should add that the AP I'm connecting to is an IoT device, which we develop, and have access to view the detailed status. It functions as a traditional AP for passing credentials. Therefore, I'm able to view as devices connect and any detailed connection issues.

Prior to Android 13, you can see the client connect and send cleartext information after the module reports a successful connection.

After Android 13, the module reports a connection, but the AP never sees the traffic.

This same problem exists with react-native-wifi-reborn.

Would you like to open a new issue? Did you try another device which runs Android 13? What do you think about using WiFI Direct if your IoT device supports it

@jayadelson
Copy link

@aboozaid This appears to be the exact issue as documented above. Maybe re-open this one?

Unfortunately, my IoT device doesn't yet support Wifi Direct, and we have thousands in the field. As people upgrade their phones to Android 13, they can no longer reconfigure our devices, so we're stepping them through manual procedures which is problematic.

@aboozaid
Copy link
Owner

aboozaid commented Oct 12, 2023

@aboozaid This appears to be the exact issue as documented above. Maybe re-open this one?

Unfortunately, my IoT device doesn't yet support Wifi Direct, and we have thousands in the field. As people upgrade their phones to Android 13, they can no longer reconfigure our devices, so we're stepping them through manual procedures which is problematic.

Could you please try any wifi management app on google play and see if it works well?

@jayadelson
Copy link

jayadelson commented Oct 13, 2023

@aboozaid This appears to be the exact issue as documented above. Maybe re-open this one?

Unfortunately, my IoT device doesn't yet support Wifi Direct, and we have thousands in the field. As people upgrade their phones to Android 13, they can no longer reconfigure our devices, so we're stepping them through manual procedures which is problematic.

Could you please try any wifi management app on google play and see if it works well?

If you mean native apps, yes I have confirmed I can still provision devices on Android 13 using WiFi, such as apps like SmartThings or SmartHQ. To be clear: The issue is that the module reports the new network is connected, but testing reveals it is not, as the issue subject suggests. I apologize for not being more helpful here, as I'm in the dark as to the mechanics of how the Android NetworkSpecifier or NetworkSuggestion works under the hood.

It could be related to how the devices communicate in LocalNetwork mode, an undocumented permission requirement for clear text http, but we've followed all the published examples and still cannot pass traffic.

@aboozaid aboozaid reopened this Oct 13, 2023
@aboozaid
Copy link
Owner

aboozaid commented Oct 13, 2023

Actually I do not have any Android 13 devices currently to do some testing and debugging maybe there are missing capabilities when connection occur or some errors/warning in logs which may help reaching to the problem. Could you please fork and do some testing on your side using multiple Android 13 devices?

However you can use saveNetworkInDevice in order to save that network on device's networks instead of letting the user connect manually

@aboozaid aboozaid reopened this Oct 13, 2023
@jayadelson
Copy link

Actually I do not have any Android 13 devices currently to do some testing and debugging maybe there are missing capabilities when connection occur or some errors/warning in logs which may help reaching to the problem. Could you please fork and do some testing on your side using multiple Android 13 devices?

However you can use saveNetworkInDevice in order to save that network on device's networks instead of letting the user connect manually

Understood, though for security reasons, it would be ideal not to save the network, though maybe worth trying as a temporary fix. Part of the problem is our users install multiple devices. They do have different SSIDs though.

I'll try to get some Android 13-specific logs that may be helpful for you. Unfortunately I fear there hasn't been much yet. Stay tuned.

@jayadelson
Copy link

@aboozaid I stand corrected here, and perhaps this is another nuance of the connectToLocalNetwork I am unfamiliar with.

I added more specific logging to the IoT device. When the connectToLocalNetwork is called, the device reports:

flags=[AUTH][ASSOC][AUTHORIZED][SHORT_PREAMBLE][WMM]
aid=1
capability=0x431
listen_interval=10
supported_rates=82 84 8b 96 24 b0 48 6c 8c 12 98 60
timeout_next=NULLFUNC POLL
dot11RSNAStatsVersion=1
dot11RSNAStatsSelectedPairwiseCipher=00-0f-ac-4
dot11RSNAStatsTKIPLocalMICFailures=0
dot11RSNAStatsTKIPRemoteMICFailures=0
hostapdWPAPTKState=11
hostapdWPAPTKGroupState=0
rx_packets=8
tx_packets=3
rx_bytes=1029
tx_bytes=355
connected_time=0

The Android 13 phone stays connected for a period (depending on the timeout set). Axios tries to send cleartext HTTP and fails to get a response, and eventually the connection times out and resets to the previous connection (depending on the autoconnect setting).

Once connectToLocalNetwork fires, there doesn't seem to be any ability to send cleartext. However, if I switch the networks manually in the Internet network settings of the phone, the app can send cleartext. Are there permissions specific to connectToLocalNetwork? This is the AndroidManifest.xml that works after a local network switch on Android 10, 11 and 12:

    <application
        android:networkSecurityConfig="@xml/network_security_config"
    </application>

and network_security_config, where 10.0.110.2 is the address of the device:

    <?xml version="1.0" encoding="utf-8"?>
    <network-security-config>
      <domain-config cleartextTrafficPermitted="true">
        <domain includeSubdomains="true">127.0.0.1</domain>
        <domain includeSubdomains="true">10.0.0.1</domain>
        <domain includeSubdomains="true">localhost</domain>
        <domain includeSubdomains="true">10.0.110.2</domain>
      </domain-config>
    </network-security-config>

Any thoughts?

@aboozaid
Copy link
Owner

I need logs from the android device itself. Can you confirm if OnNetworkDisconnected event fired immediately after connection?

@jayadelson
Copy link

jayadelson commented Oct 16, 2023

@aboozaid the only logcat information coming from TetheringManager is:

2023-10-16 16:35:31.258  4190-4611  RNWifiTethering         com.[application]                          D  onUnavailable

Then after the timeout:

2023-10-16 16:36:42.972  4190-4611  RNWifiTethering         com.[application]                          D  onLost

Usually I don't even get those... Only on first run do I get log output like that. Is there a debug flag I need to set in the module?

@jayadelson
Copy link

@aboozaid what I've been able to determine so far is that by default, on Android 13, it's creating two interfaces when you connectToLocalNetwork.

When you force disconnect, and there is only one interface, default network functions pass through the single interface.

Otherwise, despite being connected, it tries to send out the primary interface and fails.

You can specify the interface to use, using the localAddress option in Axios, but it's somewhat unreliable.

@jayadelson
Copy link

Ok @aboozaid I would go ahead and close this out. The issue persists, but it's not related to this issue IMHO, it's around Android 13 defaulting to using the "primary interface" and opening up a secondary interface when connectToLocalNetwork is fired. The network module can be forced to use the secondary, which resolves the problem.

@aboozaid
Copy link
Owner

I will close it but if you think you could fix that and send a PR it would be appreciated also feel free to open a new issue if needed

@smartmedev
Copy link

Ok @aboozaid I would go ahead and close this out. The issue persists, but it's not related to this issue IMHO, it's around Android 13 defaulting to using the "primary interface" and opening up a secondary interface when connectToLocalNetwork is fired. The network module can be forced to use the secondary, which resolves the problem.

Hi @jayadelson i'm experiencing the same problem, also with react-native-wifi-reborn library.
How did you solve it? Did you talked about primary and secondary interface and forcing android to use the secondary interface. Can you please explain how to do it?

I'm facing exactly the same problem with some android devices like OnePlus/Oppo when try to connect to the AP without internet connection of an IoT device we develop.

Thank you in advice

@jayadelson
Copy link

@smartmedev This was my solution here:

JuanSeBestia/react-native-wifi-reborn#303 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants