flutter_smartconfig/android/src/main/java/de/mc8051/fluttersmartconfig/FlutterSmartconfigPlugin.java

237 lines
7.3 KiB
Java

package de.mc8051.fluttersmartconfig;
import android.app.Activity;
import android.content.Context;
import android.Manifest;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.content.ContextWrapper;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Build.VERSION;
import android.os.Build.VERSION_CODES;
import android.content.pm.PackageManager;
import com.espressif.iot.esptouch.EsptouchTask;
import com.espressif.iot.esptouch.IEsptouchResult;
import com.espressif.iot.esptouch.IEsptouchTask;
import com.espressif.iot.esptouch.util.ByteUtil;
import com.espressif.iot.esptouch.util.EspNetUtil;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONArray;
import java.util.ArrayList;
import java.util.List;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.common.MethodChannel.MethodCallHandler;
import io.flutter.plugin.common.MethodChannel.Result;
import io.flutter.plugin.common.PluginRegistry.Registrar;
/** FlutterSmartconfigPlugin */
public class FlutterSmartconfigPlugin implements MethodCallHandler {
private IEsptouchTask mEsptouchTask;
private Context moContext;
private Activity moActivity;
private FlutterSmartconfigPlugin(Activity poActivity) {
this.moActivity = poActivity;
this.moContext = poActivity.getApplicationContext();
}
public static void registerWith(Registrar registrar) {
final MethodChannel channel = new MethodChannel(registrar.messenger(), "plugins.flutter.io/flutter_smartconfig");
channel.setMethodCallHandler(new FlutterSmartconfigPlugin(registrar.activity()));
}
@Override
public void onMethodCall(MethodCall call, Result result) {
if (call.method.equals("startSmartConfig")) {
String ssid = call.argument("ssid");
String bssid = call.argument("bssid");
String pass = call.argument("pass");
String deviceCount = call.argument("deviceCount");
String broadcast = call.argument("broadcast");
startSmartConfig(ssid, bssid, pass, deviceCount, broadcast, result);
} else if (call.method.equals("stopSmartConfig")) {
stopSmartConfig();
} else if (call.method.equals("getConnectedWiFiInfo")) {
getWifiInfo(result);
} else {
result.notImplemented();
}
}
private void getWifiInfo(Result result) {
final ConnectivityManager connManager = (ConnectivityManager) moContext.getSystemService(Context.CONNECTIVITY_SERVICE);
final NetworkInfo activeNetwork = connManager.getActiveNetworkInfo();
if (activeNetwork != null && activeNetwork.getType() == ConnectivityManager.TYPE_WIFI) {
final WifiManager wifiManager = (WifiManager) moContext.getSystemService(Context.WIFI_SERVICE);
final WifiInfo wifiInfo = wifiManager.getConnectionInfo();
if (wifiInfo != null) {
final String ssid = wifiInfo.getSSID().replaceAll("^\"|\"$", "");
final String bssid = wifiInfo.getBSSID();
String is5G = "unknow";
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
int frequency = wifiInfo.getFrequency();
if (frequency > 4900 && frequency < 5900) {
// Connected 5G wifi. Device does not support 5G
is5G = "yes";
} else {
is5G = "no";
}
}
JSONObject re = new JSONObject();
try {
re.put("ssid", ssid);
re.put("bssid", bssid);
re.put("is5G", is5G);
} catch (JSONException ex) {
result.error("getWifiInfo", ex.getMessage(), null);
return;
}
result.success(re.toString());
} else {
result.error("getWifiInfo", "Unable to obtain WiFi details", null);
}
} else {
result.error("getWifiInfo", "Not connected to WiFi", null);
}
}
public void stopSmartConfig() {
if (mEsptouchTask != null) {
mEsptouchTask.interrupt();
}
}
private void startSmartConfig(String ssid, String bssid, String pass, String deviceCount, String broadcast, final Result resultResp) {
stopSmartConfig();
byte[] apSsid = ByteUtil.getBytesByString(ssid);
byte[] apBssid = EspNetUtil.parseBssid2bytes(bssid);
byte[] apPassword = ByteUtil.getBytesByString(pass);
byte[] deviceCountData = deviceCount.getBytes();
byte[] broadcastData = broadcast.getBytes();
new EsptouchAsyncTask4(new TaskListener() {
@Override
public void onFinished(List < IEsptouchResult > result) {
// Do Something after the task has finished
try {
IEsptouchResult firstResult = result.get(0);
if (!firstResult.isCancelled()) {
if (firstResult.isSuc()) {
StringBuilder sb = new StringBuilder();
JSONArray jsonArray = new JSONArray();
for (IEsptouchResult resultInList: result) {
if (!resultInList.isCancelled() && resultInList.getBssid() != null) {
sb.append("Esptouch success, bssid = ")
.append(resultInList.getBssid())
.append(", InetAddress = ")
.append(resultInList.getInetAddress().getHostAddress())
.append("\n");
JSONObject re = new JSONObject();
re.put("bssid", resultInList.getBssid());
re.put("ip", resultInList.getInetAddress().getHostAddress());
jsonArray.put(re);
}
}
JSONObject configureDeviceObj = new JSONObject();
configureDeviceObj.put("devices", jsonArray);
resultResp.success(configureDeviceObj.toString());
} else {
resultResp.error("startSmartConfig", "Esptouch fail", null);
}
} else {
resultResp.error("startSmartConfig", "Esptouch cancelled", null);
}
} catch (Exception err) {
resultResp.error("startSmartConfig", err.getMessage(), null);
}
}
}).execute(apSsid, apBssid, apPassword, deviceCountData, broadcastData);
}
public interface TaskListener {
public void onFinished(List < IEsptouchResult > result);
}
private class EsptouchAsyncTask4 extends AsyncTask < byte[], Void, List < IEsptouchResult >> {
private final TaskListener taskListener;
public EsptouchAsyncTask4(TaskListener listener) {
// The listener reference is passed in through the constructor
this.taskListener = listener;
}
private final Object mLock = new Object();
@Override
protected void onPreExecute() {
}
@Override
protected List < IEsptouchResult > doInBackground(byte[]...params) {
int taskResultCount;
synchronized(mLock) {
byte[] apSsid = params[0];
byte[] apBssid = params[1];
byte[] apPassword = params[2];
byte[] deviceCountData = params[3];
byte[] broadcastData = params[4];
taskResultCount = deviceCountData.length == 0 ? -1 : Integer.parseInt(new String(deviceCountData));
mEsptouchTask = new EsptouchTask(apSsid, apBssid, apPassword, moContext);
mEsptouchTask.setPackageBroadcast(broadcastData[0] == 1); // true is broadcast, false is multicast
}
List < IEsptouchResult > resultList = mEsptouchTask.executeForResults(taskResultCount);
return resultList;
}
@Override
protected void onPostExecute(List < IEsptouchResult > result) {
IEsptouchResult firstResult = result.get(0);
// check whether the task is cancelled and no results received
if (!firstResult.isCancelled()) {
if (this.taskListener != null) {
// And if it is we call the callback function on it.
this.taskListener.onFinished(result);
}
}
}
}
}