iOS AILinkBleSDK - 蓝牙SDK
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ToothbrushConnectionViewController.m 26KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640
  1. //
  2. // ToothbrushConnectionViewController.m
  3. // AILinkBleSDKSourceCode
  4. //
  5. // Created by iot_user on 2020/9/29.
  6. // Copyright © 2020 IOT. All rights reserved.
  7. //
  8. #import "ToothbrushConnectionViewController.h"
  9. #import <AILinkBleSDK/ELToothbrushBleManager.h>
  10. #import "Masonry.h"
  11. #import "ELInputAlertView.h"
  12. #import "ELSelectView.h"
  13. #import <AILinkBleSDK/ELBluetoothManager+BleWifi.h>
  14. static NSString * const authorizeIntervalKey = @"authorizeIntervalKey";
  15. @interface ToothbrushConnectionViewController ()<ToothbrushDelegate,ELBluetoothManagerDelegate,ElBleWifiDelegate>
  16. @property (nonatomic, strong) UITextView *textView;
  17. //附近wifi
  18. @property (nonatomic, strong) NSMutableArray<ELBleWifiDetailModel *> *wifiArr;
  19. @property (nonatomic, copy) NSString *wifiPwd;
  20. @end
  21. @implementation ToothbrushConnectionViewController
  22. -(NSMutableArray<ELBleWifiDetailModel *> *)wifiArr{
  23. if (_wifiArr==nil) {
  24. _wifiArr = [[NSMutableArray alloc] init];
  25. }
  26. return _wifiArr;
  27. }
  28. - (void)viewDidLoad {
  29. [super viewDidLoad];
  30. [self setupUI];
  31. [[ELToothbrushBleManager shareManager] startScan];
  32. [ELToothbrushBleManager shareManager].toothbrushDelegate = self;
  33. [ELToothbrushBleManager shareManager].delegate = self;
  34. [ELToothbrushBleManager shareManager].bleWifiDelegate = self;
  35. }
  36. -(void)addLog:(NSString *)log{
  37. self.textView.text = [NSString stringWithFormat:@"%@\n%@",log,self.textView.text];
  38. }
  39. -(void)buttonAction:(UIButton *)sender{
  40. NSInteger tag = sender.tag;
  41. if (tag == 1) {
  42. //MARK:配网
  43. if ([ELToothbrushBleManager shareManager].state == ELBluetoothStateDidValidationPass) {
  44. [self.wifiArr removeAllObjects];
  45. [self addLog:@"正在搜索附近的WiFi..."];
  46. //搜索附近网络
  47. [[ELToothbrushBleManager shareManager] getBluetoothInfoWithELInetGetCmdType:ELInetGetCmdTypeBleWifiGetNearbyWifi];
  48. }else{
  49. [self addLog:@"未连接设备"];
  50. [self.navigationController popViewControllerAnimated:YES];
  51. }
  52. }else if (tag == 2){
  53. //MARK:配对
  54. long long interval = [[NSUserDefaults standardUserDefaults] integerForKey:authorizeIntervalKey];
  55. if (interval == 0) {
  56. interval = [[NSDate date] timeIntervalSince1970];
  57. [[NSUserDefaults standardUserDefaults] setInteger:interval forKey:authorizeIntervalKey];
  58. [[NSUserDefaults standardUserDefaults] synchronize];
  59. }
  60. [self addLog:@"发起配对"];
  61. [[ELToothbrushBleManager shareManager] requestDeviceAuthorization:interval];
  62. }
  63. else if (tag == 3){
  64. //MARK:获取设备ID
  65. [self addLog:@"请求设备ID"];
  66. [[ELToothbrushBleManager shareManager] getBluetoothInfoWithELInetGetCmdType:ELInetGetCmdTypeBleWifiGetSNNumber];
  67. }
  68. else if (tag == 4){
  69. //MARK:获取档位
  70. [self addLog:@"请求支持的档位"];
  71. [[ELToothbrushBleManager shareManager] getBluetoothInfoWithELInetGetCmdType:ELInetGetCmdTypeGetBasicInfoData];
  72. }else if (tag == 5){
  73. //MARK:获取三轴方向
  74. [self addLog:@"获取三轴方向"];
  75. [[ELToothbrushBleManager shareManager] getTriaxialDirection];
  76. }else if (tag == 6){
  77. //MARK:获取三轴数据
  78. [self addLog:@"获取三轴数据"];
  79. [[ELToothbrushBleManager shareManager] getTriaxialData];
  80. }else if (tag == 7){
  81. //MARK:查询默认刷牙时长和工作档位
  82. [self addLog:@"查询默认刷牙时长和工作档位"];
  83. [[ELToothbrushBleManager shareManager] getWorkGear];
  84. }
  85. else if (tag == 8){
  86. //MARK:查询手动设置信息
  87. [self addLog:@"查询手动设置信息"];
  88. [[ELToothbrushBleManager shareManager] getCustomGear];
  89. }
  90. else if (tag == 9){
  91. //MARK:查询二级档位默认值
  92. [self addLog:@"查询二级档位默认值"];
  93. [[ELToothbrushBleManager shareManager] getTwoGearDefualt];
  94. }else if (tag == 10){
  95. //MARK:上报工作阶段
  96. [self addLog:@"上报工作阶段"];
  97. [[ELToothbrushBleManager shareManager] getWorkPhase];
  98. }
  99. else if (tag == 11){
  100. //MARK:设置刷牙模式
  101. [self addLog:@"设置刷牙模式:舒柔"];
  102. [[ELToothbrushBleManager shareManager] setWorkGear:(ToothbrushGearTypeSoft_0A) interval:120 supportGearType:(ToothSupportGearTypeOneGear_01)];
  103. }else if (tag == 12){
  104. //MARK:开关
  105. [self addLog:@"开关"];
  106. [[ELToothbrushBleManager shareManager] switchToothbrush];
  107. }else if (tag == 13){
  108. //MARK:设置二档默认
  109. [self addLog:@"设置二档默认(清洁)"];
  110. [[ELToothbrushBleManager shareManager] setTwoGearDefaultGear:(ToothbrushGearTypeClean_01)];
  111. }else if (tag == 14){
  112. //MARK:试用
  113. [self addLog:@"试用,手动设置,二档,frequency:67 duty:50"];
  114. [[ELToothbrushBleManager shareManager] tryGear:(ToothbrushGearTypeCustom_FF) supportType:(ToothSupportGearTypeTwoGear_02) frequency:67 duty:50];
  115. }else if (tag == 15){
  116. //MARK:设置手动档
  117. [self addLog:@"设置手动档:frequency:67 duty:50 interval:120"];
  118. [[ELToothbrushBleManager shareManager] setCustomGearData:67 duty:50 interval:120];
  119. }else if (tag == 16){
  120. //MARK:上报接收数据结果
  121. [self addLog:@"上报接收数据结果"];
  122. [[ELToothbrushBleManager shareManager] reportReceiveResultOfToothbrushData:YES];
  123. } else if (tag == 17) {
  124. [[ELToothbrushBleManager shareManager] getPreventSplashingMode];
  125. [self addLog:@"获取防飞溅模式"];
  126. } else if (tag == 18) {
  127. [[ELToothbrushBleManager shareManager] setPreventSplashingMode:YES];
  128. [self addLog:@"设置防飞溅模式 on"];
  129. } else if (tag == 19) {
  130. [[ELToothbrushBleManager shareManager] setPreventSplashingMode:NO];
  131. [self addLog:@"设置防飞溅模式 off"];
  132. }
  133. else{
  134. }
  135. }
  136. -(void)setupUI{
  137. self.view.backgroundColor = [UIColor whiteColor];
  138. UIButton *button1 = [self getButtonWithTitle:@"配网" withTag:1];
  139. [self.view addSubview:button1];
  140. [button1 mas_makeConstraints:^(MASConstraintMaker *make) {
  141. make.left.mas_equalTo(10);
  142. make.top.mas_equalTo(70);
  143. make.width.mas_equalTo(70);
  144. make.height.mas_equalTo(30);
  145. }];
  146. UIButton *button2 = [self getButtonWithTitle:@"配对" withTag:2];
  147. [self.view addSubview:button2];
  148. [button2 mas_makeConstraints:^(MASConstraintMaker *make) {
  149. make.left.equalTo(button1.mas_right).mas_offset(5);
  150. make.top.equalTo(button1);
  151. make.width.mas_equalTo(70);
  152. make.height.mas_equalTo(30);
  153. }];
  154. UIButton *button3 = [self getButtonWithTitle:@"获取设备ID" withTag:3];
  155. [self.view addSubview:button3];
  156. [button3 mas_makeConstraints:^(MASConstraintMaker *make) {
  157. make.left.equalTo(button2.mas_right).mas_offset(5);
  158. make.top.equalTo(button2);
  159. make.width.mas_equalTo(70);
  160. make.height.mas_equalTo(30);
  161. }];
  162. UIButton *button4 = [self getButtonWithTitle:@"获取档位" withTag:4];
  163. [self.view addSubview:button4];
  164. [button4 mas_makeConstraints:^(MASConstraintMaker *make) {
  165. make.left.equalTo(button3.mas_right).mas_offset(5);
  166. make.top.equalTo(button3);
  167. make.width.mas_equalTo(70);
  168. make.height.mas_equalTo(30);
  169. }];
  170. UIButton *button5 = [self getButtonWithTitle:@"获取三轴方向" withTag:5];
  171. [self.view addSubview:button5];
  172. [button5 mas_makeConstraints:^(MASConstraintMaker *make) {
  173. make.left.mas_equalTo(10);
  174. make.top.equalTo(button4.mas_bottom).mas_offset(10);
  175. make.width.mas_equalTo(70);
  176. make.height.mas_equalTo(30);
  177. }];
  178. UIButton *button6 = [self getButtonWithTitle:@"获取三轴数据" withTag:6];
  179. [self.view addSubview:button6];
  180. [button6 mas_makeConstraints:^(MASConstraintMaker *make) {
  181. make.left.equalTo(button5.mas_right).mas_offset(5);
  182. make.top.equalTo(button5);
  183. make.width.mas_equalTo(70);
  184. make.height.mas_equalTo(30);
  185. }];
  186. UIButton *button7 = [self getButtonWithTitle:@"查询默认模式和时长" withTag:7];
  187. [self.view addSubview:button7];
  188. [button7 mas_makeConstraints:^(MASConstraintMaker *make) {
  189. make.left.equalTo(button6.mas_right).mas_offset(5);
  190. make.top.equalTo(button6);
  191. make.width.mas_equalTo(70);
  192. make.height.mas_equalTo(30);
  193. }];
  194. UIButton *button8 = [self getButtonWithTitle:@"查询手动设置" withTag:8];
  195. [self.view addSubview:button8];
  196. [button8 mas_makeConstraints:^(MASConstraintMaker *make) {
  197. make.left.equalTo(button7.mas_right).mas_offset(5);
  198. make.top.equalTo(button7);
  199. make.width.mas_equalTo(70);
  200. make.height.mas_equalTo(30);
  201. }];
  202. UIButton *button9 = [self getButtonWithTitle:@"查询二级档位默认" withTag:9];
  203. [self.view addSubview:button9];
  204. [button9 mas_makeConstraints:^(MASConstraintMaker *make) {
  205. make.left.equalTo(button5);
  206. make.top.equalTo(button5.mas_bottom).mas_offset(10);
  207. make.width.mas_equalTo(70);
  208. make.height.mas_equalTo(30);
  209. }];
  210. UIButton *button10 = [self getButtonWithTitle:@"上报工作阶段" withTag:10];
  211. [self.view addSubview:button10];
  212. [button10 mas_makeConstraints:^(MASConstraintMaker *make) {
  213. make.left.equalTo(button9.mas_right).mas_offset(5);
  214. make.top.equalTo(button9);
  215. make.width.mas_equalTo(70);
  216. make.height.mas_equalTo(30);
  217. }];
  218. UIButton *button11 = [self getButtonWithTitle:@"设置刷牙模式(0A)" withTag:11];
  219. [self.view addSubview:button11];
  220. [button11 mas_makeConstraints:^(MASConstraintMaker *make) {
  221. make.left.equalTo(button10.mas_right).mas_offset(5);
  222. make.top.equalTo(button9);
  223. make.width.mas_equalTo(70);
  224. make.height.mas_equalTo(30);
  225. }];
  226. UIButton *button12 = [self getButtonWithTitle:@"开关" withTag:12];
  227. [self.view addSubview:button12];
  228. [button12 mas_makeConstraints:^(MASConstraintMaker *make) {
  229. make.left.equalTo(button11.mas_right).mas_offset(5);
  230. make.top.equalTo(button9);
  231. make.width.mas_equalTo(70);
  232. make.height.mas_equalTo(30);
  233. }];
  234. UIButton *button13 = [self getButtonWithTitle:@"设置二档默认" withTag:13];
  235. [self.view addSubview:button13];
  236. [button13 mas_makeConstraints:^(MASConstraintMaker *make) {
  237. make.left.equalTo(button9);
  238. make.top.equalTo(button9.mas_bottom).mas_offset(10);
  239. make.width.mas_equalTo(70);
  240. make.height.mas_equalTo(30);
  241. }];
  242. UIButton *button14 = [self getButtonWithTitle:@"试用" withTag:14];
  243. [self.view addSubview:button14];
  244. [button14 mas_makeConstraints:^(MASConstraintMaker *make) {
  245. make.left.equalTo(button13.mas_right).mas_offset(5);
  246. make.top.equalTo(button13);
  247. make.width.mas_equalTo(70);
  248. make.height.mas_equalTo(30);
  249. }];
  250. UIButton *button15 = [self getButtonWithTitle:@"设置手动档" withTag:15];
  251. [self.view addSubview:button15];
  252. [button15 mas_makeConstraints:^(MASConstraintMaker *make) {
  253. make.left.equalTo(button14.mas_right).mas_offset(5);
  254. make.top.equalTo(button13);
  255. make.width.mas_equalTo(70);
  256. make.height.mas_equalTo(30);
  257. }];
  258. UIButton *button16 = [self getButtonWithTitle:@"上报接收数据结果" withTag:16];
  259. [self.view addSubview:button16];
  260. [button16 mas_makeConstraints:^(MASConstraintMaker *make) {
  261. make.left.equalTo(button15.mas_right).mas_offset(5);
  262. make.top.equalTo(button13);
  263. make.width.mas_equalTo(70);
  264. make.height.mas_equalTo(30);
  265. }];
  266. UIButton *button17 = [self getButtonWithTitle:@"获取防飞溅" withTag:17];
  267. [self.view addSubview:button17];
  268. [button17 mas_makeConstraints:^(MASConstraintMaker *make) {
  269. make.left.equalTo(button13);
  270. make.top.equalTo(button13.mas_bottom).mas_offset(10);
  271. make.width.mas_equalTo(70);
  272. make.height.mas_equalTo(30);
  273. }];
  274. UIButton *button18 = [self getButtonWithTitle:@"设置防飞溅On" withTag:18];
  275. [self.view addSubview:button18];
  276. [button18 mas_makeConstraints:^(MASConstraintMaker *make) {
  277. make.left.equalTo(button17.mas_right).mas_offset(5);
  278. make.top.equalTo(button17);
  279. make.width.mas_equalTo(70);
  280. make.height.mas_equalTo(30);
  281. }];
  282. UIButton *button19 = [self getButtonWithTitle:@"设置防飞溅Off" withTag:19];
  283. [self.view addSubview:button19];
  284. [button19 mas_makeConstraints:^(MASConstraintMaker *make) {
  285. make.left.equalTo(button18.mas_right).mas_offset(5);
  286. make.top.equalTo(button17);
  287. make.width.mas_equalTo(70);
  288. make.height.mas_equalTo(30);
  289. }];
  290. //
  291. self.textView = [[UITextView alloc] init];
  292. self.textView.backgroundColor = [UIColor blackColor];
  293. self.textView.text = @"Log";
  294. self.textView.textColor = [UIColor redColor];
  295. self.textView.font = [UIFont systemFontOfSize:16];
  296. self.textView.editable = NO;
  297. [self.view addSubview:self.textView];
  298. [self.textView mas_makeConstraints:^(MASConstraintMaker *make) {
  299. make.left.mas_equalTo(10);
  300. make.right.bottom.mas_equalTo(-10);
  301. make.height.mas_equalTo(200);
  302. }];
  303. }
  304. -(UIButton *)getButtonWithTitle:(NSString *)title withTag:(NSInteger)tag{
  305. UIButton *button1 = [UIButton buttonWithType:(UIButtonTypeCustom)];
  306. [button1 setTitle:title forState:(UIControlStateNormal)];
  307. button1.tag = tag;
  308. button1.titleLabel.adjustsFontSizeToFitWidth = YES;
  309. button1.titleLabel.numberOfLines = 2;
  310. button1.titleLabel.font = [UIFont systemFontOfSize:15];
  311. [button1 setTitleColor:[UIColor redColor] forState:(UIControlStateNormal)];
  312. button1.backgroundColor = [UIColor blackColor];
  313. button1.titleLabel.textAlignment = NSTextAlignmentCenter;
  314. button1.titleLabel.adjustsFontSizeToFitWidth = YES;
  315. [button1 addTarget:self action:@selector(buttonAction:) forControlEvents:(UIControlEventTouchUpInside)];
  316. return button1;
  317. }
  318. -(void)connectWiFiWithPwd:(NSString *)pwd wifi:(ELBleWifiDetailModel *)wifi{
  319. [self addLog:@"正在配置WiFi名称"];
  320. self.wifiPwd = pwd;
  321. [[ELToothbrushBleManager shareManager] bleWifiSetConnectWifiMac:wifi.macData];
  322. }
  323. -(void)connectWiFi:(ELBleWifiDetailModel *)wifi{
  324. if (wifi.secureType ==ELBleWifiSecureTypeOpen) {
  325. [self connectWiFiWithPwd:@"" wifi:wifi];
  326. }else{
  327. ELInputAlertView *input = [[ELInputAlertView alloc] initWithTittle:@"配置WiFi" withText:@"" withMessage:@"请输入WiFi密码" withLeftButton:@"取消" withRightButton:@"确定"];
  328. [input show];
  329. __weak typeof(self) weakSelf = self;
  330. input.rightHideBlock = ^BOOL(NSString * _Nonnull text, UIView * _Nonnull view) {
  331. if ([ELBlePublicTool legalBleWifiPassword:text]) {
  332. [weakSelf connectWiFiWithPwd:text wifi:wifi];
  333. return YES;
  334. }else{
  335. [self addLog:@"请输入8~56位的密码"];
  336. return NO;
  337. }
  338. };
  339. }
  340. }
  341. #pragma mark ============ ElBleWifiDelegate ==============
  342. //获取设备ID
  343. -(void)bleWifiReceiveWifiSNCode:(int)code{
  344. if (code == 0) {
  345. [self addLog:@"设备ID不存在,请先配网再获取"];
  346. }else{
  347. [self addLog:[NSString stringWithFormat:@"设备ID = %d",code]];
  348. }
  349. }
  350. //设置wifi过程结果
  351. -(void)bleWifiReceiceResponseType:(ELInetSetCmdType)type result:(ELSetBluetoothResponseType)result{
  352. if (result == ELSetBluetoothResponseTypeSuccess) {
  353. //设置WiFi结果
  354. if (type == ELInetSetCmdTypeBleWifiSetWifiMac) {
  355. [self addLog:@"正在配置WiFi密码"];
  356. [[ELToothbrushBleManager shareManager] bleWifiSetWifiPwd:self.wifiPwd];
  357. }else if (type == ELInetSetCmdTypeBleWifiSetWifiPwd){
  358. [self addLog:@"设置WiFi连接"];
  359. [[ELToothbrushBleManager shareManager] bleWifiSetupWifiConnect:YES];
  360. }else if (type == ELInetSetCmdTypeBleWifiSetConnectWifi){
  361. //获取连接状态
  362. }
  363. }
  364. else{
  365. [self addLog:@"WiFi配置失败"];
  366. }
  367. }
  368. //附近wifi
  369. -(void)bleWifiReceiveWifiDetailModel:(ELBleWifiDetailModel *)model{
  370. [self addLog:[NSString stringWithFormat:@"搜索到wifi类型---%lu 名称---%@",(unsigned long)model.wifiState,model.wifiName]];
  371. if (model.wifiState == ELBleWifiUseStateConnected) {
  372. }
  373. [self.wifiArr addObject:model];
  374. }
  375. //附近wifi搜索结果
  376. -(void)bleWifiReceiveScanedWifiCount:(int)count {
  377. [self addLog:@"搜索完成"];
  378. //扫描结束
  379. NSMutableArray *array = [[NSMutableArray alloc] init];
  380. for (int i=0; i<self.wifiArr.count; i++) {
  381. ELBleWifiDetailModel *model = self.wifiArr[i];
  382. NSString *wifiTypeStr = [NSString stringWithFormat:@"wifi类型:%lu 名称:%@",(unsigned long)model.secureType,model.wifiName];
  383. if (model.secureType == ELBleWifiSecureTypeOpen) {
  384. wifiTypeStr = [NSString stringWithFormat:@"wifi类型:%@ 名称:%@",@"Open",model.wifiName];
  385. }else if (model.secureType == ELBleWifiSecureTypeWEP){
  386. wifiTypeStr = [NSString stringWithFormat:@"wifi类型:%@ 名称:%@",@"WEP",model.wifiName];
  387. }else if (model.secureType == ELBleWifiSecureTypeWPA_PSK){
  388. wifiTypeStr = [NSString stringWithFormat:@"wifi类型:%@ 名称:%@",@"WPA_PSK",model.wifiName];
  389. }else if (model.secureType == ELBleWifiSecureTypeWPA2_PSK){
  390. wifiTypeStr = [NSString stringWithFormat:@"wifi类型:%@ 名称:%@",@"WPA2_PSK",model.wifiName];
  391. }else if (model.secureType == ELBleWifiSecureTypeWPA_WPA_2_PSK){
  392. wifiTypeStr = [NSString stringWithFormat:@"wifi类型:%@ 名称:%@",@"WPA_WPA_2_PSK",model.wifiName];
  393. }else if (model.secureType == ELBleWifiSecureTypeWPA2_ENTERPRISE){
  394. wifiTypeStr = [NSString stringWithFormat:@"wifi类型:%@ 名称:%@",@"WPA2_ENTERPRISE",model.wifiName];
  395. }
  396. [array addObject:wifiTypeStr];
  397. }
  398. ELSelectView *selectView = [[ELSelectView alloc] initWithTitle:@"附近WiFi" withSelectArray:array];
  399. [selectView show];
  400. __weak typeof(self) weakSelf = self;
  401. selectView.selectRowBlock = ^(NSInteger row) {
  402. [weakSelf connectWiFi:weakSelf.wifiArr[row]];
  403. };
  404. }
  405. //连接状态
  406. -(void)toothbrushReceiveState:(ELBluetoothState)state{
  407. if (state == ELBluetoothStateUnavailable) {
  408. self.title = @"请打开蓝牙";
  409. }else if (state == ELBluetoothStateScaning || state == ELBluetoothStateWillConnect || state == ELBluetoothStateDidConnect){
  410. self.title = @"正在连接...";
  411. }else if (state == ELBluetoothStateConnectFail || state == ELBluetoothStateDidDisconnect || state == ELBluetoothStateFailedValidation){
  412. self.title = @"连接断开";
  413. }else if (state == ELBluetoothStateDidValidationPass){
  414. self.title = @"连接成功";
  415. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  416. self.title = self.p.macAddress;
  417. });
  418. //获取版本号
  419. [[ELToothbrushBleManager shareManager] getBluetoothInfoWithELInetGetCmdType:(ELInetGetCmdTypeGetBMVersion)];
  420. //获取电量
  421. [[ELToothbrushBleManager shareManager] getBluetoothInfoWithELInetGetCmdType:(ELInetGetCmdTypeGetBatteryState)];
  422. //获取蓝牙和wifi连接状态
  423. [[ELToothbrushBleManager shareManager] getBluetoothInfoWithELInetGetCmdType:(ELInetGetCmdTypeGetConnectState)];
  424. //离线历史记录-步骤1:获取条数
  425. [[ELToothbrushBleManager shareManager] getOfflineDataCount];
  426. }
  427. }
  428. #pragma mark ============ ELBluetoothManagerDelegate ==============
  429. //离线历史记录-步骤2:返回条数
  430. - (void)toothbrushtReceiveToothbrushOfflineDataCount:(unsigned char)count result:(unsigned char)result {
  431. [self addLog:[NSString stringWithFormat:@"离线历史记录:%d条,结果:%d", count, result]];
  432. BOOL success = (result == 0);
  433. if (success && count > 0) {
  434. //离线历史记录-步骤3:请求接收
  435. [self addLog:@"请求接收离线历史记录"];
  436. [[ELToothbrushBleManager shareManager] getOfflineDataStart];
  437. }
  438. }
  439. //离线历史记录-步骤4:上报给APP
  440. - (void)toothbrushtReceiveToothbrushOfflineData:(ToothbrushOfflineData)data {
  441. //离线历史记录-步骤5:通知模块发下一组数据
  442. [self addLog:@"app通知模块可以发送下一组离线历史记录数据"];
  443. [[ELToothbrushBleManager shareManager] getOfflineNextRecord];
  444. }
  445. //离线历史记录-步骤6:发送过程
  446. - (void)toothbrushReceiveRecordDataStatus:(enum ToothbrushOfflineRecrodStatusType)status {
  447. switch (status) {
  448. case ToothbrushOfflineRecrodStatusType_DataCompleted_00: {
  449. [self addLog:@"离线历史记录数据全部发送完成"];
  450. [[ELToothbrushBleManager shareManager] getOfflineDataCancel];
  451. [[ELToothbrushBleManager shareManager] getOfflineDataClear];
  452. }
  453. break;
  454. case ToothbrushOfflineRecrodStatusType_DataAbnormal_01: {
  455. [self addLog:@"离线历史记录数据发送异常"];
  456. [[ELToothbrushBleManager shareManager] getOfflineDataCancel];
  457. [[ELToothbrushBleManager shareManager] getOfflineDataClear];
  458. }
  459. break;
  460. case ToothbrushOfflineRecrodStatusType_SendNextData_02:
  461. break;
  462. default:
  463. break;
  464. }
  465. }
  466. //
  467. -(void)bluetoothManagerReceiveDeviceAuthorizeResult:(ELBluetoothDeviceAuthorizeResult)result{
  468. if (result == ELBluetoothDeviceAuthorizeResultNoAuth) {
  469. [self addLog:@"没有授权,请授权"];
  470. }else if (result == ELBluetoothDeviceAuthorizeResultAuthorized){
  471. [self addLog:@"已经授权"];
  472. }else if (result == ELBluetoothDeviceAuthorizeResultNoNeed){
  473. [self addLog:@"不需要授权"];
  474. }else if (result == ELBluetoothDeviceAuthorizeResultSuccess){
  475. [self addLog:@"授权成功"];
  476. }
  477. }
  478. //蓝牙和wifi连接状态
  479. -(void)bluetoothManagerReceiveMCUConnectedState:(struct ELMCUStateStruct)stateStrct{
  480. [self addLog:[NSString stringWithFormat:@"wifi连接状态:%lu \n0:没有连接\n1:尝试连接热点,但是失败\n2:连接热点,热点无网络\n3:连接热点,热点有网络\n4:有热点信息,未连接",(unsigned long)stateStrct.wifiState]];
  481. }
  482. //电量
  483. -(void)bluetoothManagerReceiveBattery:(struct ELBatteryStruct)battery{
  484. [self addLog:[NSString stringWithFormat:@"设备电量为:%d",battery.power]];
  485. }
  486. //版本号
  487. -(void)bluetoothManagerReceiveBMVersion:(NSString *)bmVersion{
  488. [self addLog:[NSString stringWithFormat:@"BM版本号为:%@",bmVersion]];
  489. }
  490. #pragma mark ============ ToothbrushDelegate ==============
  491. //试用
  492. -(void)toothbrushReceiveTryResult:(ToothbrushSetResult)result{
  493. [self addLog:[NSString stringWithFormat:@"试用结果:%lu",result]];
  494. }
  495. //设置二档默认
  496. -(void)toothbrushReceiveSettingTwoGearDefaultResult:(ELSetBluetoothResponseType)result{
  497. [self addLog:[NSString stringWithFormat:@"设置二档默认结果:%lu",result]];
  498. }
  499. //开关
  500. -(void)toothbrushReceiveSwitchResult:(ToothbrushSetResult)result{
  501. [self addLog:[NSString stringWithFormat:@"开关结果:%lu",result]];
  502. }
  503. //设置刷牙模式结果
  504. -(void)toothbrushReceiveSetWorkGearResult:(ELSetBluetoothResponseType)result{
  505. [self addLog:[NSString stringWithFormat:@"设置刷牙模式结果:%lu",(unsigned long)result]];
  506. }
  507. //刷牙数据
  508. -(void)toothbrushtReceiveToothbrushData:(ToothbrushData)data{
  509. [self addLog:[NSString stringWithFormat:@"刷牙数据:档位=%lu,工作时长=%d,左时长=%d,右时长=%d,电量=%d",(unsigned long)data.gear,data.workTime,data.leftTime,data.rightTime,data.battery]];
  510. }
  511. //上报工作阶段
  512. -(void)toothbrushReceiveWorkPhase:(ToothBrushWorkPhase)workPhase{
  513. [self addLog:[NSString stringWithFormat:@"当前工作阶段:%lu,档位:%lu, 第%lu档",workPhase.workPhase,workPhase.gear,workPhase.supportType]];
  514. }
  515. //获取二档档位信息
  516. -(void)toothbrushReceiveTwoGearDefault:(ToothbrushGearType)gear{
  517. [self addLog:[NSString stringWithFormat:@"二档默认档:%lu",gear]];
  518. }
  519. //手动设置信息
  520. -(void)toothbrushReceiveCustomData:(ToothbrushCustomData)custom{
  521. [self addLog:[NSString stringWithFormat:@"频率:%d 占空比:%d 时长:%d",custom.frequency,custom.duty,custom.interval]];
  522. }
  523. //默认档位和时长
  524. -(void)toothbrushReceiveWorkGear:(ToothbrushWorkGear)workGear{
  525. [self addLog:[NSString stringWithFormat:@"档位:%lu 时长:%d",workGear.gearType,workGear.interval]];
  526. }
  527. //三轴数据
  528. -(void)toothbrushReceiveTriaxialData:(BOOL)success triaxialX:(int)x triaxialY:(int)y triaxialZ:(int)z{
  529. if (success) {
  530. [self addLog:[NSString stringWithFormat:@"三轴数据:x = %d , y = %d , = %d",x,y,z]];
  531. }else{
  532. [self addLog:@"三轴数据获取失败"];
  533. }
  534. }
  535. //三轴方向
  536. -(void)toothbrushReceiveTriaxialDirection:(ToothbrushTriaxialDirection)direction{
  537. if (direction == ToothbrushTriaxialDirectionYTop_01) {
  538. [self addLog:@"Y轴朝上"];
  539. }else if (direction == ToothbrushTriaxialDirectionYBottom_02){
  540. [self addLog:@"Y轴朝下"];
  541. }else if (direction == ToothbrushTriaxialDirectionYLeft_03){
  542. [self addLog:@"Y轴朝左"];
  543. }else if (direction == ToothbrushTriaxialDirectionYRight_04){
  544. [self addLog:@"Y轴朝右"];
  545. }
  546. }
  547. -(void)toothbrushReceiveOneGear:(NSArray<NSNumber *> *)oneGear twoGear:(NSArray<NSNumber *> *)twoGear{
  548. NSString *onegearStr = @"一档:";
  549. for (NSNumber *one in oneGear) {
  550. onegearStr = [NSString stringWithFormat:@"%@,%@",onegearStr,one];
  551. }
  552. NSString *twogearStr = @"二档:";
  553. for (NSNumber *two in twoGear) {
  554. twogearStr = [NSString stringWithFormat:@"%@,%@",twogearStr,two];
  555. }
  556. [self addLog:[NSString stringWithFormat:@"%@\n%@",onegearStr,twogearStr]];
  557. }
  558. //MCU response Prevent Splashing mode 防飞溅
  559. - (void)toothbrushtReceivePreventSplashing:(Byte)support on:(Byte)on {
  560. if (support == 0) {
  561. [self addLog:@"not support Prevent Splashing"];
  562. return;
  563. }
  564. [self addLog:[NSString stringWithFormat:@"Prevent Splashing: %@", on == 0 ? @"Off" : @"On"]];
  565. }
  566. -(void)toothbrushReceiveDevices:(NSArray<ELPeripheralModel *> *)devices{
  567. ELPeripheralModel * connectModel;
  568. for (ELPeripheralModel *deviceModel in devices) {
  569. if ([deviceModel.macAddress isEqualToString:self.p.macAddress]) {
  570. connectModel = deviceModel;
  571. break;
  572. }
  573. }
  574. if (connectModel) {
  575. [[ELToothbrushBleManager shareManager] connectPeripheral:connectModel];
  576. }
  577. }
  578. -(void)dealloc{
  579. [[ELToothbrushBleManager shareManager] disconnectPeripheral];
  580. }
  581. @end