From 1d39caf7235522ae121db7a188ba41a706217407 Mon Sep 17 00:00:00 2001 From: 单军华 <WindShan@danjunhuas-MacBook-Pro.local> Date: Sat, 28 Apr 2018 09:39:49 +0800 Subject: [PATCH] 清理多余代码,优化 --- pregnancy_guard/BaseProject/Controller/Home/SearchDevicePage.m | 553 +++++++++++++++++++++++++++++++++++++++++------------- 1 files changed, 417 insertions(+), 136 deletions(-) diff --git a/pregnancy_guard/BaseProject/Controller/Home/SearchDevicePage.m b/pregnancy_guard/BaseProject/Controller/Home/SearchDevicePage.m index b939612..381510b 100644 --- a/pregnancy_guard/BaseProject/Controller/Home/SearchDevicePage.m +++ b/pregnancy_guard/BaseProject/Controller/Home/SearchDevicePage.m @@ -9,18 +9,69 @@ #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" +#import <CoreBluetooth/CoreBluetooth.h> -@interface SearchDevicePage ()<UITableViewDelegate,UITableViewDataSource> - +@interface SearchDevicePage ()<UITableViewDelegate,UITableViewDataSource,CBCentralManagerDelegate> +{ + 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; - +@property (nonatomic, strong) CBCentralManager * centralManager; @end @implementation SearchDevicePage + +//-(CBCentralManager *)centralManager +//{ +// if (_centralManager == nil) { +// _centralManager = [[CBCentralManager alloc] initWithDelegate:self queue:nil options:nil]; +// } +// return _centralManager; +//} + +-(void)centralManagerDidUpdateState:(CBCentralManager *)central +{ + switch (central.state) { + case CBCentralManagerStatePoweredOff: + { + //[mainView makeToast:@"���������������������������������������������"]; + [Global alertMessageEx:@"���������������������������������������������������" title:@"������������" okTtitle:nil cancelTitle:@"OK" delegate:self]; + } + break; + case CBCentralManagerStatePoweredOn: + NSLog(@"������������������"); + break; + case CBCentralManagerStateResetting: + break; + case CBCentralManagerStateUnauthorized: + break; + case CBCentralManagerStateUnknown: + break; + case CBCentralManagerStateUnsupported: + NSLog(@"���������������������������"); + [Global alertMessageEx:@"���������������������������������������" title:@"������������" okTtitle:nil cancelTitle:@"OK" delegate:self]; + break; + default: + break; + } +} + - (UITableView *)tableView { if (!_tableView) @@ -35,7 +86,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 +94,86 @@ 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; + CGFloat btnSearchH = 55; + if ( IsiPhone4 || IsiPhone5 ) + { + ic_top_bkH = 260; + ic_search_doneH = 180; + btnSearchH = 40; + } + + 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+btnSearchH, 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 +183,10 @@ self.tableView.hidden = NO; // Do any additional setup after loading the view. + + self.centralManager = [[CBCentralManager alloc] initWithDelegate:self queue:nil options:nil]; + + [[CBMoralManager sharedManager] setCbDiscoveryDelegate:self]; } @@ -86,9 +202,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 +222,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 @@ -227,34 +408,133 @@ -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ - - if (_currentStatus == 2) - { - NSInteger index = [indexPath row]; - DeviceSpareModel* model = [self.modelArray objectAtIndex:index]; - // ������������������������ - //HotelSelPage* page = [[HotelSelPage alloc] initIsFirstPage:NO]; - //page.bSelRoom = 2; - // ������������ - //BaseNaviController *baseNav = [[BaseNaviController alloc] initWithRootViewController:page]; - //[self presentViewController:baseNav animated:YES completion:nil]; - //[self showActionForPhoto]; - } - + if (_currentStatus == 2) + { + NSInteger index = [indexPath row]; + 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; + // ������������ + //BaseNaviController *baseNav = [[BaseNaviController alloc] initWithRootViewController:page]; + //[self presentViewController:baseNav animated:YES completion:nil]; + //[self showActionForPhoto]; + } + + } + +/** + *This method invoke after a new peripheral found. + */ +-(void)discoveryDidRefresh +{ + _currentStatus = 2; + [self.tableView reloadData]; +} + + +#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)storyAticon -{ - -} + -(void)backAticon { @@ -269,13 +549,14 @@ } /* -#pragma mark - Navigation - -// In a storyboard-based application, you will often want to do a little preparation before navigation -- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { - // Get the new view controller using [segue destinationViewController]. - // Pass the selected object to the new view controller. -} -*/ + #pragma mark - Navigation + + // In a storyboard-based application, you will often want to do a little preparation before navigation + - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { + // Get the new view controller using [segue destinationViewController]. + // Pass the selected object to the new view controller. + } + */ @end + -- Gitblit v1.8.0