startAddDevice static method

Future<bool?> startAddDevice(
  1. String linkType,
  2. dynamic callback(
    1. String stage,
    2. dynamic stageData
    ), {
  3. String? productKey,
  4. String? productId,
  5. String? id,
  6. String? protocolVersion,
  7. ValueGetter<Future<Map<String, String>>>? getWifi,
})

linkType 应该为下列值:

设备热点配网:ForceAliLinkTypeSoftAP
蓝牙辅助配网:ForceAliLinkTypeBLE
二维码配网:ForceAliLinkTypeQR
手机热点配网:ForceAliLinkTypePhoneAP
一键配网:ForceAliLinkTypeBroadcast
零配:ForceAliLinkTypeZeroAP

Implementation

static Future<bool?> startAddDevice(
  String linkType,
  callback(String stage, dynamic stageData), {
  String? productKey,
  String? productId,
  String? id,
  String? protocolVersion,
  ValueGetter<Future<Map<String, String>>>? getWifi,
}) async {
  assert(linkType == "ForceAliLinkTypeBroadcast" && getWifi != null, "一键配网 需要提供wifi和密码");
  assert(linkType == "ForceAliLinkTypeBroadcast" && productKey != null, "一键配网 需要提供码productKey");
  Map data = {"productKey": productKey, "productId": productId, "id": id, "linkType": linkType, "protocolVersion": protocolVersion};
  if (AliIotPlugin.debug) {
    print("$TAG : startAddDevice $data");
  }
  _dispatchNetSubscription = AliIotPlugin.eventChannel.receiveBroadcastStream("startAddDevice").listen((event) {
    if (event != null) {
      print("$TAG : startAddDevice event: " + event.toString());
      if (event is List) {
        final stage = event.first;
        var stageData;
        switch (stage) {
          case "onProvisionPrepare":
            stageData = event.last; //int
            break;
          case "onProvisioning":
            stageData = ""; //String
            break;
          case "onProvisionStatus":
            stageData = {"code": event[1], "message": event[2], "extraParams": event[3]}; //Map
            break;
          case "onProvisionedResult":
            var temp = event.last as Map; //{"isSuccess":bool,"deviceInfo":String."errorCode":String}
            stageData = {
              "isSuccess": temp["isSuccess"],
              "deviceInfo": jsonDecode(temp["deviceInfo"] ?? "{}"),
              "errorCode": jsonDecode(temp["errorCode"] ?? "{}"),
            };
            break;
        }
        callback(stage, stageData);
      }
    }
  });
  AliIotPlugin._methodChannel.setMethodCallHandler((MethodCall methodCall) {
    print("$TAG : _methodChannelHandler $methodCall");
    switch (methodCall.method) {
      case 'toggleProvision':
        assert(getWifi != null, "toggleProvision is null, 一键配网 需要提供wifi和密码");
        return getWifi!();
      default:
        return Future<dynamic>.value();
    }
  });
  return AliIotPlugin.methodChannel.invokeMethod('startAddDevice', data);
}