// // DeviceHeaderView.m // tongxin // // Created by ecell on 2022/11/9. // Copyright © 2022 xTT. All rights reserved. // #import "DeviceHeaderView.h" #import "Bluetooth.h" #import #import "DeviceLoseSetViewController.h" @interface DeviceHeaderView () /// <#arguments#> @property (nonatomic ,weak) UILabel *nameLabel; /// <#arguments#> @property (nonatomic ,weak) UILabel *comLabel; @property (nonatomic ,weak) UIButton *stateBtn; @end @implementation DeviceHeaderView - (instancetype)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { self.backgroundColor = [UIColor clearColor]; [self subHeaderView]; // [[[NSNotificationCenter defaultCenter] rac_addObserverForName:BluetoothNotificationAtConnectSuccess object:nil] subscribeNext:^(NSNotification *notification) { // BLEModel *model = notification.object; // self.nameLabel.text = model.peripheral.name; // self.comLabel.text = model.MACString; // [self.stateBtn setTitle:@"连接成功" forState:0]; // [UICommon HidenLoading]; // }]; [[[NSNotificationCenter defaultCenter] rac_addObserverForName:BluetoothNotificationAtConnectFail object:nil] subscribeNext:^(NSNotification *notification) { [self.stateBtn setTitle:@"连接失败" forState:0]; }]; [[[NSNotificationCenter defaultCenter] rac_addObserverForName:BluetoothNotificationAtDisconnect object:nil] subscribeNext:^(NSNotification *notification) { [self.stateBtn setTitle:@"连接断开" forState:0]; }]; [[[NSNotificationCenter defaultCenter] rac_addObserverForName:@"LIANJIEZ" object:nil] subscribeNext:^(NSNotification *notification) { NSString *tiele = notification.object; [self.stateBtn setTitle:tiele forState:0]; }]; } return self; } - (void)setBleModel:(BLEModel *)bleModel { _bleModel = bleModel; self.nameLabel.text = bleModel.DeName; self.comLabel.text = bleModel.MACString; [self.stateBtn setTitle:Bluetooth.shareInstance.isConnected ? @"连接成功" : @"连接失败" forState:0]; } - (void)subHeaderView { UIImageView *leftImg = [UIImageView new]; leftImg.backgroundColor = mainColor; [self addSubview:leftImg]; [leftImg mas_makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(self); make.top.equalTo(self).offset(15); make.size.mas_equalTo(CGSizeMake(3, 20)); }]; UILabel *titleLabel = [UILabel new]; titleLabel.textColor = KKGrey143; titleLabel.font = [UIFont fontWithName:@"HelveticaNeue" size:14]; titleLabel.text = @"当前防丢设备"; [self addSubview:titleLabel]; [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(leftImg.mas_right).offset(5); make.centerY.equalTo(leftImg); }]; UIView *bgView = [UIView new]; bgView.backgroundColor = [UIColor whiteColor]; [self addSubview:bgView]; [bgView mas_makeConstraints:^(MASConstraintMaker *make) { make.left.right.equalTo(self); make.top.equalTo(leftImg.mas_bottom).offset(15); make.height.mas_equalTo(Adapted(85)); }]; UIImageView *deviceImg = [UIImageView new]; deviceImg.backgroundColor = [UIColor whiteColor]; deviceImg.image = [UIImage imageNamed:@"icon_imei_login_page"]; deviceImg.layer.borderWidth = 0.5; deviceImg.layer.borderColor = RGB(238, 238, 238).CGColor; deviceImg.layer.cornerRadius = 3; [bgView addSubview:deviceImg]; [deviceImg mas_makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(bgView).offset(20); make.centerY.equalTo(bgView); make.size.mas_equalTo(CGSizeMake(Adapted(40), Adapted(40))); }]; UILabel *nameLabel = [UILabel new]; nameLabel.font = [UIFont fontWithName:@"HelveticaNeue" size:Adapted(20)]; self.nameLabel = nameLabel; [bgView addSubview:nameLabel]; [nameLabel mas_makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(deviceImg.mas_right).offset(10); make.top.equalTo(bgView).offset(10); }]; UILabel *comLabel = [UILabel new]; comLabel.font = [UIFont fontWithName:@"HelveticaNeue" size:Adapted(13)]; self.comLabel = comLabel; [bgView addSubview:comLabel]; [comLabel mas_makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(nameLabel); make.centerY.equalTo(deviceImg.mas_bottom); }]; UIButton *setBtn = [UIButton new]; [setBtn setTitle:@"设置" forState:0]; setBtn.titleLabel.font = [UIFont fontWithName:@"HelveticaNeue" size:Adapted(12)]; [setBtn setTitleColor:[UIColor whiteColor] forState:0]; setBtn.backgroundColor = mainColor; setBtn.layer.cornerRadius = Adapted(14); setBtn.layer.masksToBounds = YES; setBtn.tag = 3; [setBtn addTarget:self action:@selector(btnTouch:) forControlEvents:UIControlEventTouchUpInside]; [bgView addSubview:setBtn]; [setBtn mas_makeConstraints:^(MASConstraintMaker *make) { make.right.equalTo(bgView.mas_right).offset(-20); make.bottom.equalTo(bgView.mas_centerY).offset(-Adapted(5)); make.size.mas_equalTo(CGSizeMake(Adapted(85), Adapted(28))); }]; UIButton *stateBtn = [UIButton new]; [stateBtn setTitle:@"" forState:0]; stateBtn.titleLabel.font = [UIFont fontWithName:@"HelveticaNeue" size:Adapted(12)]; [stateBtn setTitleColor:[UIColor whiteColor] forState:0]; stateBtn.backgroundColor = mainColor; stateBtn.layer.cornerRadius = Adapted(14); stateBtn.layer.masksToBounds = YES; stateBtn.tag = 4; [stateBtn addTarget:self action:@selector(btnTouch:) forControlEvents:UIControlEventTouchUpInside]; self.stateBtn = stateBtn; [bgView addSubview:stateBtn]; [stateBtn mas_makeConstraints:^(MASConstraintMaker *make) { make.right.equalTo(setBtn.mas_right); make.top.equalTo(bgView.mas_centerY).offset(Adapted(5)); make.size.mas_equalTo(CGSizeMake(Adapted(85), Adapted(28))); }]; NSArray *titleArr = @[@"移除设备",@"查找设备",@"取消查找"]; for (int i = 0; i < titleArr.count; i++) { UIButton *btn = [UIButton new]; [btn setTitle:titleArr[i] forState:0]; btn.titleLabel.font = [UIFont fontWithName:@"HelveticaNeue" size:Adapted(12)]; [btn setTitleColor:[UIColor whiteColor] forState:0]; btn.backgroundColor = mainColor; btn.layer.cornerRadius = Adapted(14); btn.layer.masksToBounds = YES; btn.tag = i; [btn addTarget:self action:@selector(btnTouch:) forControlEvents:UIControlEventTouchUpInside]; [self addSubview:btn]; [btn mas_makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(self).offset(20+Adapted(85)*i+10*i); make.top.equalTo(bgView.mas_bottom).offset(Adapted(5)); make.size.mas_equalTo(CGSizeMake(Adapted(85), Adapted(28))); }]; } } - (void)btnTouch:(UIButton *)btn { switch (btn.tag) { case 0: {/// 移除设备 [Bluetooth.shareInstance disConnectedCurrenDevice]; } break; case 1: {/// 查找设备 if (!Bluetooth.shareInstance.isConnected) { [UICommon MessageErrorText:@"设备未连接!"]; return; } [Bluetooth.shareInstance writeCheckBleWithBle:@"0x00"]; } break; case 2: {/// 取消查找 if (!Bluetooth.shareInstance.isConnected) { [UICommon MessageErrorText:@"设备未连接!"]; return; } [Bluetooth.shareInstance writeCheckBleWithBle:@"0x01"]; } break; case 3: {/// 设置 DeviceLoseSetViewController *vc = [DeviceLoseSetViewController new]; [self.getViewController.navigationController pushViewController:vc animated:YES]; } break; case 4: {/// 连接状态 if ([btn.currentTitle isEqualToString:@"连接失败"] || [btn.currentTitle isEqualToString:@"连接断开"]) { //[UICommon MessageUpload:@""WithStatus:@"连接中..."]; [self.stateBtn setTitle:@"连接中..." forState:0]; [Bluetooth.shareInstance connectPeripheralWithUUID]; } if ([btn.currentTitle isEqualToString:@"连接中.."] || [btn.currentTitle isEqualToString:@"连接中..."] || [btn.currentTitle isEqualToString:@"连接成功"]) { [Bluetooth.shareInstance disconnectBLE]; [self.stateBtn setTitle:@"连接失败" forState:0]; } } break; default: break; } } /* // Only override drawRect: if you perform custom drawing. // An empty implementation adversely affects performance during animation. - (void)drawRect:(CGRect)rect { // Drawing code } */ @end