Skip to content

网关接口示例 ​

注意:网关在正常状态下不会广播数据(SDK扫描不到),只有在刚上电的60秒内会广播数据,与SDK蓝牙连接成功一次后 广播时间延长至180秒。如果在执行第3步或第4步 返回状态 TTGatewayNotConnect 或 TTGatewayDisconnect,需要重新对网关电源的插拔 才能再次蓝牙连接通信

1.开启网关扫描

c
/*只有刚上电的网关才能扫描到,刚上电的网关指示灯会 红绿灯交替闪烁*/
[TTGateway startScanGatewayWithBlock:^(TTGatewayScanModel *model) {
 
        }];

2.连接网关

c
//连接成功后 网关广播延长至180秒
[TTGateway connectGatewayWithGatewayMac:gatewayMac block:^(TTGatewayConnectStatus connectStatus) {
        //连接成功 180秒后网关与sdk 会自动断开连接
        //只有在连接状态,才能执行第3步、第4步
       if (connectStatus == TTGatewayConnectSuccess) {
           [TTGateway stopScanGateway];
           }
          //...读取网关附近的网络
   }];

3.读取网关附近的网络

c
//只有连接成功后  才能读取附近网络
[TTGateway  scanWiFiByGatewayWithBlock:^(BOOL isFinished, NSArray *WiFiArr, TTGatewayStatus status) {
    if (status == TTGatewayNotConnect || status == TTGatewayDisconnect ) {
        //如果蓝牙连接超时或断开,请插拔网关电源,重新上电
        return ;
    }
 
    if (WiFiArr.count > 0) {
        NSMutableArray *arr = [NSMutableArray arrayWithArray:WiFiArr];
        if (isFinished == YES) {
                //网关附近的网络已经全部搜索到了     
            }           
    }
}];

4.初始化网关

c
x
//初始化网关
NSMutableDictionary *dict = [NSMutableDictionary new];
    dict[@"SSID"] = wifi;
    dict[@"wifiPwd"] = wifiPassword;
    dict[@"uid"] = userID;
    dict[@"userPwd"] = userPassword;
    dict[@"gatewayName"]= @"name";
    dict[@"gatewayVersion"]= @2; //@2 means G2,@3 means G3,@4 means G4
 
[TTGateway initializeGatewayWithInfoDic:dict block:^(TTSystemInfoModel *systemInfoModel, TTGatewayStatus status) {
 
        if (status == TTGatewayNotConnect || status == TTGatewayDisconnect) {
            //网关连接超时 请对网关重新上电
            return ;
        }
        if (status == TTGatewaySuccess) {
            //网关初始化成功 
            [TTGateway disconnectGatewayWithGatewayMac:self.gatewayMac block:nil];
             [self isUploadSuccess:systemInfoModel];
            return;
        }
        if (status == TTGatewayWrongSSID) {
            //wifi错误
            [self.view showToast:LS(@"WiFi name error")];
            return;
        }
        if (status == TTGatewayWrongSSID) {
            //wifiPasscode 错误
            [self.view showToast:LS(@"WiFi password error")];
            return;
        }
         [self.view showToast:LS(@"Failure")];
    }];
5.调用服务端接口确认网关已经成功连接到网关服务器 ​

Cloud API:https://cnapi.ttlock.com/v3/gateway/isInitSuccess

6.上传网关信息至服务器完成网关添加 ​

Cloud API: https://cnapi.ttlock.com/v3/gateway/uploadDetail