Comments (10)
Connecting and locking to a network:
val hotspotSpecifier = WifiNetworkSpecifier.Builder()
.setSsid(hotspotSSID);
val hotspotConnectRequest = NetworkRequest.Builder()
.addTransportType(NetworkCapabilities.TRANSPORT_WIFI)
.removeCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)
.setNetworkSpecifier(hotspotSpecifier.build())
.build();
connectivityCallback = object : ConnectivityManager.NetworkCallback() {
override fun onAvailable(network: Network) {
super.onAvailable(network);
if (lock) {
// Lock the device to this network and force all network requests
connectivityManager.bindProcessToNetwork(network);
wifiLock = wifiManager.createWifiLock(WifiManager.WIFI_MODE_FULL_HIGH_PERF, "WIFI_LOCK");
}
promise.resolve("SSID_CONNECTION_SUCCESS");
}
override fun onUnavailable() {
super.onUnavailable();
promise.reject("SSID_CONNECTION_FAILED", "Could not connect to $hotspotSSID");
}
override fun onLost(network: Network) {
super.onLost(network)
System.out.println("Lost network...")
System.out.println(network.toString())
}
}
connectivityManager.requestNetwork(hotspotConnectRequest, connectivityCallback);
Releasing a lock and disconnecting:
try {
connectivityManager.unregisterNetworkCallback(connectivityCallback)
// Calling removeNetworkSuggestions with an empty list will remove
// all network connections that have been suggested by this app.
//
// This should force a disconnect from the locked network and will
// let the device go back to a known network.
val networkSuggestions = listOf<WifiNetworkSuggestion>()
wifiManager.removeNetworkSuggestions(networkSuggestions)
if (::wifiLock.isInitialized && wifiLock.isHeld) {
wifiLock.release();
}
connectivityManager.bindProcessToNetwork(null)
} catch(e: Throwable){
// Wifi Lock may have already been released at this point
}
from react-native-wifi-reborn.
I assume with 'then I can get successful responses using fetch and RNFetchBlob' that you mean against a public url and not with your IoT-like device? What are your proceeding requests? To the same IoT-device or to an url on the internet?
By forceWifiUsage all of the requests your app triggers, are going to the IoT access point.
So if you want to call something on the IoT access point, and then do a regular api call to the internet you have to:
- forceWifiUsage(true)
- do api call against IoT AP
- forceWifiUsage(false)
- do other api calls (also, maybe disconnect from AP?)
from react-native-wifi-reborn.
I assume with 'then I can get successful responses using fetch and RNFetchBlob' that you mean against a public url and not with your IoT-like device? What are your proceeding requests? To the same IoT-device or to an url on the internet?
By forceWifiUsage all of the requests your app triggers, are going to the IoT access point.
So if you want to call something on the IoT access point, and then do a regular api call to the internet you have to:
- forceWifiUsage(true)
- do api call against IoT AP
- forceWifiUsage(false)
- do other api calls (also, maybe disconnect from AP?)
All requests are going to the IoT AP. I need to connect to the AP, make some API calls, and then disconnect without the OS disconnecting me automatically.
As of right now, I can't get requests to succeed if I force wifi usage before connecting to the AP.
Also, I cannot disconnect from the AP using the isRemoveWifiNetwork
function. The device stays on the network until I force close the app.
from react-native-wifi-reborn.
You first need to connect to the AP, then you can forceWifiUsage.
isRemoveWifiNetwork on < Android 10 only works if the wifi network was not yet in the saved networks list. It can't remove a network that the user or another app created first.
from react-native-wifi-reborn.
You first need to connect to the AP, then you can forceWifiUsage.
That seems to be working. Thank you!
isRemoveWifiNetwork on < Android 10 only works if the wifi network was not yet in the saved networks list. It can't remove a network that the user or another app created first.
I'm on Android 10 and it won't disconnect immediately. When it does disconnect, it won't reconnect to the known, nearby home network afterwards. The phone just sits without a wifi connection until I force close the app. Once the app is closed, the phone will connect to the known home wifi again.
Is this the proper order to disconnect?:
await WifiManager.forceWifiUsage(false);
console.log('Disconnect from', this.state.ssid);
const disconnectSuccess = await WifiManager.isRemoveWifiNetwork(this.state.ssid);
console.log('Success', disconnectSuccess);
from react-native-wifi-reborn.
Yes the code looks correct to me. Did you test on multiple devices?
from react-native-wifi-reborn.
@eliaslecomte , my issue got the same with this issue.
from react-native-wifi-reborn.
Yes the code looks correct to me. Did you test on multiple devices?
I tested on a Pixel 3a. It didn't work, so I didn't move on to any other devices. I wrote my own using NativeBridge and it's working perfectly.
from react-native-wifi-reborn.
@jcarioti , can you please share your code?
from react-native-wifi-reborn.
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.
from react-native-wifi-reborn.
Related Issues (20)
- WifiManager.connectToProtectedSSID got 4 arguments , expected 6 HOT 1
- WifiManager.connectToProtectedSSID got 7 arguments , expected 6 HOT 1
- `WifiManager.getCurrentWifiSSID()` never fulfilled HOT 5
- Why is it necessary to specify a Node version? HOT 9
- Android 13 local Websocket HOT 4
- javax.xml.bind.UnmarshalException HOT 1
- IOS 17. Cannot read property 'connectToProtectedSSID' of null HOT 5
- This project is maintained ? HOT 2
- starting IOS 17 connectToSSID opens a webportal/captive Portal HOT 5
- android build issue from recent package update HOT 11
- 4.10.0 doesn't build HOT 3
- Reconnecting to original Wifi HOT 1
- npm ERR! engine Unsupported engine HOT 2
- Can't find a method to connect via BSSID HOT 1
- iot device's http server is not accessible on release build HOT 1
- connectionStatus() is returning 'false' when the Wi-Fi is connected. HOT 2
- Failed to build the app: No package name found. HOT 2
- Multiple commands produce libRNWifi.a HOT 1
- Import only works with path HOT 1
- Android 13 - Not returning correct error when password was wrong HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from react-native-wifi-reborn.