Flutter插件mxali的介绍与使用方法解析

Flutter插件mxali的介绍与使用方法解析

mxali介绍

这是一个新的Flutter项目,专注于提供跨平台的功能支持。该插件通过实现Android和iOS平台的具体代码,为开发者提供了丰富的功能。

Flutter插件mxali使用说明

Getting Started(入门)

此项目是一个插件包的起点,适合那些希望开发插件的Flutter开发者。有关如何开始Flutter开发的更多信息,请查看官方文档,其中包含教程、示例、移动开发指南以及完整的API参考。


示例代码

以下是mxali插件的一个完整示例,展示了其潜在的使用场景。

示例代码详解

import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:mxali/mxali.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatefulWidget {
  const MyApp({super.key});

  [@override](/user/override)
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  Map scanMap = {}; // 存储扫描结果
  String productKey = ""; // 设备产品密钥
  String deviceName = ""; // 设备名称
  String iotId = ""; // 设备ID
  String isloginResut = "0"; // 登录状态

  [@override](/user/override)
  void initState() {
    super.initState();

    // 监听数据接收
    onLisenData();

    // 检查设备是否已登录
    Mxali.instance.deviceIsLogin().then((value) {
      String isLogin = value;

      setState(() {
        isloginResut = isLogin;
        if (isloginResut == "0") {
          // 如果未登录,可以尝试登录
          // Mxali.instance.loginAccount();
        }
      });
    });
  }

  void onLisenData() {
    // 监听消息接收
    Mxali.instance.onMessageRecive(
      sucCallBack: (value) {
        print("-->>value:$value");

        if (value is String) {
          Map map = jsonDecode(value);
          String state = map["state"];

          if (state == "0") { // 失败
            Map respose = map["response"];
            String code = respose["code"];

            if (code == "401") { // 设备未登录
              // 处理401错误
            } else {
              // 其他错误处理
            }
          } else { // 成功
            String key = map["key"];
            String action = map["action"];

            if (key == "device") { // 设备类
              Map response = map["response"];

              if (action == "scanDevice") { // 扫描设备
                List devices = response["devices"];
                scanMap = devices[0];
              } else if (action == "stopScanDevice") { // 停止扫描
                // 停止扫描逻辑
              } else if (action == "setDevice") { // 设置待添加设备信息
                // 设置设备信息
              } else if (action == "addDevicing") { // 配网中
                String subState = response["state"];
                if (subState == "1") { // 正在配网
                  // 正在配网逻辑
                } else if (subState == "2") { // 输入密码页面
                  // 输入密码逻辑
                } else if (subState == "3") { // 完成输入密码
                  // 密码输入完成逻辑
                }
              } else if (action == "addDevice") { // 配网成功
                productKey = response["productKey"];
                deviceName = response["deviceName"];
              } else if (action == "stopAddDevice") { // 停止配网
                // 停止配网逻辑
              } else if (action == "getbindDeviceToken") { // 获取绑定token
                // 获取绑定token逻辑
              } else if (action == "bindDevice") { // 绑定设备成功
                // 绑定设备成功逻辑
              } else if (action == "getDeviceList") { // 获取设备列表
                List dataList = response["data"];
                Map deviceMap = dataList[0];
                iotId = deviceMap["iotId"];
              } else if (action == "deleteDevice") { // 删除设备
                // 删除设备逻辑
              }
            } else if (key == "account") { // 账号类
              // 账号相关逻辑
            } else if (key == "mqtt") { // mqtt类
              // MQTT相关逻辑
            } else if (key == "location") { // 定位
              if (action == "startLocation") { // 开始定位
                // 开始定位逻辑
              } else if (action == "endLocation") { // 结束定位
                // 结束定位逻辑
              }
            }
          }
        }
      },
      errCallBack: (error) {
        print("-->>error:$error");
      },
    );
  }

  [@override](/user/override)
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Plugin example app'),
        ),
        body: isloginResut == "0"
            ? Center(
                child: ElevatedButton(
                  onPressed: () {
                    Mxali.instance.loginAccount();
                  },
                  child: Text(isloginResut),
                ),
              )
            : Container(
                alignment: Alignment.center,
                margin: const EdgeInsets.all(20),
                child: ListView(
                  shrinkWrap: true,
                  children: [
                    ElevatedButton(
                      onPressed: () async {
                        Mxali.instance.registerAccount();
                      },
                      child: const Text("注册"),
                    ),
                    const SizedBox(height: 20),
                    ElevatedButton(
                      onPressed: () async {
                        Mxali.instance.loginAccount();
                      },
                      child: const Text("登录"),
                    ),
                    const SizedBox(height: 20),
                    ElevatedButton(
                      onPressed: () async {
                        Mxali.instance.forgotAccount();
                      },
                      child: const Text("忘记密码"),
                    ),
                    const SizedBox(height: 20),
                    ElevatedButton(
                      onPressed: () async {
                        Mxali.instance.loginOutAccount();
                      },
                      child: const Text("退出登录"),
                    ),
                    const SizedBox(height: 20),
                    ElevatedButton(
                      onPressed: () async {
                        Mxali.instance.deviceIsLogin();
                      },
                      child: const Text("设备服务是否登录"),
                    ),
                    const SizedBox(height: 20),
                    ElevatedButton(
                      onPressed: () async {
                        Mxali.instance.deviceList();
                      },
                      child: const Text("获取设备列表"),
                    ),
                    const SizedBox(height: 20),
                    ElevatedButton(
                      onPressed: () async {
                        Mxali.instance.scanDevices();
                      },
                      child: const Text("扫描设备"),
                    ),
                    const SizedBox(height: 20),
                    ElevatedButton(
                      onPressed: () async {
                        Mxali.instance.stopScanDevices();
                      },
                      child: const Text("停止扫描设备"),
                    ),
                    const SizedBox(height: 20),
                    ElevatedButton(
                      onPressed: () async {
                        // Mxali.instance.setAddDevices();
                      },
                      child: const Text("选择设备开始预添加"),
                    ),
                    const SizedBox(height: 20),
                    ElevatedButton(
                      onPressed: () async {
                        Mxali.instance.setAddDevices("c4d7fd877609");
                        Mxali.instance.startAddDevice();
                      },
                      child: const Text("开始配网"),
                    ),
                    const SizedBox(height: 20),
                    ElevatedButton(
                      onPressed: () async {
                        String ssid = "MU_guest";
                        String pwd = "muxintech";
                        String timeout = "60";
                        Mxali.instance.setSSidAndpWd(ssid, pwd, timeout);
                      },
                      child: const Text("输入WiFi"),
                    ),
                    const SizedBox(height: 20),
                    ElevatedButton(
                      onPressed: () async {
                        if (productKey != "" && deviceName != "") {
                          Mxali.instance.deviceBind(productKey, deviceName);
                        }
                      },
                      child: const Text("绑定设备"),
                    ),
                    const SizedBox(height: 20),
                    ElevatedButton(
                      onPressed: () async {
                        Mxali.instance.deviceDelete(iotId);
                      },
                      child: const Text("删除设备"),
                    ),
                    const SizedBox(height: 20),
                    ElevatedButton(
                      onPressed: () async {
                        Mxali.instance.refreshCode();
                      },
                      child: const Text("刷新code"),
                    ),
                    const SizedBox(height: 20),
                    ElevatedButton(
                      onPressed: () async {
                        Mxali.instance.iotOpenALiListener();
                      },
                      child: const Text("开启iot监听"),
                    ),
                    const SizedBox(height: 20),
                    ElevatedButton(
                      onPressed: () async {
                        String path = "event/property/set";
                        Mxali.instance.iotsubscribeTopic(path);
                      },
                      child: const Text("订阅"),
                    ),
                    const SizedBox(height: 20),
                    ElevatedButton(
                      onPressed: () async {
                        String path = "event/property/set";
                        Mxali.instance.iotCancelSubscribeTopic(path);
                      },
                      child: const Text("取消订阅"),
                    ),
                    const SizedBox(height: 20),
                    ElevatedButton(
                      onPressed: () async {
                        String path = "event/property/post";
                        Map params = {"BodyTemperature": 1234};
                        Mxali.instance.iotPublishTopic(path, params);
                      },
                      child: const Text("发布订阅"),
                    ),
                    const SizedBox(height: 20),
                    ElevatedButton(
                      onPressed: () async {
                        Mxali.instance.deviceBaseAPITest();
                      },
                      child: const Text("测试iot通道"),
                    ),
                    const SizedBox(height: 20),
                    ElevatedButton(
                      onPressed: () async {
                        Mxali.instance.getUserInfoAccount();
                      },
                      child: const Text("获取用户信息"),
                    ),
                    const SizedBox(height: 20),
                    ElevatedButton(
                      onPressed: () async {
                        Mxali.instance.startLocation();
                      },
                      child: const Text("开始实时定位"),
                    ),
                    const SizedBox(height: 20),
                    ElevatedButton(
                      onPressed: () async {
                        Mxali.instance.endLocation();
                      },
                      child: const Text("停止实时定位"),
                    ),
                    const SizedBox(height: 20),
                  ],
                ),
              ),
      ),
    );
  }
}

更多关于Flutter插件mxali的介绍与使用方法解析的实战教程也可以访问 https://www.itying.com/category-92-b0.html

回到顶部