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

这是中裕冠的mqtt插件

zyg_mqtt_plugin #

  • 这是中裕冠的mqtt插件

使用步骤 #

安装 #

在项目里的pubspec.yaml文件内添加,如:

dependencies:
  zyg_mqtt_plugin: ^0.0.2

引入,这使用的地方引入,如下: #


import 'package:zyg_mqtt_plugin/zyg_mqtt_plugin.dart';

1. 挂载 ZygMqttWidget(),示例如下: #

Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Plugin example app'),
        ),
        body: Center(
          child: Stack(
            children: [
              ZygMqttWidget(),
              
            ],
          ),
        ),
      ),
    );
  }

2.配置,(必须要先配置,再去连接) #

                        //1.先配置,再去连接
                        const productKey = 'a1WPnByrqko';
                        const deviceName = '1224708605@qq.com';
                        const deviceSecret = '3a694c1543fa8582ffc6d8ff1d7af6ca';
                        // 当前产品和设备所属地域的ID
                        // const region = 'us-west-1';
                        const region = 'cn-shanghai';

                        // const productKey = 'a3Q9ncgAkZp';
                        // const deviceName = '1224708605@qq.com';
                        // const deviceSecret = 'b5829c0cde2d0eb43989cc8e0c63f607';
                        // // 当前产品和设备所属地域的ID。
                        // const region = 'us-west-1';

                        // 接收消息的时候需要用到
                        String subscribeUrl =
                            '/${productKey}/${deviceName}/user/get';
                        //发送消息的时候需要用到
                        String publishUrl =
                            '/${productKey}/${deviceName}/user/update';
                        //配置
                        ZygMqttPlugin().setConfigure(productKey, deviceName,
                            deviceSecret, region, subscribeUrl, publishUrl);

3. 设置连接状态回调 #


                        //连接状态回调
                        ZygMqttPlugin().myConnectStateCallback = (state) {
                          if (ZygConnectState.connected == state) {
                            print('已连接');
                            setState(() {
                              connectState = '已连接';
                            });
                          } else if (ZygConnectState.disconnect == state) {
                            print('已断开');
                            connectState = '已断开';
                          }
                        };


4. 设置接收消息回调 #

                        //接收消息回调, rawData是接收到的原始数据,mapData是json格式的数据
                        ZygMqttPlugin().myReceiveCallback = (rawData, jsonData) {
                          //
                          setState(() {
                            receiveDataList.add(rawData.toString());
                          });
                        };

5. 去连接mqtt #

                        //连接
                        ZygMqttPlugin().goToConnetMqtt();

6. 去发送mqtt消息 #

                        //获取电量的指令
                        Map map = {
                          "target_device": "89860124801080169975",
                          "cmd": "detect_battery"
                        };

                        String msg = jsonEncode(map);

                        //发送数据
                        ZygMqttPlugin().sendData(msg);
                        
0
likes
125
points
43
downloads

Publisher

unverified uploader

Weekly Downloads

这是中裕冠的mqtt插件

Homepage

Documentation

API reference

License

BSD-3-Clause (license)

Dependencies

flutter, path_provider, plugin_platform_interface, shared_preferences, webview_flutter

More

Packages that depend on zyg_mqtt_plugin