1.接入SDK
-
1. 將connect.framework添加到項(xiàng)目中
-
方法一:直接拖拽到Xcode中,需要勾選Copy items if needed。
image.png -
方法二:先將connect.framework拷貝到項(xiàng)目文件夾中,在Xcode中添加該connect.framework,選擇Add Files to "****",注意這里不用勾選Copy items if needed
image.png
image.png
image.png
-
-
2.在使用到SDK功能的時(shí)候引用SDK
- 引用 #import <connect/connect.h>
2.使用SDK
-
1. 配網(wǎng)模塊
-
添加藍(lán)牙權(quán)限
- 首先添加app藍(lán)牙使用權(quán)限,在TARGETS->Info->Custom iOS Target Properties中添加下面兩個(gè)權(quán)限
Privacy - Bluetooth Always Usage Description
Privacy - Bluetooth Peripheral Usage Description - 引用 #import <CoreBluetooth/CoreBluetooth.h>
- 檢測(cè)藍(lán)牙是否打開(kāi)
創(chuàng)建實(shí)例對(duì)象,并設(shè)置代理
在代理方法中監(jiān)聽(tīng),藍(lán)牙是否打開(kāi)
CBCentralManager *centralManager = [[CBCentralManager alloc] initWithDelegate:self queue:nil options:@{CBCentralManagerOptionShowPowerAlertKey:@NO}];- (void)centralManagerDidUpdateState:(CBCentralManager *)central { if ([central state] == CBCentralManagerStatePoweredOn) { NSLog(@"藍(lán)牙已打開(kāi)"); } else if ([central state] == CBManagerStateUnauthorized) { NSLog(@"藍(lán)牙關(guān)閉"); } else { NSLog(@"藍(lán)牙關(guān)閉"); } } - 首先添加app藍(lán)牙使用權(quán)限,在TARGETS->Info->Custom iOS Target Properties中添加下面兩個(gè)權(quán)限
-
藍(lán)牙搜索、連接
- 開(kāi)始搜索
- (void)beginSearch{ kWeakSelf; [[HPBluetoothManager instance] startBluetoothSearch:^(NSArray * _Nonnull peripheralInfoArr) { weakSelf.peripheralsArray = [NSMutableArray arrayWithArray:peripheralInfoArr]; [weakSelf.tableView reloadData]; }]; }- 從搜索出來(lái)的設(shè)備中選擇想要配網(wǎng)的設(shè)備,進(jìn)行配網(wǎng)
- 開(kāi)始藍(lán)牙連接
- (void)clickConnect{ kWeakSelf; [[HPBluetoothManager instance] connectBluetooth:self.peripheralInfo successBlock:^{ NSLog(@"連接成功"); } failedBlock:^{ NSLog(@"連接失敗"); }]; } -
配網(wǎng)開(kāi)始
- 輸入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:@"設(shè)備配網(wǎng)成功" 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進(jìn)入后臺(tái)和進(jìn)入前臺(tái)時(shí),需要斷開(kāi)和重新搜索設(shè)備
-(void)willEnterForeground { [[HPWifiConnectManager instance] hp_willEnterForeground]; } -(void)didEnterBackground { [[HPWifiConnectManager instance] hp_didEnterBackground]; }
-
-
2.控制模塊
- app連接設(shè)備
-(void)connectDevice { self.selectDevice = [ConnectBedManager instance].wantConnectDeviceId; NSString *bindDeviceIP = [ConnectBedManager instance].networkBed.device_ip; [[HPControlManager instance] connectDevice:self.selectDevice ip:bindDeviceIP successBlock:^{ NSLog(@"app連接設(shè)備成功"); }]; }- 設(shè)置按鈕的點(diǎn)擊動(dòng)作,觸發(fā)點(diǎn)擊動(dòng)作時(shí),發(fā)送指令
- (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) { // 禁止多個(gè)按鈕同時(shí)按下 [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連續(xù)發(fā)送命令 timer = [NSTimer scheduledTimerWithTimeInterval:0.3 target:self selector:@selector(sendContinuousMessage) userInfo:nil repeats:YES]; // 先關(guān)閉定時(shí)器 [timer setFireDate:[NSDate distantFuture]]; } //設(shè)置按鈕點(diǎn)擊時(shí)向設(shè)備發(fā)送的命令 - (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 { // 持續(xù)發(fā)送模式,電機(jī)運(yùn)動(dòng),按鍵抬起時(shí)關(guān)閉定時(shí)器,下發(fā)停止動(dòng)作命令 if (isContinousSend) { // 定時(shí)器關(guān)閉 [timer setFireDate:[NSDate distantFuture]]; // 延遲0.2s再發(fā)一條 [self sendZero]; isContinousSend = false; } else { [self sendSingle]; [self sendZero]; } } -(void)sendSingle { // 單發(fā)指令 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]; }); } } //發(fā)送結(jié)束時(shí)的命令 -(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]; }- 在進(jìn)入后臺(tái)、前臺(tái),以及離開(kāi)頁(yè)面時(shí)需要連接或者斷開(kāi)連接
-(void)willEnterForeground { [self connectDevice]; NSLog(@"home進(jìn)入前臺(tái),打開(kāi)socket"); } // 按home鍵觸發(fā) -(void)didEnterBackground{ [self unConnectDevice]; NSLog(@"home進(jìn)入后臺(tái),斷開(kāi)socket"); } //取消訂閱并關(guān)閉與MQTT Server的連接 -(void)viewWillDisappear:(BOOL)animated { [super viewWillDisappear:animated]; [self unConnectDevice]; NSLog(@"斷開(kāi)socket"); }- 需要發(fā)送命令的枚舉類型,詳解
typedef NS_ENUM(NSInteger, HPCommandType) { //0x00000000 停止發(fā)送指令 HPCommandType_Control_Zero = 1000, //0x00000010 頭部驅(qū)動(dòng)器伸出 HPCommandType_Header_Up = 1001, //0x00000020 頭部驅(qū)動(dòng)器縮進(jìn) HPCommandType_Header_Down = 1002, //0x00000001 背部驅(qū)動(dòng)器伸出 HPCommandType_Back_Up = 1003, //0x00000002 背部驅(qū)動(dòng)器縮進(jìn); HPCommandType_Back_Down = 1004, //0x00000040 腰部驅(qū)動(dòng)器伸出 HPCommandType_Waist_Up = 1005, //0x00000080 腰部驅(qū)動(dòng)器縮進(jìn) HPCommandType_Waist_Down = 1006, //0x00000004 腳部驅(qū)動(dòng)器伸出; HPCommandType_Footer_Up = 1007, //0x00000008 腳部驅(qū)動(dòng)器縮進(jìn); HPCommandType_Footer_Down = 1008, //0x00020000 床底燈開(kāi)/關(guān) HPCommandType_Light_OnOff = 1009, //0x00000100 全部按摩打開(kāi),3個(gè)Level強(qiáng)度 HPCommandType_Massage_All_Level_Control = 1010, //0x02000000 全部按摩關(guān)閉 HPCommandType_Massage_All_Stop = 1011, //0x00000200 按摩定時(shí),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 背部按摩強(qiáng)度增加 HPCommandType_Massage_Back_Up = 1016, //0x00800000 背部按摩強(qiáng)度減少 HPCommandType_Massage_Back_Down = 1017, //0x00000400 腳部按摩強(qiáng)度增加 HPCommandType_Massage_Footer_Up = 1018, //0x01000000 腳部按摩強(qiáng)度減少 HPCommandType_Massage_Footer_Down = 1019, //0x00008000 護(hù)脊起身 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, };



