-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
57 additions
and
82 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,4 +12,8 @@ | |
|
||
## 0.0.4 | ||
|
||
* update. | ||
|
||
## 0.0.4+1 | ||
|
||
* update. |
133 changes: 52 additions & 81 deletions
133
android/src/main/java/com/iotserv/flutter_oneshot/FlutterOneshotPlugin.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,100 +1,71 @@ | ||
package com.iotserv.flutter_oneshot; | ||
|
||
import android.app.Activity; | ||
import android.net.wifi.WifiManager; | ||
import android.util.Log; | ||
|
||
import androidx.annotation.NonNull; | ||
|
||
import com.winnermicro.smartconfig.ConfigType; | ||
import com.winnermicro.smartconfig.IOneShotConfig; | ||
import com.winnermicro.smartconfig.SmartConfigFactory; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import io.flutter.embedding.engine.plugins.FlutterPlugin; | ||
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 com.winnermicro.smartconfig.*; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
/** FlutterOneshotPlugin */ | ||
/** | ||
* FlutterOneshotPlugin | ||
*/ | ||
public class FlutterOneshotPlugin implements FlutterPlugin, MethodCallHandler { | ||
/// The MethodChannel that will the communication between Flutter and native Android | ||
/// | ||
/// This local reference serves to register the plugin with the Flutter Engine and unregister it | ||
/// when the Flutter Engine is detached from the Activity | ||
private int timeout = 60;//miao | ||
private FlutterPluginBinding myFlutterPluginBinding; | ||
private MethodChannel channel; | ||
/// The MethodChannel that will the communication between Flutter and native Android | ||
/// | ||
/// This local reference serves to register the plugin with the Flutter Engine and unregister it | ||
/// when the Flutter Engine is detached from the Activity | ||
private int timeout = 60;//miao | ||
private FlutterPluginBinding myFlutterPluginBinding; | ||
private MethodChannel channel; | ||
|
||
private String ssid; | ||
private String password = null; | ||
private IOneShotConfig oneshotConfig = null; | ||
private SmartConfigFactory factory = null; | ||
private String ssid; | ||
private String password = null; | ||
private IOneShotConfig oneshotConfig = null; | ||
private SmartConfigFactory factory = null; | ||
|
||
@Override | ||
public void onAttachedToEngine(@NonNull FlutterPluginBinding flutterPluginBinding) { | ||
myFlutterPluginBinding = flutterPluginBinding; | ||
channel = new MethodChannel(flutterPluginBinding.getBinaryMessenger(), "flutter_oneshot"); | ||
channel.setMethodCallHandler(this); | ||
} | ||
|
||
@Override | ||
public void onMethodCall(@NonNull MethodCall call, @NonNull Result result) { | ||
if (call.method.equals("getPlatformVersion")) { | ||
result.success("Android " + android.os.Build.VERSION.RELEASE); | ||
} else if (call.method.equals("start")){ | ||
//参数获取 | ||
ssid = call.argument("ssid"); | ||
password = call.argument("password"); | ||
try { | ||
timeout = call.argument("timeout"); | ||
}catch (Exception e){ | ||
e.printStackTrace(); | ||
timeout = 30; | ||
} | ||
new Thread(new UDPReqThread(result)).start(); | ||
} | ||
else { | ||
result.notImplemented(); | ||
@Override | ||
public void onAttachedToEngine(@NonNull FlutterPluginBinding flutterPluginBinding) { | ||
myFlutterPluginBinding = flutterPluginBinding; | ||
channel = new MethodChannel(flutterPluginBinding.getBinaryMessenger(), "flutter_oneshot"); | ||
channel.setMethodCallHandler(this); | ||
} | ||
} | ||
|
||
@Override | ||
public void onDetachedFromEngine(@NonNull FlutterPluginBinding binding) { | ||
channel.setMethodCallHandler(null); | ||
} | ||
|
||
class UDPReqThread implements Runnable { | ||
private Result result; | ||
public UDPReqThread(Result result) | ||
{ | ||
this.result = result; | ||
@Override | ||
public void onMethodCall(@NonNull MethodCall call, @NonNull Result result) { | ||
if (call.method.equals("getPlatformVersion")) { | ||
result.success("Android " + android.os.Build.VERSION.RELEASE); | ||
} else if (call.method.equals("start")) { | ||
//参数获取 | ||
ssid = call.argument("ssid"); | ||
password = call.argument("password"); | ||
try { | ||
timeout = call.argument("timeout"); | ||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
timeout = 30; | ||
} | ||
factory = new SmartConfigFactory(); | ||
oneshotConfig = factory.createOneShotConfig(ConfigType.UDP); | ||
oneshotConfig.start(ssid, password, timeout, myFlutterPluginBinding.getApplicationContext()); | ||
final Map<String, String> ret = new HashMap<String, String>(); | ||
ret.put("result", "success"); | ||
result.success(ret); | ||
} else { | ||
result.notImplemented(); | ||
} | ||
} | ||
|
||
public void run() { | ||
final Map<String, String> ret = new HashMap<String, String>(); | ||
factory = new SmartConfigFactory(); | ||
oneshotConfig = factory.createOneShotConfig(ConfigType.UDP); | ||
// start config | ||
try { | ||
oneshotConfig.start(ssid, password, timeout, myFlutterPluginBinding.getApplicationContext()); | ||
} | ||
catch (OneShotException e) { | ||
Log.d("===oneshot-OneShotE===",e.getMessage()); | ||
e.printStackTrace(); | ||
int code = e.getErrorID(); | ||
Log.d("onshot err", String.valueOf(code)); | ||
} | ||
catch (Exception e) { | ||
Log.d("===oneshot-exception===",e.getMessage()); | ||
e.printStackTrace(); | ||
} finally { | ||
oneshotConfig.stop( ); | ||
ret.put("result","success"); | ||
Log.d("===oneshot-success===","success"); | ||
result.success(ret); | ||
} | ||
@Override | ||
public void onDetachedFromEngine(@NonNull FlutterPluginBinding binding) { | ||
channel.setMethodCallHandler(null); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters