LynxWifi
Reads the Wi-Fi network the phone is on, and joins another one. import { LynxWifi } from '@blotch/angular-lynx';
Setting up a smart device usually goes this way:
- The app reads the phone's network, and asks the person for its password.
- It sends both to the device, over Bluetooth or over the device's own access point, which the app joins first.
Turn it on
Add wifi to the build target's options in angular.json, with the parts your app calls. To read the network, also add permissions.location, with the reason iOS shows when it asks for the person's location:
What each part adds:
- Android grants the Wi-Fi and network permissions at install, and asks for the location ones. Wi-Fi is declared not required, so Google Play still offers the app to devices without it.
readCurrentNetworkneedspermissions.locationtoo, and the build stops without it. Both platforms give the network's name only to an app with the person's location.
Without wifi, the native module is left out of both apps, and every method rejects with unavailable. A part you leave out is left out the same way: its native code isn't in either app, and its methods reject with unavailable.
A free Apple ID can't sign these capabilities. Each part adds an entitlement to the iOS app: com.apple.developer.networking.HotspotConfiguration and com.apple.developer.networking.wifi-info.
Only a team in the paid Apple Developer Program can sign them for an iPhone, so ask only for the parts you call. The Simulator needs no team.
See Turn on device features for every setting.
Usage
Frame stands for the app's own code that talks to the device, and 1234 for the code printed on it.
API
LynxWifi
currentNetwork()
Resolves the network the phone is on, { ssid, bssid? }, or null when it isn't on Wi-Fi. The BSSID, the access point's hardware address, is in lowercase, and missing when the platform hides it.
- iOS:
NEHotspotNetwork.fetchCurrent. The first call asks for the person's location, withpermissions.location's reason. iOS gives the network only with Precise Location on. It rejects withdeniedwhen the person says no. - Android:
WifiManager, once the app hasACCESS_FINE_LOCATIONand Location is on. A call without the permission asks for the person's location first. Android 12 and later offer precise and approximate, and only precise gives the network's name. deniedon Android: when the person refuses or picks approximate, and when Location is off. Android stops asking after the second refusal, and then only Settings can grant it (openAppSettings()opens the app's page there). So let the person type the network's name when it'sdenied.
join(options)
Joins the network. When it resolves depends on the platform and the options. Don't take it as proof the phone is on the network unless the list below says so:
- iOS with
readCurrentNetwork: once the phone is on it. - iOS with
joinalone: once the person allowed the network. iOS says nothing about whether the phone then joined it. - Android with
joinOnce: once the phone is on it. - Android without
joinOnce: once Android has the network, which the phone joins later, when it's in range.
It throws a TypeError, before asking anything, for a name longer than 32 bytes of UTF-8. It does the same for a password that isn't 8 to 63 printable ASCII characters.
On iOS, it uses NEHotspotConfigurationManager, after iOS asks the person whether the app may join the network.
- iOS answers that it joined even when the network is out of range. So with
readCurrentNetworktoo,join()reads the network afterwards, and rejects withnotFoundwhen the phone isn't on it. - With
joinOnce, iOS leaves the network once the app has been in the background a while.
On Android 10 and later, it depends on joinOnce. Earlier Android versions reject with unavailable.
- With
joinOnce: aWifiNetworkSpecifier. Android shows a dialog where the person picks the network. The app's traffic then goes over it: a device's access point has no internet, so Android would otherwise keep the app on mobile data or another Wi-Fi. That includes the app's ownfetch(), so callleave()once you're done with the device. - Without
joinOnce: aWifiNetworkSuggestion. The system adds it to the networks it joins by itself, the first time after the person allows the app's suggestions in a notification.
leave(ssid)
Forgets a network join() joined, which takes the phone off it if it's on it. iOS removes the configuration. Android releases the network it asked for and removes its suggestion.
It resolves for a network the app didn't join too, which stays as it is.
WifiError
Each method rejects with a WifiError, whose code says why:
Android doesn't tell a closed dialog apart from a network the phone couldn't join.
Testing
Each test's phone starts off Wi-Fi, so currentNetwork() resolves null. Every join() succeeds and puts the phone on the network, and leave() takes it off again.
setWifiNetwork()puts it on a network.denyWifiNetworkRead()makescurrentNetwork()reject, as a phone whose owner refused the location does.queueWifiJoinResult()answers the nextjoin().wifiJoins()lists the joins.
Related
- Turn on device features: the
wifioption and the other opt-in build options - Permission reasons: the
locationreason LynxBluetooth: send the network to a device over Bluetooth
