|
获取
注意:网关在正常状态下不会广播数据(SDK扫描不到),只有在刚上电的60秒内会广播数据,与SDK蓝牙连接成功一次后 广播时间延长至180秒。如果在执行第3步或第4步 返回状态 TTGatewayNotConnect 或 TTGatewayDisconnect,需要重新对网关电源的插拔 才能再次蓝牙连接通信
1.开启网关扫描
/*只有刚上电的网关才能扫描到,刚上电的网关指示灯会 红绿灯交替闪烁*/
[TTGateway startScanGatewayWithBlock:^(TTGatewayScanModel *model) {
}];
2.连接网关
//连接成功后 网关广播延长至180秒
[TTGateway connectGatewayWithGatewayMac:gatewayMac block:^(TTGatewayConnectStatus connectStatus) {
//连接成功 180秒后网关与sdk 会自动断开连接
//只有在连接状态,才能执行第3步、第4步
if (connectStatus == TTGatewayConnectSuccess) {
[TTGateway stopScanGateway];
}
//...读取网关附近的网络
}];
3.读取网关附近的网络
//只有连接成功后 才能读取附近网络
[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.初始化网关
//初始化网关
NSMutableDictionary *dict = [NSMutableDictionary new];
dict[@"SSID"] = wifi;
dict[@"wifiPwd"] = wifiPassword;
dict[@"uid"] = userID;
dict[@"userPwd"] = userPassword;
dict[@"gatewayName"]= @"I am G2";
[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")];
}];
Note:The gateway broadcasts data only during the first 60 seconds of power
1.Scan
[TTGateway startScanGatewayWithBlock:^(TTGatewayScanModel *model) {
}];
2.Connect
[TTGateway connectGatewayWithGatewayMac:gatewayMac block:^(TTGatewayConnectStatus connectStatus) {
if (connectStatus == TTGatewayConnectSuccess) {
//The gateway automatically disconnects the application after 180 seconds
[TTGateway stopScanGateway];
[TTGateway scanWiFiByGatewayWithBlock:^(BOOL isFinished, NSArray *WiFiArr, TTGatewayStatus status) {
}
}
}];
3.Get the network near the gateway
[TTGateway scanWiFiByGatewayWithBlock:^(BOOL isFinished, NSArray *WiFiArr, TTGatewayStatus status) {
if (status == TTGatewayNotConnect || status == TTGatewayDisconnect ) {
//connect gateway again
return ;
}
if (WiFiArr.count > 0) {
NSMutableArray *arr = [NSMutableArray arrayWithArray:WiFiArr];
if (isFinished == YES) {
}
}
}];
4.Init gateway
//Parameters
NSMutableDictionary *dict = [NSMutableDictionary new];
dict[@"SSID"] = wifi;
dict[@"wifiPwd"] = wifiPassword;
dict[@"uid"] = userID;
dict[@"userPwd"] = userPassword;
dict[@"gatewayName"]= @"I am G2";
[TTGateway initializeGatewayWithInfoDic:dict block:^(TTSystemInfoModel *systemInfoModel, TTGatewayStatus status) {
if (status == TTGatewayNotConnect || status == TTGatewayDisconnect) {
//Please re-power the gateway
return ;
}
if (status == TTGatewaySuccess) {
[TTGateway disconnectGatewayWithGatewayMac:self.gatewayMac block:nil];
[self isUploadSuccess:systemInfoModel];
return;
}
if (status == TTGatewayWrongSSID) {
[self.view showToast:LS(@"WiFi name error")];
return;
}
if (status == TTGatewayWrongSSID) {
[self.view showToast:LS(@"WiFi password error")];
return;
}
[self.view showToast:LS(@"Failure")];
}];
|