From f99cf1d5cc50407394501853be06cb39f38a092c Mon Sep 17 00:00:00 2001
From: 单军华 <WindShan@danjunhuas-MacBook-Pro.local>
Date: Wed, 28 Mar 2018 14:09:19 +0800
Subject: [PATCH] 界面更换与适配调整

---
 pregnancy_guard/BaseProject/Controller/Home/SearchDevicePage.m |  463 +++++++++++++++++++++++++++++++++++++++++++--------------
 1 files changed, 351 insertions(+), 112 deletions(-)

diff --git a/pregnancy_guard/BaseProject/Controller/Home/SearchDevicePage.m b/pregnancy_guard/BaseProject/Controller/Home/SearchDevicePage.m
index b939612..c99daa2 100644
--- a/pregnancy_guard/BaseProject/Controller/Home/SearchDevicePage.m
+++ b/pregnancy_guard/BaseProject/Controller/Home/SearchDevicePage.m
@@ -9,12 +9,26 @@
 #import "SearchDevicePage.h"
 #import "GloriaLabel.h"
 #import "SearchDeviceCell.h"
+#import "DevicesTableViewCell.h"
+#import "BaseNaviController.h"
+#import "ProgressHandler.h"
+#import "UIView+Toast.h"
+#import "UIImage+animatedGIF.h"
+#import "SearchTipsCell.h"
 
 @interface SearchDevicePage ()<UITableViewDelegate,UITableViewDataSource>
-
+{
+    BOOL isBluetoothON;
+    UIButton* ic_search_btn;
+    UIImageView * ic_search_done;
+    UIImageView * ic_searching;
+    CGFloat ic_top_bkH;
+    CGFloat ic_search_doneH;
+    GloriaLabel* _TitleLabel;
+    GloriaLabel* _SetTitleLabel;
+}
 @property (nonatomic, strong) UITableView                   * tableView;
 @property (nonatomic, assign) int                           currentStatus; // 0 ��������� 1 ��������� 2 ������������
-@property (nonatomic, strong) NSMutableArray                * modelArray;
 
 @end
 
@@ -35,7 +49,7 @@
         [self.view addSubview:_tableView];
         
         [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
-            make.edges.mas_equalTo(UIEdgeInsetsMake(325 , 0, 0, 0));
+            make.edges.mas_equalTo(UIEdgeInsetsMake(ic_top_bkH , 0, 0, 0));
         }];
         
         _tableView.tableFooterView = [UIView new];
@@ -43,25 +57,84 @@
     return _tableView;
 }
 
+-(void)viewDidAppear:(BOOL)animated
+{
+    [super viewDidAppear:animated];
+    [[CBMoralManager sharedManager] disconnectPeripheral:[[CBMoralManager sharedManager] myPeripheral]];
+
+    
+}
+
+// ������������������
+//if (isBluetoothON) {
+//    return [[[CBMoralManager sharedManager] foundPeripherals] count];
+//}
+
+-(void)viewDidDisappear:(BOOL)animated
+{
+    [[CBMoralManager sharedManager]stopScanning];
+}
+
+-(void)startScanning
+{
+    // Start scanning for devices
+    [[CBMoralManager sharedManager] startScanning];
+    [self performSelector:@selector(stopScanning) withObject:nil afterDelay:5.0f];
+}
+
+-(void)pullToRefresh
+{
+    [[CBMoralManager sharedManager] refreshPeripherals];
+    [self performSelector:@selector(stopScanning) withObject:nil afterDelay:5.0f];
+}
+
+-(void)stopScanning
+{
+    if([[[CBMoralManager sharedManager] foundPeripherals] count] == 0)
+    {
+        self.currentStatus = 0;
+        
+        [self.tableView reloadData];
+    }
+    
+    ic_search_done.hidden = NO;
+    ic_searching.hidden = YES;
+    [ic_search_btn setBackgroundImage:[UIImage imageNamed:@"ic_search_start_btn" ] forState:UIControlStateNormal];
+    [[CBMoralManager sharedManager]stopScanning];
+    //[self.tableView.header endRefreshing];
+}
+
 - (void)viewDidLoad
 {
     [super viewDidLoad];
-    self.title = @"������������";
+    self.title = @"������������";
     [self setNavigationLeft:@"ic_back_arraw.png" sel:@selector(backAticon)];
-    [self setNavigationRight:@"icon_story.png" sel:@selector(storyAticon)];
     
     self.currentStatus = 0;
     
-    UIImageView * ic_top_bk = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 325)];
+    ic_top_bkH = 325;
+    ic_search_doneH = 230;
+    if ( IsiPhone4 || IsiPhone5 )
+    {
+        ic_top_bkH = 260;
+        ic_search_doneH = 180;
+    }
+    
+    UIImageView * ic_top_bk = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, ic_top_bkH)];
     ic_top_bk.image = [UIImage imageNamed:@"ic_top_bk"];
     [self.view addSubview:ic_top_bk];
     
-    UIImageView * ic_search_done = [[UIImageView alloc] initWithFrame:CGRectMake((SCREEN_WIDTH-230)/2, 20, 230, 230)];
+    ic_search_done = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, ic_top_bkH)];
     ic_search_done.image = [UIImage imageNamed:@"ic_search_done"];
     [self.view addSubview:ic_search_done];
     
-    UIButton* ic_search_btn = [UIButton buttonWithType:UIButtonTypeCustom];
-    ic_search_btn.frame = CGRectMake((SCREEN_WIDTH-128)/2, 20+230+20, 128, 32);
+    ic_searching = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, ic_top_bkH)];
+    [ic_searching setImage:[UIImage animatedImageWithAnimatedGIFURL:[[NSBundle mainBundle] URLForResource:@"gif_searching" withExtension:@"gif"]]];
+    [self.view addSubview:ic_searching];
+    ic_searching.hidden = YES;
+    
+    ic_search_btn = [UIButton buttonWithType:UIButtonTypeCustom];
+    ic_search_btn.frame = CGRectMake((SCREEN_WIDTH-128)/2, ic_search_doneH+55, 128, 32);
     [ic_search_btn setBackgroundImage:[UIImage imageNamed:@"ic_search_start_btn" ] forState:UIControlStateNormal];
     ic_search_btn.titleLabel.font = [UIFont systemFontOfSize: 18.0];
     ic_search_btn.layer.masksToBounds=YES;
@@ -71,6 +144,8 @@
     
     self.tableView.hidden = NO;
     // Do any additional setup after loading the view.
+    
+    [[CBMoralManager sharedManager] setCbDiscoveryDelegate:self];
 }
 
 
@@ -86,9 +161,14 @@
 
 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
 {
-    if (_currentStatus == 2)
+    if (_currentStatus == 2 && isBluetoothON )
     {
-        return self.modelArray.count;
+        if(section == 0)
+        {
+            return 1;
+        }
+        else
+            return [[[CBMoralManager sharedManager] foundPeripherals] count];
     }
     else
         return 5;
@@ -101,123 +181,183 @@
 {
     if (_currentStatus != 2)
     {
-        static NSString *CellIdentifier = @"searchDeviceCell";
-
-        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
-        if (cell == nil)
-        {
-            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
-        }
-        
         NSUInteger index = [indexPath row];
-        switch (index)
+        if(index == 0 )
         {
-            case 0:
+            NSString *CellIdentifier = @"searchTipsCell";
+            
+            UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
+            if (cell == nil)
             {
-                GloriaLabel* _TitleLabel = [[GloriaLabel alloc] initWithFrame:CGRectMake(10, 6,SCREEN_WIDTH/2, 30)];
-                _TitleLabel.font = [UIFont systemFontOfSize:14];
-                _TitleLabel.textAlignment = UITextAlignmentLeft;
-                _TitleLabel.textColor = kUIColorFromRGB(0x595959);
-                _TitleLabel.text = @"������������������";
-                [cell.contentView addSubview:_TitleLabel];
-                
+                cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
             }
-                break;
-            case 1:
+            
+            if(_SetTitleLabel==nil)
             {
-                UIImageView * icon_one = [[UIImageView alloc] initWithFrame:CGRectMake(10, 11, 22, 22)];
-                icon_one.image = [UIImage imageNamed:@"icon_one"];
-                [cell.contentView addSubview:icon_one];
-                
-                GloriaLabel* _TitleLabel = [[GloriaLabel alloc] initWithFrame:CGRectMake(10+22+10, 15,SCREEN_WIDTH-(10+22+10), 22)];
-                _TitleLabel.font = [UIFont systemFontOfSize:14];
-                _TitleLabel.textAlignment = UITextAlignmentLeft;
-                _TitleLabel.textColor = kUIColorFromRGB(0x595959);
-                _TitleLabel.text = @"������������������������������";
-                [cell.contentView addSubview:_TitleLabel];
-                
+                _SetTitleLabel = [[GloriaLabel alloc] initWithFrame:CGRectMake(10, 6,SCREEN_WIDTH/2, 30)];
+                _SetTitleLabel.font = [UIFont systemFontOfSize:14];
+                _SetTitleLabel.textAlignment = UITextAlignmentLeft;
+                _SetTitleLabel.textColor = kUIColorFromRGB(0x595959);
+                [cell.contentView addSubview:_SetTitleLabel];
             }
-                
-                break;
-            case 2:
-            {
-                UIImageView * icon_two = [[UIImageView alloc] initWithFrame:CGRectMake(10, 11, 22, 22)];
-                icon_two.image = [UIImage imageNamed:@"icon-two"];
-                [cell.contentView addSubview:icon_two];
-                
-                GloriaLabel* _TitleLabel = [[GloriaLabel alloc] initWithFrame:CGRectMake(10+22+10, 15,SCREEN_WIDTH-(10+22+10), 22)];
-                _TitleLabel.font = [UIFont systemFontOfSize:14];
-                _TitleLabel.textAlignment = UITextAlignmentLeft;
-                _TitleLabel.textColor = kUIColorFromRGB(0x595959);
-                _TitleLabel.text = @"������������������";
-                [cell.contentView addSubview:_TitleLabel];
-            }
-                
-                break;
-            case 3:
-            {
-                UIImageView * icon_three = [[UIImageView alloc] initWithFrame:CGRectMake(10, 11, 22, 22)];
-                icon_three.image = [UIImage imageNamed:@"icon-three"];
-                [cell.contentView addSubview:icon_three];
-                
-                GloriaLabel* _TitleLabel = [[GloriaLabel alloc] initWithFrame:CGRectMake(10+22+10, 15,SCREEN_WIDTH-(10+22+10), 22)];
-                _TitleLabel.font = [UIFont systemFontOfSize:14];
-                _TitleLabel.textAlignment = UITextAlignmentLeft;
-                _TitleLabel.textColor = kUIColorFromRGB(0x595959);
-                _TitleLabel.text = @"������APP������[������������������]";
-                [cell.contentView addSubview:_TitleLabel];
-            }
-                
-                break;
-            case 4:
-            {
-                UIImageView * icon_four = [[UIImageView alloc] initWithFrame:CGRectMake(10, 11, 22, 22)];
-                icon_four.image = [UIImage imageNamed:@"icon-four"];
-                [cell.contentView addSubview:icon_four];
-                
-                GloriaLabel* _TitleLabel = [[GloriaLabel alloc] initWithFrame:CGRectMake(10+22+10, 15,SCREEN_WIDTH-(10+22+10), 22)];
-                _TitleLabel.font = [UIFont systemFontOfSize:14];
-                _TitleLabel.textAlignment = UITextAlignmentLeft;
-                _TitleLabel.textColor = kUIColorFromRGB(0x595959);
-                _TitleLabel.text = @"������������������������������������������������������";
-                [cell.contentView addSubview:_TitleLabel];
-            }
-                
-                break;
+            
+            _SetTitleLabel.text = @"������������������";
+            
+            return cell;
+
         }
-        
-        return cell;
+        else
+        {
+            NSString *CellIdentifier = @"searchTips2Cell";
+            
+            SearchTipsCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
+            if (cell == nil)
+            {
+                cell = [[SearchTipsCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
+            }
+            
+            switch (index)
+            {
+                case 1:
+                {
+                    [cell setItemView:@"������������������������������" imgName:@"icon_one"];
+                    
+                }
+                    break;
+                case 2:
+                {
+                    [cell setItemView:@"������������������" imgName:@"icon-two"];
+                }
+                    
+                    break;
+                case 3:
+                {
+                    [cell setItemView:@"������APP������[������������������]" imgName:@"icon-three"];
+                }
+                    
+                    break;
+                case 4:
+                {
+                    [cell setItemView:@"������������������������������������������������������" imgName:@"icon-four"];
+                }
+                    
+                    break;
+            }
+            
+            return cell;
+        }
     }
     else
     {
-        static NSString *CellIdentifier = @"searchDeviceCell";
-        
-        SearchDeviceCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
-        if (cell == nil)
-        {
-            cell = [[SearchDeviceCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
-        }
-        
         NSUInteger section = [indexPath section];
         if(section==0)
         {
-            GloriaLabel* _TitleLabel = [[GloriaLabel alloc] initWithFrame:CGRectMake(20, 15,SCREEN_WIDTH/2, 20)];
-            _TitleLabel.font = [UIFont systemFontOfSize:14];
-            _TitleLabel.textAlignment = UITextAlignmentLeft;
-            _TitleLabel.textColor = kUIColorFromRGB(0x595959);
-            _TitleLabel.text = @"������������";
-            [cell.contentView addSubview:_TitleLabel];
+            NSString *CellIdentifier = @"searchDeviceCell1";
+            
+            UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
+            if (cell == nil)
+            {
+                cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
+            }
+            
+            if( _TitleLabel == nil)
+            {
+                _TitleLabel = [[GloriaLabel alloc] initWithFrame:CGRectMake(20, 15,SCREEN_WIDTH/2, 20)];
+                _TitleLabel.font = [UIFont systemFontOfSize:14];
+                _TitleLabel.textAlignment = UITextAlignmentLeft;
+                _TitleLabel.textColor = kUIColorFromRGB(0x595959);
+                _TitleLabel.text = @"������������";
+                [cell.contentView addSubview:_TitleLabel];
+            }
+            
+            return cell;
         }
         else if(section==1)
         {
-            DeviceSpareModel* model = [self.modelArray objectAtIndex:indexPath.row];
+            NSString *CellIdentifier = @"searchDeviceCell";
             
-            [cell setItemView:model];
+            SearchDeviceCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
+            if (cell == nil)
+            {
+                cell = [[SearchDeviceCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
+            }
+            
+            CBPeripheralExt *newPeriPheral = [[[CBMoralManager sharedManager] foundPeripherals] objectAtIndex:indexPath.row];
+            
+            NSString * bleName = [self nameForPeripheral:newPeriPheral];
+            NSString * bleUUID = [self UUIDStringfromPeripheral:newPeriPheral];
+            NSString * bleService = [self ServiceCountfromPeripheral:newPeriPheral];
+            
+            [cell setItemView:bleName];
+            
+            //[cell setDiscoveredPeripheralDataFromPeripheral:newPeriPheral];
+            
+           return cell;
         }
-        
-        return cell;
     }
 
+    return  nil;
+}
+
+/*!
+ *  @method nameForPeripheral:
+ *
+ *  @discussion Method to get the peripheral name
+ *
+ */
+-(NSString *)nameForPeripheral:(CBPeripheralExt *)ble
+{
+    NSString *bleName ;
+    
+    if ([ble.mAdvertisementData valueForKey:CBAdvertisementDataLocalNameKey] != nil)
+    {
+        bleName = [ble.mAdvertisementData valueForKey:CBAdvertisementDataLocalNameKey];
+    }
+    
+    if(bleName.length < 1 )
+        bleName = ble.mPeripheral.name;
+    
+    if(bleName.length < 1 )
+        bleName = UNKNOWN_PERIPHERAL;
+    
+    return bleName;
+}
+
+
+/*!
+ *  @method UUIDStringfromPeripheral:
+ *
+ *  @discussion Method to get the UUID from the peripheral
+ *
+ */
+-(NSString *)UUIDStringfromPeripheral:(CBPeripheralExt *)ble
+{
+    
+    NSString *bleUUID = ble.mPeripheral.identifier.UUIDString;
+    if(bleUUID.length < 1 )
+        bleUUID = @"Nil";
+    else
+        bleUUID = [NSString stringWithFormat:@"%@",bleUUID];
+    
+    return bleUUID;
+}
+
+/*!
+ *  @method ServiceCountfromPeripheral:
+ *
+ *  @discussion Method to get the number of services present in a device
+ *
+ */
+-(NSString *)ServiceCountfromPeripheral:(CBPeripheralExt *)ble
+{
+    NSString *bleService =@"";
+    NSInteger serViceCount = [[ble.mAdvertisementData valueForKey:CBAdvertisementDataServiceUUIDsKey] count];
+    if(serViceCount < 1 )
+        bleService = @"No Services";
+    else
+        bleService = [NSString stringWithFormat:@" %ld Service Advertised ",(long)serViceCount];
+    
+    return bleService;
 }
 
 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
@@ -232,7 +372,13 @@
      if (_currentStatus == 2)
      {
           NSInteger index = [indexPath row];
-          DeviceSpareModel* model = [self.modelArray objectAtIndex:index];
+         if (isBluetoothON)
+         {
+             [tableView deselectRowAtIndexPath:indexPath animated:YES];
+             [self connectPeripheral:indexPath.row];
+         }
+         
+         // DeviceSpareModel* model = [self.modelArray objectAtIndex:index];
          // ������������������������
          //HotelSelPage* page = [[HotelSelPage alloc] initIsFirstPage:NO];
          //page.bSelRoom = 2;
@@ -246,16 +392,109 @@
 }
 
 
--(void)searchAction
+/**
+ *This method invoke after a new peripheral found.
+ */
+-(void)discoveryDidRefresh
 {
-    
+    _currentStatus = 2;
+    [self.tableView reloadData];
 }
 
--(void)storyAticon
+
+#pragma mark - BlueTooth Turned Off Delegate
+
+/*!
+ *  @method bluetoothStateUpdatedToState:
+ *  ������������������
+ *  @discussion Method to be called when state of Bluetooth changes
+ *
+ */
+
+-(void)bluetoothStateUpdatedToState:(BOOL)state
 {
     
+    isBluetoothON = state;
+    [self.tableView reloadData];
+    isBluetoothON ? [self.tableView setScrollEnabled:YES] : [self.tableView setScrollEnabled:NO];
 }
 
+
+
+#pragma mark - Connect
+
+/*!
+ *  @method connectPeripheral:
+ *
+ *  @discussion Method to connect the selected peripheral
+ *
+ */
+
+-(void)connectPeripheral:(NSInteger)index
+{
+    
+    if ([[CBMoralManager sharedManager] foundPeripherals].count != 0)
+    {
+        CBPeripheralExt *selectedBLE = [[[CBMoralManager sharedManager] foundPeripherals] objectAtIndex:index];
+        [[ProgressHandler sharedInstance] showWithDetailsLabel:@"Connecting.." Detail:selectedBLE.mPeripheral.name];
+        
+        [[CBMoralManager sharedManager] connectPeripheral:selectedBLE.mPeripheral CompletionBlock:^(BOOL success, NSError *error) {
+            [[ProgressHandler sharedInstance] hideProgressView];
+            if(success)
+            {
+                _bConnectSuccess = YES;
+                NSString * bleName = [self nameForPeripheral:selectedBLE];
+                NSString * bleUUID = [self UUIDStringfromPeripheral:selectedBLE];
+                
+                [UserDefault setObject:bleUUID forKey:@"bleUUID"];
+                [UserDefault setObject:bleName forKey:@"bleName"];
+                
+                [UserDefault synchronize];//������synchronize���������������������������������,���������������NSUserDefaults������������������������������������ 
+                //CBPeripheralExt *selBLE = [UserDefault objectForKey:@"Peripheral"];//������������������Peripheral
+                [self backAticon];
+            }
+            else
+            {
+                if(error)
+                {
+                    NSString *errorString = [error.userInfo valueForKey:NSLocalizedDescriptionKey];
+                    
+                    if(errorString.length)
+                    {
+                        [self.view makeToast:errorString];
+                    }
+                    else
+                    {
+                        [self.view makeToast:UNKNOWN_ERROR];
+                    }
+                }
+            }
+            
+        }];
+        
+    }
+    else
+    {
+        NSLog(@"Array is nil");
+        [[CBMoralManager sharedManager] refreshPeripherals];
+    }
+}
+
+-(void)searchAction
+{
+    self.currentStatus = 1;
+    
+    ic_search_done.hidden = YES;
+    ic_searching.hidden = NO;
+    
+    //[mainImageView setImage:[UIImage animatedImageWithAnimatedGIFURL:[[NSBundle mainBundle] URLForResource:imageName withExtension:@"gif"]]];
+    [ic_search_btn setBackgroundImage:[UIImage imageNamed:@"ic_search_stop_btn"] forState:UIControlStateNormal];
+    
+    [self startScanning];
+}
+
+
+
 -(void)backAticon
 {
     //[self.navigationController popToViewController:self animated:YES];

--
Gitblit v1.8.0