NB-IoT 设备配网

更新时间:2024-08-22 02:19:00下载pdf

NB-IoT 指窄带物联网(Narrowband Internet of Things),构建于蜂窝网络,支持低功耗设备在广域网的蜂窝数据链接,支持待机时间长、对网络连接要求较高设备的高效连接。通俗地说,NB-IoT 设备就是一台带有 SIM 卡的设备,硬件设备在制造过程中,会采用内嵌 SIM 卡或者预留卡槽、后期再插入 SIM 的方式。而 NB-IoT 配网是指 NB-IoT 设备通过蜂窝网络到云端,进行激活注册的流程。

ThingSmartBusinessExtensionKit 组件提供了比 ThingSmartActivator 更多的功能。如果您仍直接在使用 ThingSmartActivator,请参考 此链接

## 配网流程
AppNB-IoT 设备业务拓展 SDK云端设备接通电源注册设备1根据事件判断任务注册设备2注册设备信息3返回设备 ID 和其他信息4返回注册结果5扫描二维码获取到设备信息6向用户账号绑定设备7绑定设备返回绑定结果8返回绑定结果,记录 UID9返回绑定结果,设备配网成功10AppNB-IoT 设备业务拓展 SDK云端NB-IoT 设备配网流程

二维码解析接口

扫描设备二维码,获取 URL,然后通过通用接口获取设备信息。通过接口返回的参数,确定该二维码是否是 NB-IoT 设备二维码。

@protocol ThingSmartActivatorRequestProtocol <NSObject>

/// Provide QR code parsing capability
/// @param param Request Parameters
/// @param success Network success callback
/// @param failure Network Failure Callbacks
- (void)requestParseQRCodeWithParam:(ThingActivatorParseQRCodeRequestData *)param success:(void(^)(ThingSmartAScanCodeModel *result))success failure:(ThingActivatorCallbackError)failure;
@end

参数说明

参数 说明
param 请求参数
success 解析成功返回信息
failure 解析失败回调

返回结果说明:

@interface ThingSmartAScanCodeModel : NSObject
@property (nonatomic, strong) NSString * _Nullable actionName;
@property (nonatomic, assign) id _Nullable actionData;
@end

NB-IoT 设备对应 actionName"device_net_conn_bind_nb",具体使用可参考示例代码。

注册配网类型

基座初始化时,需要注册一下配网的类型,NB-IoT 设备对应为 ThingSmartActivatorTypeNBModel

/// Initialize network configuration types
/// @param typeList Network configuration types
- (void)registerWithActivatorList:(NSArray<ThingSmartActivatorTypeModel *>*)typeList;

参数说明

参数 说明
typeList 配网类型列表

设备激活

接口说明

/// Activate devices with a single network configuration type
/// @param type Network configuration type
/// @param deviceList Devices to be activated
- (void)startActive:(ThingSmartActivatorTypeModel *)type deviceList:(NSArray<ThingSmartActivatorDeviceModel *>*)deviceList;

参数说明

参数 说明
type 配网类型
deviceList 待激活设备列表,目前仅支持单个设备

停止激活

接口说明

/// Stop activating devices
/// @param typeList Array of network configuration types
/// @param clearCache Whether to clear the cache
- (void)stopActive:(NSArray <ThingSmartActivatorTypeModel *>*)typeList clearCache:(BOOL)clearCache;

参数说明

参数 说明
typeList 配网类型
clearCache 清空缓存设备信息

设备激活回调

接口说明

// Device network configuration result callback
/// @param service Device network configuration implementation object
/// @param type Network configuration type
/// @param devices Devices being configured
/// @param errorModel Error encountered during network configuration
- (void)activatorService:(id<ThingSmartActivatorActiveProtocol>)service
           activatorType:(ThingSmartActivatorTypeModel *)type
       didReceiveDevices:(nullable NSArray<ThingSmartActivatorDeviceModel *> *)devices
                   error:(nullable ThingSmartActivatorErrorModel *)errorModel;

参数说明

参数 说明
service 配网服务
type 配网类型
devices 激活成功设备
errorModel 如果配网失败或者超时,返回此模型,成功时返回 nil

错误码说明

配网失败或者配网超时的情况下,会返回 ThingSmartActivatorErrorModel

@interface ThingSmartActivatorErrorModel : NSObject
@property (nonatomic, strong) ThingSmartActivatorDeviceModel *deviceModel;
@property (nonatomic) NSError *error;
@end

其中 error 对应的错误码,定义在 ThingSmartActivatorDiscoveryError 中。

示例代码

Swift

class NBmodeConfigurationVC: UITableViewController {

    private var token: String = ""
    private var typeModel:ThingSmartActivatorTypeNBModel = {
        let type = ThingSmartActivatorTypeNBModel()
        type.type = ThingSmartActivatorType.NB
        type.typeName = NSStringFromThingSmartActivatorType(ThingSmartActivatorType.NB)
        type.timeout = 120
        type.spaceId = Home.current!.homeId
        return type
    }()


    lazy var discovery: ThingSmartActivatorDiscovery = {
        let discovery = ThingSmartActivatorDiscovery()
        discovery.register(withActivatorList: [self.typeModel])
        discovery.setupDelegate(self)
        discovery.loadConfig()
        return discovery
    }()

    var request: ThingSmartActivatorDiscoveryRequest = {
        let request = ThingSmartActivatorDiscoveryRequest()
        return request
    }()

    private func startConfiguration(qrcode codeStr: String?) -> Void{
        SVProgressHUD.show()
        let requestDate = ThingActivatorParseQRCodeRequestData()
        requestDate.code = codeStr ?? ""
        request.requestParseQRCode(withParam: requestDate) {codeModel in
            if codeModel.actionName == "device_net_conn_bind_nb" {
                self.typeModel.nbId = codeModel.actionData?.object(forKey: "id") as! String
                self.discovery.startActive(self.typeModel, deviceList: [])
            }
        } failure: { error in

        }
    }
}

extension NBmodeConfigurationVC: ThingSmartActivatorActiveDelegate {
    func activatorService(_ service: ThingSmartActivatorActiveProtocol, activatorType type: ThingSmartActivatorTypeModel, didReceiveDevices devices: [ThingSmartActivatorDeviceModel]?, error errorModel: ThingSmartActivatorErrorModel?) {
        if errorModel != nil {
            SVProgressHUD.showError(withStatus: "Bind Failure. (\(errorModel?.error.localizedDescription ?? ""))")
            return
        }

        let device = devices?.first
        SVProgressHUD.show(withStatus: "Bind Success. \n devId: \(device?.uniqueID ?? "") \n name: \(device?.name ?? "")")
    }
}

Objective-C

- (void)qrParse:(NSString *)url {
    ThingActivatorParseQRCodeRequestData *param = [[ThingActivatorParseQRCodeRequestData alloc] init];
    param.code = url;

   [self.requestService requestParseQRCodeWithParam:param success:^(ThingSmartAScanCodeModel * _Nonnull model) {
        if ([model.actionName isEqualToString:@"device_net_conn_bind_nb"]) {
            ThingSmartActivatorTypeNBModel *type = [[ThingSmartActivatorTypeNBModel alloc] init];
            type.type = ThingSmartActivatorTypeNB;
            type.typeName = NSStringFromThingSmartActivatorType(ThingSmartActivatorTypeNB);
            type.timeout = 120;
            type.spaceId = [ThingSmartActivatorLinkTool getSpaceId];
            type.nbId = [codeModel.actionData objectForKey:@"id"];;
            [self startConfigQRCode:type];
        }
    } failure:^(NSError * _Nonnull error) {

    }];
}

- (void)startConfigQRCode:(ThingSmartActivatorTypeNBModel *)type {

  [self.discovery registerWithActivatorList:@[type]];
  [self.discovery setupDelegate:self];
  [self.discovery startActive:type deviceList:@[]];

}

- (void)activatorService:(id<ThingSmartActivatorActiveProtocol>)service 
           activatorType:(ThingSmartActivatorTypeModel *)type 
       didReceiveDevices:(NSArray<ThingSmartActivatorDeviceModel *> *)devices 
                   error:(ThingSmartActivatorErrorModel *)errorModel {    
    if (errorModel) {
        [self _connectWifiError:errorModel];
        return;
    }

    if (device) {
        [self _handleDevice:device];
    }
}

- (void)_handleDevice:(ThingSmartActivatorDeviceModel *)deviceModel {
    /// handle success device
}

- (ThingSmartActivatorDiscovery *)discovery {
    if (!_discovery) {
        _discovery = [[ThingSmartActivatorDiscovery alloc] init];
    }
    return _discovery;
}

- (ThingSmartActivatorDiscoveryRequest *)requestService {
    if (!_requestService) {
        _requestService = [[ThingSmartActivatorDiscoveryRequest alloc] init];
    }
    return _requestService;
}