HomeHubWifiPasswdInputViewController.m 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. //
  2. // HomeHubWifiPasswdInputViewController.m
  3. // OneCable
  4. //
  5. // Created by nComz on 2017. 5. 11..
  6. // Copyright © 2017년 ntels. All rights reserved.
  7. //
  8. #import "HomeHubWifiPasswdInputViewController.h"
  9. #import "HomeHubConnectWifiViewController.h"
  10. @interface HomeHubWifiPasswdInputViewController ()<UITextFieldDelegate> {
  11. NSTimer *_timer;
  12. NSInteger _elapsedSeconds;
  13. BLEServiceHandler *bleService;
  14. BOOL updateSSID;
  15. BOOL updateBSSID;
  16. BOOL updateIPSet;
  17. BOOL updateIPAddr;
  18. }
  19. @end
  20. @implementation HomeHubWifiPasswdInputViewController
  21. - (void)viewDidLoad {
  22. [super viewDidLoad];
  23. [self initUI];
  24. [self prepareViewDidLoad];
  25. }
  26. - (void)initUI {
  27. _lblSSID.text = [NSString stringWithFormat:@"SSID : %@", _selectedWLanModel.ssid];
  28. _imgvLoading.hidden = YES;
  29. }
  30. - (void)prepareViewDidLoad {
  31. //ble
  32. bleService = [BLEServiceHandler sharedManager];
  33. bleService.delegate = self;
  34. updateSSID = updateBSSID = updateIPSet = updateIPAddr = NO;
  35. }
  36. - (void)didReceiveMemoryWarning {
  37. [super didReceiveMemoryWarning];
  38. }
  39. -(void)startLoading:(BOOL)isStart {
  40. _imgvLoading.hidden = !isStart;
  41. if (isStart) {
  42. _elapsedSeconds = kMaxTimeOut;
  43. if (!_timer) {
  44. _timer = [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(updateInclusionStatus) userInfo:nil repeats:YES];
  45. }
  46. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  47. if ([_imgvLoading.layer animationForKey:@"SpinAnimation"] == nil) {
  48. CABasicAnimation* animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
  49. animation.fromValue = [NSNumber numberWithFloat:0.0f];
  50. animation.toValue = [NSNumber numberWithFloat: 2*M_PI];
  51. animation.duration = 2.0f;
  52. animation.repeatCount = INFINITY;
  53. [_imgvLoading.layer addAnimation:animation forKey:@"SpinAnimation"];
  54. }
  55. });
  56. }
  57. else {
  58. _elapsedSeconds = kMaxTimeOut;
  59. if (_timer) {
  60. [_timer invalidate];
  61. _timer = nil;
  62. }
  63. if ([_imgvLoading.layer animationForKey:@"SpinAnimation"] != nil) {
  64. [_imgvLoading.layer removeAnimationForKey:@"SpinAnimation"];
  65. }
  66. }
  67. }
  68. - (void)updateInclusionStatus {
  69. dispatch_async(dispatch_get_main_queue(), ^(void) {
  70. _elapsedSeconds--;
  71. if (_elapsedSeconds == 0) {
  72. [self startLoading:NO];
  73. }
  74. });
  75. }
  76. #pragma mark - User Event
  77. - (IBAction)btnAgainTouched:(id)sender {
  78. BOOL find = NO;
  79. for (UIViewController *vc in self.navigationController.viewControllers) {
  80. if ([vc isKindOfClass:HomeHubConnectWifiViewController.class]) {
  81. find = YES;
  82. [self.navigationController popToViewController:vc animated:YES];
  83. }
  84. }
  85. if (!find) {
  86. UIViewController *vc = [CommonUtil instantiateViewControllerWithIdentifier:@"HomeHubConnectWifiViewController" storyboardName:@"HomeHub"];
  87. [self.navigationController pushViewController:vc animated:YES];
  88. }
  89. }
  90. - (IBAction)btnConnectTouched:(id)sender {
  91. NSString *pass = _txtInputPasswd.text.trim;
  92. if (pass.length == 0) {
  93. return;
  94. }
  95. [self startLoading:YES];
  96. [bleService setWiFiPwd:pass];
  97. [bleService enableDHCP];
  98. [bleService readAndNotifyCharacteristicUUID:kBLEChrStDHCPArg isNotify:NO];
  99. updateSSID = updateBSSID = updateIPSet = updateIPAddr = NO;
  100. }
  101. - (IBAction)btnCancelTouched:(id)sender {
  102. [self.navigationController popToRootViewControllerAnimated:YES];
  103. }
  104. - (BOOL)textFieldShouldReturn:(UITextField *)textField {
  105. NSLog(@"wifi pass textFieldShouldReturn");
  106. if (textField.text.length > 0) {
  107. [self.view endEditing:YES];
  108. [self btnConnectTouched:nil];
  109. }
  110. return YES;
  111. }
  112. #pragma mark - ble delegate
  113. //enableDHCP
  114. - (void)BLEWiFiDHCPUpdate:(id)data {
  115. [bleService applyWiFiSettingInfo];
  116. [bleService readAndNotifyCharacteristicUUID:kBLEChrRdConInfo isNotify:YES];
  117. }
  118. //kBLEChrRdConInfo
  119. - (void)BLEWiFiConnectionUpdate:(CBCharacteristic *)info {
  120. NSLog(@"Connection Update!");
  121. NSString *value = [bleService hexStringValue:info] ;
  122. NSLog(@"value : %@", value) ;
  123. NSLog(@"value : %@", [value substringFromIndex:value.length-1]) ;
  124. if (value.length > 0 &&
  125. EQUALS([value substringFromIndex:value.length-1], @"1")) {
  126. [bleService readConnectionWiFiInfo];
  127. }
  128. }
  129. //readConnectionWiFiInfo
  130. - (void)BLEWiFiConnectionInfoUpdateWithKey:(NSString *)kBLEChr
  131. result:(NSString *)result {
  132. NSLog(@"Connection Info Update!") ;
  133. if (EQUALS(kBLEChr, kBLEChrRdSSID)) {
  134. updateSSID = YES;
  135. }
  136. else if(EQUALS(kBLEChr, kBLEChrRdBSSID)) {
  137. updateBSSID = YES;
  138. }
  139. else if(EQUALS(kBLEChr, kBLEChrRnIpSet)) {
  140. updateIPSet = YES;
  141. }
  142. else if(EQUALS(kBLEChr, kBLEChrRnIpAddr)) {
  143. updateIPAddr = YES;
  144. }
  145. // 연결 확인 완료
  146. if (updateSSID && updateBSSID && updateIPSet && updateIPAddr) {
  147. [self startLoading:NO];
  148. UIViewController *vc = [CommonUtil instantiateViewControllerWithIdentifier:@"HomeHubUpdateCompleteViewController" storyboardName:@"HomeHub"];
  149. [self.navigationController pushViewController:vc animated:YES];
  150. }
  151. }
  152. @end