Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding getConnectedBSSID to Android platform #82

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
52 changes: 40 additions & 12 deletions src/android/src/com/pylonproducts/wifiwizard/WifiWizard.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
Expand Down Expand Up @@ -43,6 +43,7 @@ public class WifiWizard extends CordovaPlugin {
private static final String START_SCAN = "startScan";
private static final String GET_SCAN_RESULTS = "getScanResults";
private static final String GET_CONNECTED_SSID = "getConnectedSSID";
private static final String GET_CONNECTED_BSSID = "getConnectedBSSID";
private static final String IS_WIFI_ENABLED = "isWifiEnabled";
private static final String SET_WIFI_ENABLED = "setWifiEnabled";
private static final String TAG = "WifiWizard";
Expand Down Expand Up @@ -99,6 +100,9 @@ else if(action.equals(DISCONNECT)) {
else if(action.equals(GET_CONNECTED_SSID)) {
return this.getConnectedSSID(callbackContext);
}
else if(action.equals(GET_CONNECTED_BSSID)) {
return this.getConnectedBSSID(callbackContext);
}
else {
callbackContext.error("Incorrect action parameter: " + action);
}
Expand Down Expand Up @@ -466,6 +470,28 @@ private boolean startScan(CallbackContext callbackContext) {
* @return true if SSID found, false if not.
*/
private boolean getConnectedSSID(CallbackContext callbackContext){
return getWifiServiceInfo(callbackContext, false);
}

/**
* This method retrieves the BSSID for the currently connected network
*
* @param callbackContext A Cordova callback context
* @return true if SSID found, false if not.
*/
private boolean getConnectedBSSID(CallbackContext callbackContext){
return getWifiServiceInfo(callbackContext, true);
}

/**
* This method retrieves the WifiInformation for the (SSID or BSSID)
* currently connected network.
*
* @param callbackContext A Cordova callback context
* @param basicIdentifier A flag to get BSSID if true or SSID if false.
* @return true if SSID found, false if not.
*/
private boolean getWifiServiceInfo(CallbackContext callbackContext, boolean basicIdentifier){
if(!wifiManager.isWifiEnabled()){
callbackContext.error("Wifi is disabled");
return false;
Expand All @@ -478,19 +504,21 @@ private boolean getConnectedSSID(CallbackContext callbackContext){
return false;
}

String ssid = info.getSSID();
if(ssid.isEmpty()) {
ssid = info.getBSSID();
String serviceInfo;
if(basicIdentifier) {
serviceInfo = info.getBSSID();
} else {
serviceInfo = info.getSSID();
}
if(ssid.isEmpty()){
callbackContext.error("SSID is empty");

if(serviceInfo.isEmpty()){
callbackContext.error("Wifi information is empty");
return false;
}

callbackContext.success(ssid);
callbackContext.success(serviceInfo);
return true;
}

/**
* This method retrieves the current WiFi status
*
Expand Down Expand Up @@ -531,9 +559,9 @@ private boolean setWifiEnabled(CallbackContext callbackContext, JSONArray data)
Log.d(TAG, "WifiWizard: disconnectNetwork invalid data");
return false;
}

String status = "";

try {
status = data.getString(0);
}
Expand All @@ -542,11 +570,11 @@ private boolean setWifiEnabled(CallbackContext callbackContext, JSONArray data)
Log.d(TAG, e.getMessage());
return false;
}

if (wifiManager.setWifiEnabled(status.equals("true"))) {
callbackContext.success();
return true;
}
}
else {
callbackContext.error("Cannot enable wifi");
return false;
Expand Down