Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
sushichop committed May 4, 2018
0 parents commit f9a68c7
Show file tree
Hide file tree
Showing 11 changed files with 475 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# macOS
.DS_Store

# Node
.idea/
node_modules/
5 changes: 5 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.npmignore
.gitignore
.DS_Store
.idea/
node_modules/
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Change Log

## [0.1.0](https://github.com/sushichop/cordova-plugin-wifi-manager/tree/0.1.0) (2018-05-04)

**Implemented enhancements:**

- First release
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2018 sushichop.net

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
51 changes: 51 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# cordova-plugin-wifi-manager

Wi-Fi Manager Plugin for Apache Cordova

## Supported Platforms

- Android: 4.4 or later
- iOS: 10.0 or later

## Installation

```bash
cordova plugin add cordova-plugin-wifi-manager
```

## Usage

#### Connect to Wi-Fi access point

```javascript
window.document.addEventListener('deviceready', onDeviceReady, false);

function onDeviceReady () {
window.wifiManager.connect(
'TARGET_SSID',
'TARGET_PASSPHRASE',
function (success) { console.log(success); },
function (failure) { console.log(failure); }
);
}
```

#### Disconnect from Wi-Fi access point

```javascript
window.document.addEventListener('deviceready', onDeviceReady, false);

function onDeviceReady () {
window.wifiManager.disconnect(
'TARGET_SSID',
function (success) { console.log(success); },
function (failure) { console.log(failure); }
);
}
```

## License

[MIT]: http://www.opensource.org/licenses/mit-license

`cordova-plugin-wifi-manager` is available under the [MIT license][MIT]. See the LICENSE file for details.
25 changes: 25 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"name": "cordova-plugin-wifi-manager",
"version": "0.1.0",
"description": "Wi-Fi Manager Plugin for Apache Cordova",
"cordova": {
"id": "cordova-plugin-wifi-manager",
"platforms": [
"android",
"ios"
]
},
"repository": {
"type": "git",
"url": "https://github.com/sushichop/cordova-plugin-wifi-manager"
},
"keywords": [
"cordova",
"ecosystem:cordova",
"cordova-android",
"cordova-ios",
"wifi"
],
"author": "sushichop.net",
"license": "MIT"
}
50 changes: 50 additions & 0 deletions plugin.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?xml version="1.0" encoding="UTF-8"?>
<plugin xmlns="http://apache.org/cordova/ns/plugins/1.0" id="cordova-plugin-wifi-manager" version="0.1.0">
<name>WiFiManager</name>
<description>Wi-Fi Manager Plugin for Apache Cordova</description>
<license>MIT</license>
<keywords>cordova,wifi</keywords>
<repo>https://github.com/sushichop/cordova-plugin-wifi-manager.git</repo>

<js-module src="www/wifiManager.js" name="wifiManager">
<clobbers target="wifiManager" />
</js-module>

<platform name="android">
<config-file target="config.xml" parent="/*">
<feature name="WiFiManager">
<param name="android-package" value="net.sushichop.cordova.wifimanager.WiFiManagerPlugin" />
</feature>
</config-file>

<config-file target="AndroidManifest.xml" parent="/*">
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
</config-file>

<source-file src="src/android/WiFiManagerPlugin.java" target-dir="src/net/sushichop/cordova/wifimanager/" />

</platform>

<platform name="ios">
<config-file target="config.xml" parent="/*">
<feature name="WiFiManager">
<param name="ios-package" value="SCPWiFiManagerPlugin" />
</feature>
</config-file>

<config-file parent="com.apple.developer.networking.HotspotConfiguration" target="*/Entitlements-Debug.plist">
<true />
</config-file>
<config-file parent="com.apple.developer.networking.HotspotConfiguration" target="*/Entitlements-Release.plist">
<true />
</config-file>

<header-file src="src/ios/SCPWiFiManagerPlugin.h" />
<source-file src="src/ios/SCPWiFiManagerPlugin.m" />

<framework src="NetworkExtension.framework" />

</platform>

</plugin>
170 changes: 170 additions & 0 deletions src/android/WiFiManagerPlugin.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
package net.sushichop.cordova.wifimanager;

import android.content.Context;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiManager;

import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaInterface;
import org.apache.cordova.CordovaPlugin;
import org.apache.cordova.CordovaWebView;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;


public class WiFiManagerPlugin extends CordovaPlugin {

private static final int ERROR_CODE_OFFSET = 1000;

private static final int ADD_NETWORK_ERROR = 1 + ERROR_CODE_OFFSET;
private static final int ENABLE_NETWORK_ERROR = 2 + ERROR_CODE_OFFSET;
private static final int RECONNECT_ERROR = 3 + ERROR_CODE_OFFSET;
private static final int DISCONNECT_ERROR = 4 + ERROR_CODE_OFFSET;
private static final int DISABLE_NETWORK_ERROR = 5 + ERROR_CODE_OFFSET;
private static final int UNKNOWN_ACTION_ERROR = 6 + ERROR_CODE_OFFSET;

private static final String ADD_NETWORK_ERROR_MESSAGE = "addNetwork failed.";
private static final String ENABLE_NETWORK_ERROR_MESSAGE = "enableNetwork failed.";
private static final String RECONNECT_ERROR_MESSAGE = "reconnect failed.";
private static final String DISCONNECT_ERROR_MESSAGE = "disconnect failed.";
private static final String DISABLE_NETWORK_ERROR_MESSAGE = "disableNetwork failed.";
private static final String UNKNOWN_ACTION_ERROR_MESSAGE = "unknownAction occurred.";

private WifiManager manager;

@Override
public void initialize(CordovaInterface cordova, CordovaWebView webView) {
super.initialize(cordova, webView);
Context context = cordova.getActivity().getApplicationContext();
manager = (WifiManager)context.getSystemService(Context.WIFI_SERVICE);
}

@Override
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {

switch (action) {
case "connect":
connect(args, callbackContext);
break;
case "disconnect":
disconnect(args, callbackContext);
break;
default:
executeUnknownAction(callbackContext);
}

return true;
}

private void connect(JSONArray args, CallbackContext callbackContext) throws JSONException {

if (!manager.isWifiEnabled()) {
manager.setWifiEnabled(true);
}

String ssid = args.optString(0);
String passphrase = args.optString(1);
JSONObject json = new JSONObject();

WifiConfiguration config = setWPAConfiguration(ssid, passphrase);

int networkId = manager.addNetwork(config);
manager.updateNetwork(config);

if (networkId == -1) {
json.put("code", ADD_NETWORK_ERROR);
json.put("message", ADD_NETWORK_ERROR_MESSAGE);
callbackContext.error(json);
return;
}

if (!manager.enableNetwork(networkId, true)) {
json.put("code", ENABLE_NETWORK_ERROR);
json.put("message", ENABLE_NETWORK_ERROR_MESSAGE);
callbackContext.error(json);
return;
}

if (!manager.reconnect()) {
json.put("code", RECONNECT_ERROR);
json.put("message", RECONNECT_ERROR_MESSAGE);
callbackContext.error(json);
return;
}

json.put("ssid", ssid);
json.put("passphrase", passphrase);
callbackContext.success(json);
}


private void disconnect(JSONArray args, CallbackContext callbackContext) throws JSONException {

String ssid = args.optString(0);

JSONObject json = new JSONObject();

WifiConfiguration config = setWPAConfiguration(ssid);

int networkId = manager.addNetwork(config);
manager.updateNetwork(config);

if (networkId == -1) {
json.put("code", ADD_NETWORK_ERROR);
json.put("message", ADD_NETWORK_ERROR_MESSAGE);
callbackContext.error(json);
return;
}

if (!manager.disconnect()) {
json.put("code", DISCONNECT_ERROR);
json.put("message", DISCONNECT_ERROR_MESSAGE);
callbackContext.error(json);
return;
}

if (!manager.disableNetwork(networkId)) {
json.put("code", DISABLE_NETWORK_ERROR);
json.put("message", DISABLE_NETWORK_ERROR_MESSAGE);
callbackContext.error(json);
return;
}

json.put("ssid", ssid);

callbackContext.success(json);
}

private void executeUnknownAction(CallbackContext callbackContext) throws JSONException {
JSONObject json = new JSONObject();
json.put("code", UNKNOWN_ACTION_ERROR);
json.put("message", UNKNOWN_ACTION_ERROR_MESSAGE);
callbackContext.error(json);
}

private WifiConfiguration setWPAConfiguration(String ssid, String passphrase) {

WifiConfiguration config = new WifiConfiguration();
config.SSID = "\"" + ssid + "\"";
if (passphrase != null) {
config.preSharedKey = "\"" + passphrase + "\"";
}

//config.status = WifiConfiguration.Status.ENABLED;

config.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
config.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
config.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
config.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);

return config;
}

private WifiConfiguration setWPAConfiguration(String ssid) {
return setWPAConfiguration(ssid, null);
}
}
9 changes: 9 additions & 0 deletions src/ios/SCPWiFiManagerPlugin.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#import <Foundation/Foundation.h>
#import <Cordova/CDV.h>

@interface SCPWiFiManagerPlugin : CDVPlugin

- (void)connect:(CDVInvokedUrlCommand *)command;
- (void)disconnect:(CDVInvokedUrlCommand *)command;

@end
Loading

0 comments on commit f9a68c7

Please sign in to comment.