生活物联网平台支持您在自有App中集成批量配网功能,可以实现一次性为多个相同型号的设备配网,单次最多可批量配网20个设备。
前提条件
已在控制台创建产品,并完成产品的开发工作。 产品的默认配网方案需要设置为设备热点配网。详细请参见配置App配网方案和引导文案。 已完成设备热点配网和零配配网功能的开发。详细请参见Wi-Fi设备配网方案介绍。 已完成App端配网功能的开发。详细请参见配网开发指南。方案介绍
批量配网方案是基于设备热点配网方案的基础上开发的方案,因此设备需要支持设备热点配网和零配配网。批量配网方案的时序图如下所示。
设备上电或操作设备按键,将所有待添加的设备进入设备热点配网模式。 通过App对其中一个设备进行设备热点配网,并完成配网及绑定操作。调用的SDK请参见Android App SDK和iOS App SDK。 指定2中完成配网的设备为主设备,并通过主设备去发现其他待配网设备。主设备发送广播消息,其他待配网设备接收到广播消息后切换至零配模式。此时,主设备可以发现进入零配模式的待配网设备,并在App SDK回调中反馈搜索到的设备信息。
3中返回的其他待配网设备,通过零配配网方式完成配网,并在App SDK回调中会反馈配网结果。集成设备端
获取生活物联网平台SDK V1.3.0及以上版本。请参见SDK概述与开发环境设置。 使能SDK中功能模块。在应用的mk文件中增加配置项GLOBAL_CFLAGS += -DAWSS_BATCH_DEVAP_ENABLE。如您基于living_platform开发,在living_platform.mk文件中已包含该配置项,如下图所示。
设备端SDK以及芯片需要支持的基本能力如下。
SDK的基本能力要求 支持设备热点配网功能 支持零配配网功能 支持设备热点批量配网功能 芯片或模组的能力要求 支持设备热点开启状态下接收Wi-Fi管理帧能力 支持连接AP状态下切信道发送Wi-Fi管理帧能力 支持设备热点批量配网的设备,设备热点必须设置在1信道、6信道或11信道中 处理应用中调用SDK功能模块接口以及回调。 在应用示例中的awss_open_dev_ap()函数中增加awss_dev_ap_reg_modeswit_cb()函数的调用。void awss_open_dev_ap(void *p) { iotx_event_regist_cb(linkkit_event_monitor); LOG("%s\n", __func__); if (netmgr_start(false) != 0) { aos_msleep(2000); #ifdef AWSS_BATCH_DEVAP_ENABLE awss_dev_ap_reg_modeswit_cb(awss_dev_ap_modeswitch_cb); //增加此调用 #endif awss_dev_ap_start(); } aos_task_exit(0); } 实现应用层的回调函数awss_dev_ap_modeswitch_cb()。
#ifdef AWSS_BATCH_DEVAP_ENABLE #define DEV_AP_ZCONFIG_TIMEOUT_MS 120000 // (ms) extern void awss_set_config_press(uint8_t press); extern uint8_t awss_get_config_press(void); extern void zconfig_80211_frame_filter_set(uint8_t filter, uint8_t fix_channel); void do_awss_dev_ap(); static aos_timer_t dev_ap_zconfig_timeout_timer; static uint8_t g_dev_ap_zconfig_timer = 0; // this timer create once and can restart static uint8_t g_dev_ap_zconfig_run = 0; static void timer_func_devap_zconfig_timeout(void *arg1, void *arg2) { LOG("%s run\n", __func__); if (awss_get_config_press()) { // still in zero Wi-Fi provision stage, should stop and switch to dev ap do_awss_dev_ap(); } else { // zero Wi-Fi provision finished } awss_set_config_press(0); zconfig_80211_frame_filter_set(0xFF, 0xFF); g_dev_ap_zconfig_run = 0; aos_timer_stop(&dev_ap_zconfig_timeout_timer); } static void awss_dev_ap_switch_to_zeroconfig(void *p) { LOG("%s run\n", __func__); // Stop dev ap Wi-Fi provision awss_dev_ap_stop(); // Start and enable zero Wi-Fi provision iotx_event_regist_cb(linkkit_event_monitor); awss_set_config_press(1); // Start timer to count duration time of zero provision timeout if (!g_dev_ap_zconfig_timer) { aos_timer_new(&dev_ap_zconfig_timeout_timer, timer_func_devap_zconfig_timeout, NULL, DEV_AP_ZCONFIG_TIMEOUT_MS, 0); g_dev_ap_zconfig_timer = 1; } aos_timer_start(&dev_ap_zconfig_timeout_timer); // This will hold thread, when awss is going netmgr_start(true); LOG("%s exit\n", __func__); aos_task_exit(0); } int awss_dev_ap_modeswitch_cb(uint8_t awss_new_mode, uint8_t new_mode_timeout, uint8_t fix_channel) { if ((awss_new_mode == 0) && !g_dev_ap_zconfig_run) { g_dev_ap_zconfig_run = 1; // Only receive zero provision packets zconfig_80211_frame_filter_set(0x00, fix_channel); LOG("switch to awssmode %d, mode_timeout %d, chan %d\n", 0x00, new_mode_timeout, fix_channel); // switch to zero config aos_task_new("devap_to_zeroconfig", awss_dev_ap_switch_to_zeroconfig, NULL, 2048); } } #endif 编译固件,并烧录到设备中。
集成iOS App端
获取SDK。确保配网SDK版本升级到1.11.0及以上,建议使用官网最新版本。
您可以从生活物联网平台的控制台下载SDK(请参见下载并集成SDK),也可以集成以下代码。
pod 'IMSDeviceCenter', '~>1.11.0' 开始或停止批量发现。
/** 批量发现功能,通过已配网设备发现周边其他设备,需特定设备支持 @param targetProductKey 搜索目标设备ProductKey,若为nil,则目标为所有设备 @param searcherProductKey 作为搜索者的已配网设备的ProductKey @param searchDeviceName 作为搜索者的已配网设备的DeviceName @param batchResultBlock 搜索到设备时触发的回调,会调用多次 */ - (void)startBatchDiscoveryForTargetProductKey:(NSString *) targetProductKey bySearcherProductKey:(NSString *) searcherProductKey deviceName:(NSString *) searchDeviceName resultBlock:(void(^)(NSArray * devices, NSError * err))batchResultBlock; /** 停止已配网设备搜索周边设备 */ - (void)stopBatchDiscovery; 开始批量配网。
IMLCandDeviceModel *model = [[IMLCandDeviceModel alloc] init]; model.productKey = productKey; //待配网设备的ProductKey model.regProductKey = productKey; // 已配好设备的ProductKey model.regDeviceName = deviceName; // 已配好设备的DeviceName model.linkType = ForceAliLinkTypeZeroInBatches; //指定配网模式为批量配网 [[IMLAddDeviceBiz sharedInstance] setDevice:model]; [[IMLAddDeviceBiz sharedInstance] startAddDevice:notifier]; 批量配网成功回调。
与普通配网类似,回调会通过notifyProvisionResult反馈。每成功配网一个设备就回调一次。若发生回调失败,则表示此次批量配网全部失败或者整体超时。
/** 通知上层UI:配网完成结果回调 @param candDeviceModel 配网结果设备信息返回:配网失败时为nil @param provisionError 错误信息 */ - (void)notifyProvisionResult:(IMLCandDeviceModel *)candDeviceModel withProvisionError:(NSError *)provisionError;
集成Android App端
获取SDK。您可以从生活物联网平台的控制台下载SDK(请参见下载并集成SDK),也可以集成以下代码。
// maven仓库地址 maven { url "http://maven.aliyun.com/nexus/content/repositories/releases" } api('com.aliyun.alink.linksdk:ilop-devicecenter:1.7.3') 开始或停止批量发现。
API reference 参见 LocalDeviceMgr。
/** * 参数无效会抛出 IllegalArgumentException * @param context context * @param params {@link BatchDiscoveryParams} * @param listener discovery results, callback at least once */ void startBatchDiscovery(Context context, BatchDiscoveryParams params, IDiscovery listener); /** * 停止批量配网 * @param productKey 已配网设备ProductKey * @param deviceName 已配网设备DeviceName */ void stopBatchDiscovery(String productKey, String deviceName); 开始批量配网并监听回调结果。
startAddDevice各回调说明参见配网SDK。
DeviceInfo info = new DeviceInfo(); info.linkType = LinkType.ALI_ZERO_IN_BATCHES.getName(); info.productKey = pk; // 待配网设备ProductKey info.regProductKey = regPk; // 已配网设备ProductKey info.regDeviceName = regdn; // 已配网设备DeviceName // 设置配网信息 AddDeviceBiz.getInstance().setDevice(info); // 开始批量配网 AddDeviceBiz.getInstance().startAddDevice(context, new IAddDeviceListener() { @Override public void onPreCheck(boolean isSuccess, DCErrorCode dcErrorCode) { // 预检查 } @Override public void onProvisionPrepare(int prepareType) { } @Override public void onProvisioning() { // 配网中 } @Override public void onProvisionStatus(ProvisionStatus provisionStatus) { } @Override public void onProvisionedResult(boolean isSuccess, DeviceInfo deviceInfo, DCErrorCode dcErrorCode) { // 配网结果回调,每成功配网一个设备就回调一次。请您根据配网开始时发现的设备列表和返回的配网成功的次数来判断是否配网结束。 } });