Skip to content
This repository was archived by the owner on Jan 14, 2025. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions IOTWifi.podspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
require 'json'

package = JSON.parse(File.read('package.json'))

Pod::Spec.new do |s|
s.name = 'IOTWifi'
s.version = package['version']
s.summary = package['description']
s.description = package['description']
s.homepage = package['homepage']
s.license = package['license']
s.authors = package['authors']
s.source = { :git => 'https://github.com/tadasr/react-native-iot-wifi.git' }
s.platform = :ios, '10.3'
s.ios.deployment_target = '10.3'
s.source_files = 'ios/**/*.{h,m}'
s.exclude_files = 'android/**/*'
s.exclude_files = 'example/**/*'
s.dependency 'React'
end

7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@
Wifi configuration.
This library was written to config iot devices. With iOS 11 Apple introduced NEHotspotConfiguration class for wifi configuration. Library supports same functionality on ios and android.

## 1.0.1
iOS:
* Add a podspec to support autolinking in React Native versions >= 0.60

Android:
* Update compileSdkVersion to 28

## 1.0.0
* Optional Force binding to a Wifi on both iOS and Android platforms
* Android: Better error handling
Expand Down
2 changes: 1 addition & 1 deletion android/build.gradle
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
apply plugin: 'com.android.library'

android {
compileSdkVersion 26
compileSdkVersion 28
buildToolsVersion '28.0.3'

defaultConfig {
Expand Down
48 changes: 32 additions & 16 deletions android/src/main/java/com/tadasr/IOTWifi/IOTWifiModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,20 @@ class FailureCodes {
static int FAILED_TO_BIND_CONFIG = 4;
}

class IOTWifiCallback {
private Callback callback;

public IOTWifiCallback(Callback callback) {
this.callback = callback;
}

public void invoke(Object... args) {
if (callback == null) return;
callback.invoke(args);
callback = null;
}
}

public class IOTWifiModule extends ReactContextBaseJavaModule {
private WifiManager wifiManager;
private ConnectivityManager connectivityManager;
Expand Down Expand Up @@ -67,13 +81,10 @@ public void run() {
}).start();
}

private void connectToWifi(String ssid, String passphrase, Boolean isWEP, Boolean bindNetwork, Callback callback) {
if (Build.VERSION.SDK_INT > 28) {
callback.invoke("Not supported on Android Q");
return;
}
private void connectToWifi(String ssid, String passphrase, Boolean isWEP, Boolean bindNetwork, final Callback callback) {
IOTWifiCallback iotWifiCallback = new IOTWifiCallback(callback);
if (!removeSSID(ssid)) {
callback.invoke(errorFromCode(FailureCodes.SYSTEM_ADDED_CONFIG_EXISTS));
iotWifiCallback.invoke(errorFromCode(FailureCodes.SYSTEM_ADDED_CONFIG_EXISTS));
return;
}

Expand All @@ -85,31 +96,31 @@ private void connectToWifi(String ssid, String passphrase, Boolean isWEP, Boolea
wifiManager.disconnect();
boolean success = wifiManager.enableNetwork(networkId, true);
if (!success) {
callback.invoke(errorFromCode(FailureCodes.FAILED_TO_ADD_CONFIG));
iotWifiCallback.invoke(errorFromCode(FailureCodes.FAILED_TO_ADD_CONFIG));
return;
}
success = wifiManager.reconnect();
if (!success) {
callback.invoke(errorFromCode(FailureCodes.FAILED_TO_CONNECT));
iotWifiCallback.invoke(errorFromCode(FailureCodes.FAILED_TO_CONNECT));
return;
}
boolean connected = pollForValidSSSID(10, ssid);
if (!connected) {
callback.invoke(errorFromCode(FailureCodes.FAILED_TO_CONNECT));
iotWifiCallback.invoke(errorFromCode(FailureCodes.FAILED_TO_CONNECT));
return;
}
if (!bindNetwork) {
callback.invoke();
iotWifiCallback.invoke();
return;
}
try {
bindToNetwork(ssid, callback);
bindToNetwork(ssid, iotWifiCallback);
} catch (Exception e) {
Log.d("IoTWifi", "Failed to bind to Wifi: " + ssid);
callback.invoke();
iotWifiCallback.invoke();
}
} else {
callback.invoke(errorFromCode(FailureCodes.FAILED_TO_ADD_CONFIG));
iotWifiCallback.invoke(errorFromCode(FailureCodes.FAILED_TO_ADD_CONFIG));
}
}

Expand Down Expand Up @@ -149,7 +160,7 @@ private boolean pollForValidSSSID(int maxSeconds, String expectedSSID) {
return false;
}

private void bindToNetwork(final String ssid, final Callback callback) {
private void bindToNetwork(final String ssid, final IOTWifiCallback callback) {
NetworkRequest.Builder builder = new NetworkRequest.Builder();
builder.addTransportType(NetworkCapabilities.TRANSPORT_WIFI);
connectivityManager.requestNetwork(builder.build(), new ConnectivityManager.NetworkCallback() {
Expand Down Expand Up @@ -218,7 +229,10 @@ private boolean removeSSID(String ssid) {
if (existingNetworkId == -1) {
return success;
}
success = wifiManager.removeNetwork(existingNetworkId) && wifiManager.saveConfiguration();
success = wifiManager.removeNetwork(existingNetworkId);
if (success && Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
success = wifiManager.saveConfiguration();
}
//If not our config then success would be false
return success;
}
Expand Down Expand Up @@ -253,7 +267,9 @@ private WifiConfiguration getExistingNetworkConfig(String ssid) {
String comparableSSID = ('"' + ssid + '"'); // Add quotes because wifiConfig.SSID has them
if (configList != null) {
for (WifiConfiguration wifiConfig : configList) {
if (wifiConfig.SSID.equals(comparableSSID)) {
String savedSSID = wifiConfig.SSID;
if (savedSSID == null) continue; // In few cases SSID is found to be null, ignore those configs
if (savedSSID.equals(comparableSSID)) {
Log.d("IoTWifi", "Found Matching Wifi: "+ wifiConfig.toString());
existingNetworkConfigForSSID = wifiConfig;
break;
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-native-iot-wifi",
"version": "1.0.0",
"version": "1.2.0",
"description": "Connect to WiFi with React Native on Android and iOS.",
"main": "index.js",
"types": "index.d.ts",
Expand Down