mxali 0.0.2 copy "mxali: ^0.0.2" to clipboard
mxali: ^0.0.2 copied to clipboard

阿里生活物联网,包括内置登录系统,蓝牙辅助配网,设备管理,mqtt连接

example/lib/main.dart

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
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
 Map scanMap = {};
 String productKey = "";
 String deviceName = "";
 String iotId = "";
 String isloginResut = "0";

  @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){
     // print("");
      Map map = jsonDecode(value);
      String state = map["state"];
      if(state == "0"){//失败
        Map respose = map["response"];
          String code = respose["code"];
       // String jsonMsg = respose["msg"];
        //Map msdDic = jsonDecode(jsonMsg);
      // String errorMsg = msdDic["msg"];
       if(code  == "401"){ //设备未登录

       }else{ //其他错误

       }
       //print("");
      }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"];
            //  print("");
           }else if(action == "stopAddDevice"){ //停止配网
              //  print("");
           }else if(action == "getbindDeviceToken"){ //获取绑定token
           //  print("");
           }else if(action == "bindDevice"){ //绑定设备成功
            //  print("");
           }else if(action == "getDeviceList"){ //获取设备列表
          List dataList = response["data"];
          Map deviceMap = dataList[0];
          iotId = deviceMap["iotId"];
            //  print("");
           }else if(action == "deleteDevice"){ //删除设备
              //print("");
           }
       
         }else if(key == "account"){ //账号类

         }else if(key == "mqtt"){ //mqtt类

         }else if(key == "location"){ //定位
             if(action == "startLocation"){ //开始

             }else if(action == "endLocation"){ //结束

             }
         }
       
      }
      
     }
     // print("");
    },errCallBack:(error) {
     //  print("-->>error:$error");
     //  print("");
    },);
  }

 @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{
                  //mac
                  //String mac = scanMap["mac"];
                 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.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,),

              ],
             ),
        ),
      ),
    );
  }
}
1
likes
130
points
28
downloads

Publisher

unverified uploader

Weekly Downloads

阿里生活物联网,包括内置登录系统,蓝牙辅助配网,设备管理,mqtt连接

Homepage

Documentation

API reference

License

MIT (license)

Dependencies

flutter, flutter_web_plugins, plugin_platform_interface

More

Packages that depend on mxali