1.接入SDK
-
1. 将connect.framework添加到项目中
-
方法一:直接拖拽到Xcode中,需要勾选Copy items if needed。
-
方法二:先将connect.framework拷贝到项目文件夹中,在Xcode中添加该connect.framework,选择Add Files to "****",注意这里不用勾选Copy items if needed
-
-
2.在使用到SDK功能的时候引用SDK
- 引用 #import <connect/connect.h>
2.使用SDK
-
1. 配网模块
-
添加蓝牙权限
- 首先添加app蓝牙使用权限,在TARGETS->Info->Custom iOS Target Properties中添加下面两个权限
Privacy - Bluetooth Always Usage Description
Privacy - Bluetooth Peripheral Usage Description - 引用 #import <CoreBluetooth/CoreBluetooth.h>
- 检测蓝牙是否打开
创建实例对象,并设置代理
在代理方法中监听,蓝牙是否打开
CBCentralManager *centralManager = [[CBCentralManager alloc] initWithDelegate:self queue:nil options:@{CBCentralManagerOptionShowPowerAlertKey:@NO}];
- (void)centralManagerDidUpdateState:(CBCentralManager *)central { if ([central state] == CBCentralManagerStatePoweredOn) { NSLog(@"蓝牙已打开"); } else if ([central state] == CBManagerStateUnauthorized) { NSLog(@"蓝牙关闭"); } else { NSLog(@"蓝牙关闭"); } }
- 首先添加app蓝牙使用权限,在TARGETS->Info->Custom iOS Target Properties中添加下面两个权限
-
蓝牙搜索、连接
- 开始搜索
- (void)beginSearch{ kWeakSelf; [[HPBluetoothManager instance] startBluetoothSearch:^(NSArray * _Nonnull peripheralInfoArr) { weakSelf.peripheralsArray = [NSMutableArray arrayWithArray:peripheralInfoArr]; [weakSelf.tableView reloadData]; }]; }
- 从搜索出来的设备中选择想要配网的设备,进行配网
- 开始蓝牙连接
- (void)clickConnect{ kWeakSelf; [[HPBluetoothManager instance] connectBluetooth:self.peripheralInfo successBlock:^{ NSLog(@"连接成功"); } failedBlock:^{ NSLog(@"连接失败"); }]; }
-
配网开始
- 输入wifi名、wifi密码
- (void)beginCASConnect{ kWeakSelf; [[HPWifiConnectManager instance] wifiConnectWithPeripheralInfo:self.peripheralInfo WifiName:self.wifiName wifiPassword:self.wifiPassword successBlock:^(MDNSDevice * _Nonnull device) { [ConnectBedManager instance].networkBed.device_id = device.Devicename; [ConnectBedManager instance].networkBed.bed_mode = device.Bed_MOD.intValue; [ConnectBedManager instance].networkBed.device_ip = device.IP; [weakSelf bindBedToService]; }]; } - (void)bindBedToService{ kWeakSelf; [HPAlert normalAlertTitle:@"" message:@"设备配网成功" left:@"确定" right:@"" leftBlock:^{ [GlobalData instance].bed_info.device_id = [ConnectBedManager instance].wantConnectDeviceId; UIViewController *vc = [[UIStoryboard storyboardWithName:@"Remote" bundle:nil] instantiateViewControllerWithIdentifier:@"NewSFD2ControlViewController"]; [self.navigationController pushViewController:vc animated:YES]; } rightBlock:nil]; }
- 在app进入后台和进入前台时,需要断开和重新搜索设备
-(void)willEnterForeground { [[HPWifiConnectManager instance] hp_willEnterForeground]; } -(void)didEnterBackground { [[HPWifiConnectManager instance] hp_didEnterBackground]; }
-
-
2.控制模块
- app连接设备
-(void)connectDevice { self.selectDevice = [ConnectBedManager instance].wantConnectDeviceId; NSString *bindDeviceIP = [ConnectBedManager instance].networkBed.device_ip; [[HPControlManager instance] connectDevice:self.selectDevice ip:bindDeviceIP successBlock:^{ NSLog(@"app连接设备成功"); }]; }
- 设置按钮的点击动作,触发点击动作时,发送指令
- (void)initUIAndData{ NSArray *buttonArray = [[NSArray alloc] initWithObjects:btnHeadUp,btnHeadDown,btnFootUp,btnFootDown,btnFlat,btnZg,btnLight,btnMassHead,btnMassFoot,btnMassStop,btnM1,btnM2,massageBtn0,massageBtn1,massageBtn2, nil]; for (UIButton *button in buttonArray) { // 禁止多个按钮同时按下 [button setExclusiveTouch:YES]; // 按键事件 [button addTarget:self action:@selector(btnPressed:) forControlEvents:UIControlEventTouchDown]; [button addTarget:self action:@selector(btnReleased:) forControlEvents:UIControlEventTouchUpInside]; [button addTarget:self action:@selector(btnReleased:) forControlEvents:UIControlEventTouchUpOutside]; [button addTarget:self action:@selector(btnReleased:) forControlEvents:UIControlEventTouchCancel]; button.showsTouchWhenHighlighted = YES; } // 每隔200ms连续发送命令 timer = [NSTimer scheduledTimerWithTimeInterval:0.3 target:self selector:@selector(sendContinuousMessage) userInfo:nil repeats:YES]; // 先关闭定时器 [timer setFireDate:[NSDate distantFuture]]; } //设置按钮点击时向设备发送的命令 - (void)btnPressed:(id)sender { UIButton *button = (UIButton *)sender; if([GVUserDefaults standardUserDefaults].vibrate) { AudioServicesPlaySystemSound(kSystemSoundID_Vibrate); } if ([self.btnHeadUp isEqual:button]) { self.type = HPCommandType_Header_Up; [self buttonContinuousDown]; } else if ([self.btnHeadDown isEqual:button]) { self.type = HPCommandType_Header_Down; [self buttonContinuousDown]; } else if ([self.btnFootUp isEqual:button]) { self.type = HPCommandType_Footer_Up; [self buttonContinuousDown]; } else if ([self.btnFootDown isEqual:button]) { self.type = HPCommandType_Footer_Down; [self buttonContinuousDown]; } else if ([self.btnFlat isEqual:button]) { self.type = HPCommandType_Position_Flat; } else if ([self.btnZg isEqual:button]) { self.type = HPCommandType_Position_Zero; } else if ([self.btnMassHead isEqual:button]) { self.type = HPCommandType_Massage_Header_Down; } else if ([self.btnMassFoot isEqual:button]) { self.type = HPCommandType_Massage_Footer_Down; } else if ([self.btnMassStop isEqual:button]) { self.type = HPCommandType_Massage_All_Stop; } else if ([self.btnLight isEqual:button]) { self.type = HPCommandType_Light_OnOff; } else if ([self.btnM1 isEqual:button]) { self.type = HPCommandType_Position_Music; } else if ([self.btnM2 isEqual:button]) { self.type = HPCommandType_Position_Extend; } else if ([self.massageBtn0 isEqual:button]){ self.type = HPCommandType_Massage_Type_Trapezoid; } else if ([self.massageBtn1 isEqual:button]){ self.type = HPCommandType_Massage_Type_Sin; } else if ([self.massageBtn2 isEqual:button]){ self.type = HPCommandType_Massage_Type_Triangle; } } - (void)btnReleased:(id)sender { // 持续发送模式,电机运动,按键抬起时关闭定时器,下发停止动作命令 if (isContinousSend) { // 定时器关闭 [timer setFireDate:[NSDate distantFuture]]; // 延迟0.2s再发一条 [self sendZero]; isContinousSend = false; } else { [self sendSingle]; [self sendZero]; } } -(void)sendSingle { // 单发指令 for(int i = 0;i<1;i++) { dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * i * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ [[HPControlManager instance] sendCommand:self.type]; }); } } //发送结束时的命令 -(void)sendZero { dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ [[HPControlManager instance] sendCommand:HPCommandType_Control_Zero]; }); } -(void)buttonContinuousDown { isContinousSend = true; [timer setFireDate:[NSDate distantPast]]; } -(void)sendContinuousMessage { [[HPControlManager instance] sendCommand:self.type]; }
- 在进入后台、前台,以及离开页面时需要连接或者断开连接
-(void)willEnterForeground { [self connectDevice]; NSLog(@"home进入前台,打开socket"); } // 按home键触发 -(void)didEnterBackground{ [self unConnectDevice]; NSLog(@"home进入后台,断开socket"); } //取消订阅并关闭与MQTT Server的连接 -(void)viewWillDisappear:(BOOL)animated { [super viewWillDisappear:animated]; [self unConnectDevice]; NSLog(@"断开socket"); }
- 需要发送命令的枚举类型,详解
typedef NS_ENUM(NSInteger, HPCommandType) { //0x00000000 停止发送指令 HPCommandType_Control_Zero = 1000, //0x00000010 头部驱动器伸出 HPCommandType_Header_Up = 1001, //0x00000020 头部驱动器缩进 HPCommandType_Header_Down = 1002, //0x00000001 背部驱动器伸出 HPCommandType_Back_Up = 1003, //0x00000002 背部驱动器缩进; HPCommandType_Back_Down = 1004, //0x00000040 腰部驱动器伸出 HPCommandType_Waist_Up = 1005, //0x00000080 腰部驱动器缩进 HPCommandType_Waist_Down = 1006, //0x00000004 脚部驱动器伸出; HPCommandType_Footer_Up = 1007, //0x00000008 脚部驱动器缩进; HPCommandType_Footer_Down = 1008, //0x00020000 床底灯开/关 HPCommandType_Light_OnOff = 1009, //0x00000100 全部按摩打开,3个Level强度 HPCommandType_Massage_All_Level_Control = 1010, //0x02000000 全部按摩关闭 HPCommandType_Massage_All_Stop = 1011, //0x00000200 按摩定时,10min,20min,30min HPCommandType_Massage_All_Level_Time = 1012, //0x00080000 按摩模式-三角波 HPCommandType_Massage_Type_Triangle = 1013, //0x00100000 按摩模式-梯形波 HPCommandType_Massage_Type_Trapezoid = 1014, //0x00200000 按摩模式-正弦波 HPCommandType_Massage_Type_Sin = 1015, //0x00000800 背部按摩强度增加 HPCommandType_Massage_Back_Up = 1016, //0x00800000 背部按摩强度减少 HPCommandType_Massage_Back_Down = 1017, //0x00000400 脚部按摩强度增加 HPCommandType_Massage_Footer_Up = 1018, //0x01000000 脚部按摩强度减少 HPCommandType_Massage_Footer_Down = 1019, //0x00008000 护脊起身 HPCommandType_Position_ProtectSpine = 1020, //0x00001000 零压模式 HPCommandType_Position_Zero = 1021, //0x40000000 休闲模式 HPCommandType_Position_Relax = 1022, //0x00002000 阅读/游戏模式 HPCommandType_Position_Read = 1023, //0x00004000 TV模式 HPCommandType_Position_TV = 1024, //0x00010000 记忆位置 HPCommandType_Position_Memory = 1025, //0x08000000 一键放平 HPCommandType_Position_Flat = 1026, };