JuanSeBestia / react-native-wifi-reborn

A react-native implementation for viewing and connecting to Wifi networks on Android and iOS devices.
ISC License
362 stars 125 forks source link

Android 13 issue #303

Closed bankiprudhvi closed 4 months ago

bankiprudhvi commented 1 year ago

Wifi connection success but not connected to wifi ... mobile -Google pixel 6a

notjulian commented 7 months ago

Hi folks, I had the same problem and tried to fix, using the WiFi module of flutter

someone would like to try?

you can add in package.json

"react-native-wifi-reborn": "git+https://github.com/notjulian/react-native-wifi-reborn.git",

or create a patch using patch-package

Code example (for a hotspot without Internet), first connect, then forceWifiUsage true, before disconnect forceWiFiUsage to false

//To connect
await WifiManager.connectToProtectedWifiSSID({
                  ssid: WifiNetToUse,
                  password: pwdWifi,
                  isWEP: false,
                  isHidden: false,
                  timeout: 15,
                })
                  .then(async () => {
                    if (!isIOS)
                      await WifiManager.forceWifiUsageWithOptions(true, {
                        noInternet: true,
                      })
                  })
                  .catch(e => {
                    console.log(e.message, e.code)
                    error = e.code
                  })

//To disconnect
 await WifiManager.forceWifiUsageWithOptions(false, {
            noInternet: false,
})
await WifiManager.isRemoveWifiNetwork(currentSSID)
github-actions[bot] commented 6 months ago

Hey there, it looks like there has been no activity on this issue recently. Has the issue been fixed, or does it still require the community attention? This issue may be closed if no further activity occurs.

Sotatek-HuyNguyen12 commented 6 months ago

I'm still facing this issue on Samsung S23 - Android 14 and Samsung A23 - Android 13. Does anyone have a solution?

notjulian commented 6 months ago

I'm still facing this issue on Samsung S23 - Android 14 and Samsung A23 - Android 13. Does anyone have a solution?

have you tried this ?

smartmedev commented 5 months ago

Hi @jayadelson is noInternet: Boolean the only option you used? And if it's okay for you to share, can you lay out the steps that allowed you to connect to the network? (which method you called first and how you handle the disconnect/connect) Thanks in advance!

@Audrey-Ann First, I make sure my permissions are spot on with AndroidManifest.xml:

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_COURSE_LOCATION"/>
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission
        android:required="true"
        android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission 
        android:required="true"
        android:name="android.permission.CHANGE_NETWORK_STATE" />
        <uses-permission
           android:required="true"
           android:name="android.permission.ACCESS_WIFI_STATE"/>
    <uses-permission
           android:required="true"
           android:name="android.permission.CHANGE_WIFI_STATE"/>
    <uses-permission android:name="android.permission.NEARBY_WIFI_DEVICES"
                     android:usesPermissionFlags="neverForLocation" />

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

and my network_security_config. Note that I replace A.B.C.D with the IP address of the device I'm connecting to over the new connection, as I intend on sending clear text info to that device:

<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
  <domain-config cleartextTrafficPermitted="true">
    <!-- Without localhost setting, you won't be able to connect metro with app. -->
    <domain includeSubdomains="true">127.0.0.1</domain>
    <!-- For  a device, [A.B.C.D] is the address of the the device's network interface -->
    <domain includeSubdomains="true">[A.B.C.D]</domain>
  </domain-config>
</network-security-config>

I then go through a series of permissions checks when the app opens or tries to render the component that will be doing the WiFi activity. Since this is Android 13, it requires the NEARBY_WIFI_DEVICES check which hadn't been required previously, along with the other permissions above. Once those permissions have cleared and check back as granted, I grab the current SSID, and perform a WiFi scan for the newDeviceSSID. Then I begin to run through the process of changing the network in this order. Don't use this code verbatim, it's heavily edited, but gives you the general idea:

    if (Platform.OS == 'android') {
      try {
        if (WifiManager.forceWifiUsageWithOptions && Platform.Version >= 28) {
          await WifiManager.forceWifiUsageWithOptions(true, {noInternet: true});
        }
        if (currentWifi.current != newDeviceSSID) {
          try {
            await WifiManager.isRemoveWifiNetwork(currentWifi.current);
              // Wait for the device to disconnect from the current network
            await new Promise(resolve => setTimeout(resolve, 5000));
          } catch(disconnectError) {
            console.log("Error while disconnecting from WiFi:", disconnectError)
          }
        }
        const data = await WifiManager.connectToProtectedSSID(
          newDeviceSSID,
          password,
          false,
          false,
        );
        } catch (error) {
          console.log(`Android failed to connect to ${newDeviceSSID}.`)
          console.log("Error:", error.message);
          }
        }
    } else {
      [iOS]
      }
    }

Essentially, I first execute the forceWifiUsageWithOptions, then disconnect the current wlan0, then connect the new network, in that order. What isn't in this code is my tracking of the connection status. If the function completes I set the noInternet to be false and restore the original connection.

Hi everyone, I've the same problem connecting to an ioT device via Wi-Fi with no internet access. Everything works great on Android 13 on Samsung A22. On Pixel 8 and Android 14, I can't really connect. The callback fo connectToProtectedSSID and connectToProtectedWifiSSID said successfully connected but on wifi settings the phone really don't change wifi, remain connected on my home local network.

I've tried your steps, like these but is still not working, specially nor disconnect() nor isRemoveWifiNetwork(ssid) works. The phone remain connected to my home local wifi.

Anyone can help me?

notjulian commented 5 months ago

have you tried this ? @smartmedev

I'm currently using in production, also with Android 14

smartmedev commented 5 months ago

Hi @notjulian Yes, i've tried all the code snippets suggested in this thread but nothing works. I'm using Google Pixel 8 with Android 14. The problem is that the phone never really disconnect from my home local wifi network even if the library said that it has successfully connected to the ioT device Wi-Fi.

If when I try connection to the ioT Wi-Fi I'm not connected to any other local Wi-Fi network, everything works fine. What I have found trying these, is that on Google Pixel 8 and Android 14, when I use this library to connect to the ioT Wi-Fi device, when the connection is successfully made via the library classes, if I check on the phone settings, the wifi network not appear to be connected and I don't know why.

notjulian commented 5 months ago

Hi @notjulian Yes, i've tried all the code snippets suggested in this thread but nothing works. I'm using Google Pixel 8 with Android 14. The problem is that the phone never really disconnect from my home local wifi network even if the library said that it has successfully connected to the ioT device Wi-Fi.

If when I try connection to the ioT Wi-Fi I'm not connected to any other local Wi-Fi network, everything works fine. What I have found trying these, is that on Google Pixel 8 and Android 14, when I use this library to connect to the ioT Wi-Fi device, when the connection is successfully made via the library classes, if I check on the phone settings, the wifi network not appear to be connected and I don't know why.

can you please post your package.json please?

smartmedev commented 5 months ago

Hi @notjulian

{
  "name": "MyApp",
  "version": "0.0.24",
  "private": true,
  "scripts": {
    "android": "react-native run-android",
    "ios": "react-native run-ios",
    "start": "react-native start",
    "test": "jest",
    "lint": "eslint .",
    "debug": "open 'rndebugger://set-debugger-loc?host=localhost&port=8081'"
  },
  "dependencies": {
    "@gorhom/bottom-sheet": "^4.6.3",
    "@invertase/react-native-apple-authentication": "^2.3.0",
    "@mapbox/geo-viewport": "^0.5.0",
    "@react-native-async-storage/async-storage": "1.23.1",
    "@react-native-community/netinfo": "^11.3.2",
    "@react-native-community/slider": "^4.5.2",
    "@react-navigation/core": "6.4.16",
    "@react-navigation/drawer": "^6.6.15",
    "@react-navigation/native": "^6.1.17",
    "@react-navigation/native-stack": "^6.9.26",
    "@rnmapbox/maps": "^10.1.24",
    "@sayem314/react-native-keep-awake": "^1.2.3",
    "@types/react-native": "^0.73.0",
    "axios": "^1.7.1",
    "deprecated-react-native-prop-types": "^5.0.0",
    "geolib": "^3.3.4",
    "i18next": "^23.11.4",
    "jwt-decode": "^3.1.2",
    "lodash": "^4.17.21",
    "moment": "^2.30.1",
    "prop-types": "^15.8.1",
    "qs": "^6.12.1",
    "react": "18.2.0",
    "react-i18next": "^14.1.1",
    "react-native": "0.74.1",
    "react-native-android-open-settings": "^1.3.0",
    "react-native-bouncy-checkbox": "^4.0.1",
    "react-native-circular-slider": "^1.0.1",
    "react-native-device-info": "^11.1.0",
    "react-native-geolocation-service": "^5.3.1",
    "react-native-gesture-handler": "^2.16.2",
    "react-native-get-random-values": "^1.11.0",
    "react-native-google-places-autocomplete": "^2.5.6",
    "react-native-google-signin": "^2.1.1",
    "react-native-gradle-plugin": "^0.71.19",
    "react-native-linear-gradient": "^2.8.3",
    "react-native-option-menu": "^1.1.3",
    "react-native-progress": "^5.0.1",
    "react-native-reanimated": "^3.9.0",
    "react-native-safe-area-context": "^4.10.1",
    "react-native-screens": "^3.31.1",
    "react-native-select-dropdown": "^3.4.0",
    "react-native-splash-screen": "^3.3.0",
    "react-native-svg": "^15.3.0",
    "react-native-swipe-gestures": "^1.0.5",
    "react-native-switch": "^1.5.1",
    "react-native-tcp-socket": "6.0",
    "react-native-toast-message": "^2.2.0",
    "react-native-version-check": "^3.4.7",
    "react-native-web": "^0.19.11",
    "react-native-webview": "^13.10.2",
    "react-native-wifi-reborn": "^4.12.1",
    "react-redux": "^7.2.9",
    "redux": "^4.2.1",
    "redux-devtools-extension": "^2.13.9",
    "redux-thunk": "^2.4.2",
    "remote-redux-devtools": "^0.5.16",
    "text-decoding": "^1.0.0",
    "uuid": "^9.0.1"
  },
  "devDependencies": {
    "@babel/core": "^7.20.0",
    "@babel/preset-env": "^7.20.0",
    "@babel/runtime": "^7.20.0",
    "@react-native/babel-preset": "0.74.83",
    "@react-native/eslint-config": "0.74.83",
    "@react-native/metro-config": "0.74.83",
    "@react-native/typescript-config": "0.74.83",
    "@types/react": "^18.2.6",
    "@types/react-test-renderer": "^18.0.0",
    "babel-jest": "^29.6.3",
    "eslint": "^8.19.0",
    "jest": "^29.6.3",
    "prettier": "2.8.8",
    "react-test-renderer": "18.2.0",
    "typescript": "5.0.4"
  },
  "jest": {
    "preset": "react-native"
  },
  "engines": {
    "node": ">=18"
  },
  "packageManager": "yarn@3.6.4"
}
notjulian commented 5 months ago

hi @smartmedev, if you'd like to try this this you should change react-native-wifi-reborn in package.json

"react-native-wifi-reborn": "git+https://github.com/notjulian/react-native-wifi-reborn.git",

smartmedev commented 5 months ago

hi @notjulian thank you, I was missing your import in my package.json. Anyway I tried to add it and run npm install, but unfortunately nothing changed. I see your changes to the library code, the library said that the connection is successfully done with the ioT device, but data still not pass through that connection.

I'm stucked with this, i think that the only option remain send user to manually connect via the phone settings

YaroslavRoom4 commented 5 months ago

I should note that I only encountered this problem on Pixel phones. Also I have a suspicion that this may be related to Tensor processors or something related specifically to their phones with that processors. @smartmedev could you check this?

smartmedev commented 5 months ago

Hi everyone! I think I've found a definitive solution. Definitely for my case. The problem to me was that this library on Google Pixel phone (Android 13/14), returns a feedback that the phone successfully connected to the ioT WiFi but really, from the phone settings, the phone remains connected to my home WiFi network.

The complete scenario of my app is as follows:

  1. I use this library (react-native-wifi-reborn) to connect to the ioT device WiFi directly via app.
  2. When connection is done, i use another library (react-native-tcp-socket) to create a tcp socket connection between app and ioT device via ioT device WiFi.

Based on suggestions made by @jayadelson on this comment. I've debugged Android on native part and I've found that on Pixel phone, if you're already connected to a WiFi with internet access (suppose my home WiFi), when you connect to a WiFi network without internet (ioT device), the phone successfully connect to ioT device WiFi but without disconnecting from home WiFi. So, when you go to the phone WiFi settings, you find that you're connected to your home WiFi and you can see your ioT device WiFi that seems disconnected, ...but you're not! In reality, on Pixel phones, Android create two concurrent wifi network connection interfaces: the first is your home WiFi (with internet connection), the second is your ioT with no internet WiFi.

This scenario should be real on every app targeting Android 12 (API 31) or higher (see here: https://developer.android.com/about/versions/12/behavior-changes-12#concurrent-connections) The fact is that on my Samsung A22 (Android 13), instead, the phone diconnect from home WiFi before connecting to ioT device WiFi and this info reflects on phone WiFi settings.

However, I was focusing that the problems were on this library (react-native-wifi-reborn), but in reality during debug I've found that the two concurrent network interfaces are successfully created. So I checked the code of react-native-tcp-socket library and I found that it was trying to render all data to the first network interface (wlan0) that is my home WiFi. So I've made some changes on that library to check all networks that the phone is connected, then find the desired one based on Dhcp server address or network subnet address, bind all data to desired network and the magic happened!

For everyone who is having a similar problem using these two libraries, I'm posting a PR for the library react-native-tcp-socket. Here is it the PR for react-native-tcp-socket

github-actions[bot] commented 4 months ago

Hey there, it looks like there has been no activity on this issue recently. Has the issue been fixed, or does it still require the community attention? This issue may be closed if no further activity occurs.