iOS AILinkBleSDK - 蓝牙SDK
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

ToothbrushConnectionViewController.m 25KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595
  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. }
  124. else{
  125. }
  126. }
  127. -(void)setupUI{
  128. self.view.backgroundColor = [UIColor whiteColor];
  129. UIButton *button1 = [self getButtonWithTitle:@"配网" withTag:1];
  130. [self.view addSubview:button1];
  131. [button1 mas_makeConstraints:^(MASConstraintMaker *make) {
  132. make.left.mas_equalTo(10);
  133. make.top.mas_equalTo(70);
  134. make.width.mas_equalTo(70);
  135. make.height.mas_equalTo(30);
  136. }];
  137. UIButton *button2 = [self getButtonWithTitle:@"配对" withTag:2];
  138. [self.view addSubview:button2];
  139. [button2 mas_makeConstraints:^(MASConstraintMaker *make) {
  140. make.left.equalTo(button1.mas_right).mas_offset(5);
  141. make.top.equalTo(button1);
  142. make.width.mas_equalTo(70);
  143. make.height.mas_equalTo(30);
  144. }];
  145. UIButton *button3 = [self getButtonWithTitle:@"获取设备ID" withTag:3];
  146. [self.view addSubview:button3];
  147. [button3 mas_makeConstraints:^(MASConstraintMaker *make) {
  148. make.left.equalTo(button2.mas_right).mas_offset(5);
  149. make.top.equalTo(button2);
  150. make.width.mas_equalTo(70);
  151. make.height.mas_equalTo(30);
  152. }];
  153. UIButton *button4 = [self getButtonWithTitle:@"获取档位" withTag:4];
  154. [self.view addSubview:button4];
  155. [button4 mas_makeConstraints:^(MASConstraintMaker *make) {
  156. make.left.equalTo(button3.mas_right).mas_offset(5);
  157. make.top.equalTo(button3);
  158. make.width.mas_equalTo(70);
  159. make.height.mas_equalTo(30);
  160. }];
  161. UIButton *button5 = [self getButtonWithTitle:@"获取三轴方向" withTag:5];
  162. [self.view addSubview:button5];
  163. [button5 mas_makeConstraints:^(MASConstraintMaker *make) {
  164. make.left.mas_equalTo(10);
  165. make.top.equalTo(button4.mas_bottom).mas_offset(10);
  166. make.width.mas_equalTo(70);
  167. make.height.mas_equalTo(30);
  168. }];
  169. UIButton *button6 = [self getButtonWithTitle:@"获取三轴数据" withTag:6];
  170. [self.view addSubview:button6];
  171. [button6 mas_makeConstraints:^(MASConstraintMaker *make) {
  172. make.left.equalTo(button5.mas_right).mas_offset(5);
  173. make.top.equalTo(button5);
  174. make.width.mas_equalTo(70);
  175. make.height.mas_equalTo(30);
  176. }];
  177. UIButton *button7 = [self getButtonWithTitle:@"查询默认模式和时长" withTag:7];
  178. [self.view addSubview:button7];
  179. [button7 mas_makeConstraints:^(MASConstraintMaker *make) {
  180. make.left.equalTo(button6.mas_right).mas_offset(5);
  181. make.top.equalTo(button6);
  182. make.width.mas_equalTo(70);
  183. make.height.mas_equalTo(30);
  184. }];
  185. UIButton *button8 = [self getButtonWithTitle:@"查询手动设置" withTag:8];
  186. [self.view addSubview:button8];
  187. [button8 mas_makeConstraints:^(MASConstraintMaker *make) {
  188. make.left.equalTo(button7.mas_right).mas_offset(5);
  189. make.top.equalTo(button7);
  190. make.width.mas_equalTo(70);
  191. make.height.mas_equalTo(30);
  192. }];
  193. UIButton *button9 = [self getButtonWithTitle:@"查询二级档位默认" withTag:9];
  194. [self.view addSubview:button9];
  195. [button9 mas_makeConstraints:^(MASConstraintMaker *make) {
  196. make.left.equalTo(button5);
  197. make.top.equalTo(button5.mas_bottom).mas_offset(10);
  198. make.width.mas_equalTo(70);
  199. make.height.mas_equalTo(30);
  200. }];
  201. UIButton *button10 = [self getButtonWithTitle:@"上报工作阶段" withTag:10];
  202. [self.view addSubview:button10];
  203. [button10 mas_makeConstraints:^(MASConstraintMaker *make) {
  204. make.left.equalTo(button9.mas_right).mas_offset(5);
  205. make.top.equalTo(button9);
  206. make.width.mas_equalTo(70);
  207. make.height.mas_equalTo(30);
  208. }];
  209. UIButton *button11 = [self getButtonWithTitle:@"设置刷牙模式(0A)" withTag:11];
  210. [self.view addSubview:button11];
  211. [button11 mas_makeConstraints:^(MASConstraintMaker *make) {
  212. make.left.equalTo(button10.mas_right).mas_offset(5);
  213. make.top.equalTo(button9);
  214. make.width.mas_equalTo(70);
  215. make.height.mas_equalTo(30);
  216. }];
  217. UIButton *button12 = [self getButtonWithTitle:@"开关" withTag:12];
  218. [self.view addSubview:button12];
  219. [button12 mas_makeConstraints:^(MASConstraintMaker *make) {
  220. make.left.equalTo(button11.mas_right).mas_offset(5);
  221. make.top.equalTo(button9);
  222. make.width.mas_equalTo(70);
  223. make.height.mas_equalTo(30);
  224. }];
  225. UIButton *button13 = [self getButtonWithTitle:@"设置二档默认" withTag:13];
  226. [self.view addSubview:button13];
  227. [button13 mas_makeConstraints:^(MASConstraintMaker *make) {
  228. make.left.equalTo(button9);
  229. make.top.equalTo(button9.mas_bottom).mas_offset(10);
  230. make.width.mas_equalTo(70);
  231. make.height.mas_equalTo(30);
  232. }];
  233. UIButton *button14 = [self getButtonWithTitle:@"试用" withTag:14];
  234. [self.view addSubview:button14];
  235. [button14 mas_makeConstraints:^(MASConstraintMaker *make) {
  236. make.left.equalTo(button13.mas_right).mas_offset(5);
  237. make.top.equalTo(button13);
  238. make.width.mas_equalTo(70);
  239. make.height.mas_equalTo(30);
  240. }];
  241. UIButton *button15 = [self getButtonWithTitle:@"设置手动档" withTag:15];
  242. [self.view addSubview:button15];
  243. [button15 mas_makeConstraints:^(MASConstraintMaker *make) {
  244. make.left.equalTo(button14.mas_right).mas_offset(5);
  245. make.top.equalTo(button13);
  246. make.width.mas_equalTo(70);
  247. make.height.mas_equalTo(30);
  248. }];
  249. UIButton *button16 = [self getButtonWithTitle:@"上报接收数据结果" withTag:16];
  250. [self.view addSubview:button16];
  251. [button16 mas_makeConstraints:^(MASConstraintMaker *make) {
  252. make.left.equalTo(button15.mas_right).mas_offset(5);
  253. make.top.equalTo(button13);
  254. make.width.mas_equalTo(70);
  255. make.height.mas_equalTo(30);
  256. }];
  257. //
  258. self.textView = [[UITextView alloc] init];
  259. self.textView.backgroundColor = [UIColor blackColor];
  260. self.textView.text = @"Log";
  261. self.textView.textColor = [UIColor redColor];
  262. self.textView.font = [UIFont systemFontOfSize:16];
  263. self.textView.editable = NO;
  264. [self.view addSubview:self.textView];
  265. [self.textView mas_makeConstraints:^(MASConstraintMaker *make) {
  266. make.left.mas_equalTo(10);
  267. make.right.bottom.mas_equalTo(-10);
  268. make.height.mas_equalTo(200);
  269. }];
  270. }
  271. -(UIButton *)getButtonWithTitle:(NSString *)title withTag:(NSInteger)tag{
  272. UIButton *button1 = [UIButton buttonWithType:(UIButtonTypeCustom)];
  273. [button1 setTitle:title forState:(UIControlStateNormal)];
  274. button1.tag = tag;
  275. button1.titleLabel.adjustsFontSizeToFitWidth = YES;
  276. button1.titleLabel.numberOfLines = 2;
  277. button1.titleLabel.font = [UIFont systemFontOfSize:15];
  278. [button1 setTitleColor:[UIColor redColor] forState:(UIControlStateNormal)];
  279. button1.backgroundColor = [UIColor blackColor];
  280. button1.titleLabel.textAlignment = NSTextAlignmentCenter;
  281. button1.titleLabel.adjustsFontSizeToFitWidth = YES;
  282. [button1 addTarget:self action:@selector(buttonAction:) forControlEvents:(UIControlEventTouchUpInside)];
  283. return button1;
  284. }
  285. -(void)connectWiFiWithPwd:(NSString *)pwd wifi:(ELBleWifiDetailModel *)wifi{
  286. [self addLog:@"正在配置WiFi名称"];
  287. self.wifiPwd = pwd;
  288. [[ELToothbrushBleManager shareManager] bleWifiSetConnectWifiMac:wifi.macData];
  289. }
  290. -(void)connectWiFi:(ELBleWifiDetailModel *)wifi{
  291. if (wifi.secureType ==ELBleWifiSecureTypeOpen) {
  292. [self connectWiFiWithPwd:@"" wifi:wifi];
  293. }else{
  294. ELInputAlertView *input = [[ELInputAlertView alloc] initWithTittle:@"配置WiFi" withText:@"" withMessage:@"请输入WiFi密码" withLeftButton:@"取消" withRightButton:@"确定"];
  295. [input show];
  296. __weak typeof(self) weakSelf = self;
  297. input.rightHideBlock = ^BOOL(NSString * _Nonnull text, UIView * _Nonnull view) {
  298. if ([ELBlePublicTool legalBleWifiPassword:text]) {
  299. [weakSelf connectWiFiWithPwd:text wifi:wifi];
  300. return YES;
  301. }else{
  302. [self addLog:@"请输入8~56位的密码"];
  303. return NO;
  304. }
  305. };
  306. }
  307. }
  308. #pragma mark ============ ElBleWifiDelegate ==============
  309. //获取设备ID
  310. -(void)bleWifiReceiveWifiSNCode:(int)code{
  311. if (code == 0) {
  312. [self addLog:@"设备ID不存在,请先配网再获取"];
  313. }else{
  314. [self addLog:[NSString stringWithFormat:@"设备ID = %d",code]];
  315. }
  316. }
  317. //设置wifi过程结果
  318. -(void)bleWifiReceiceResponseType:(ELInetSetCmdType)type result:(ELSetBluetoothResponseType)result{
  319. if (result == ELSetBluetoothResponseTypeSuccess) {
  320. //设置WiFi结果
  321. if (type == ELInetSetCmdTypeBleWifiSetWifiMac) {
  322. [self addLog:@"正在配置WiFi密码"];
  323. [[ELToothbrushBleManager shareManager] bleWifiSetWifiPwd:self.wifiPwd];
  324. }else if (type == ELInetSetCmdTypeBleWifiSetWifiPwd){
  325. [self addLog:@"设置WiFi连接"];
  326. [[ELToothbrushBleManager shareManager] bleWifiSetupWifiConnect:YES];
  327. }else if (type == ELInetSetCmdTypeBleWifiSetConnectWifi){
  328. //获取连接状态
  329. }
  330. }
  331. else{
  332. [self addLog:@"WiFi配置失败"];
  333. }
  334. }
  335. //附近wifi
  336. -(void)bleWifiReceiveWifiDetailModel:(ELBleWifiDetailModel *)model{
  337. [self addLog:[NSString stringWithFormat:@"搜索到wifi类型---%lu 名称---%@",(unsigned long)model.wifiState,model.wifiName]];
  338. if (model.wifiState == ELBleWifiUseStateConnected) {
  339. }
  340. [self.wifiArr addObject:model];
  341. }
  342. //附近wifi搜索结果
  343. -(void)bleWifiReceiveScanedWifiCount:(int)count {
  344. [self addLog:@"搜索完成"];
  345. //扫描结束
  346. NSMutableArray *array = [[NSMutableArray alloc] init];
  347. for (int i=0; i<self.wifiArr.count; i++) {
  348. ELBleWifiDetailModel *model = self.wifiArr[i];
  349. NSString *wifiTypeStr = [NSString stringWithFormat:@"wifi类型:%lu 名称:%@",(unsigned long)model.secureType,model.wifiName];
  350. if (model.secureType == ELBleWifiSecureTypeOpen) {
  351. wifiTypeStr = [NSString stringWithFormat:@"wifi类型:%@ 名称:%@",@"Open",model.wifiName];
  352. }else if (model.secureType == ELBleWifiSecureTypeWEP){
  353. wifiTypeStr = [NSString stringWithFormat:@"wifi类型:%@ 名称:%@",@"WEP",model.wifiName];
  354. }else if (model.secureType == ELBleWifiSecureTypeWPA_PSK){
  355. wifiTypeStr = [NSString stringWithFormat:@"wifi类型:%@ 名称:%@",@"WPA_PSK",model.wifiName];
  356. }else if (model.secureType == ELBleWifiSecureTypeWPA2_PSK){
  357. wifiTypeStr = [NSString stringWithFormat:@"wifi类型:%@ 名称:%@",@"WPA2_PSK",model.wifiName];
  358. }else if (model.secureType == ELBleWifiSecureTypeWPA_WPA_2_PSK){
  359. wifiTypeStr = [NSString stringWithFormat:@"wifi类型:%@ 名称:%@",@"WPA_WPA_2_PSK",model.wifiName];
  360. }else if (model.secureType == ELBleWifiSecureTypeWPA2_ENTERPRISE){
  361. wifiTypeStr = [NSString stringWithFormat:@"wifi类型:%@ 名称:%@",@"WPA2_ENTERPRISE",model.wifiName];
  362. }
  363. [array addObject:wifiTypeStr];
  364. }
  365. ELSelectView *selectView = [[ELSelectView alloc] initWithTitle:@"附近WiFi" withSelectArray:array];
  366. [selectView show];
  367. __weak typeof(self) weakSelf = self;
  368. selectView.selectRowBlock = ^(NSInteger row) {
  369. [weakSelf connectWiFi:weakSelf.wifiArr[row]];
  370. };
  371. }
  372. //连接状态
  373. -(void)toothbrushReceiveState:(ELBluetoothState)state{
  374. if (state == ELBluetoothStateUnavailable) {
  375. self.title = @"请打开蓝牙";
  376. }else if (state == ELBluetoothStateScaning || state == ELBluetoothStateWillConnect || state == ELBluetoothStateDidConnect){
  377. self.title = @"正在连接...";
  378. }else if (state == ELBluetoothStateConnectFail || state == ELBluetoothStateDidDisconnect || state == ELBluetoothStateFailedValidation){
  379. self.title = @"连接断开";
  380. }else if (state == ELBluetoothStateDidValidationPass){
  381. self.title = @"连接成功";
  382. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  383. self.title = self.p.macAddress;
  384. });
  385. //获取版本号
  386. [[ELToothbrushBleManager shareManager] getBluetoothInfoWithELInetGetCmdType:(ELInetGetCmdTypeGetBMVersion)];
  387. //获取电量
  388. [[ELToothbrushBleManager shareManager] getBluetoothInfoWithELInetGetCmdType:(ELInetGetCmdTypeGetBatteryState)];
  389. //获取蓝牙和wifi连接状态
  390. [[ELToothbrushBleManager shareManager] getBluetoothInfoWithELInetGetCmdType:(ELInetGetCmdTypeGetConnectState)];
  391. //离线历史记录-步骤1:获取条数
  392. [[ELToothbrushBleManager shareManager] getOfflineDataCount];
  393. }
  394. }
  395. #pragma mark ============ ELBluetoothManagerDelegate ==============
  396. //离线历史记录-步骤2:返回条数
  397. - (void)toothbrushtReceiveToothbrushOfflineDataCount:(unsigned char)count result:(unsigned char)result {
  398. [self addLog:[NSString stringWithFormat:@"离线历史记录:%d条,结果:%d", count, result]];
  399. BOOL success = (result == 0);
  400. if (success && count > 0) {
  401. //离线历史记录-步骤3:请求接收
  402. [self addLog:@"请求接收离线历史记录"];
  403. [[ELToothbrushBleManager shareManager] getOfflineDataStart];
  404. }
  405. }
  406. //离线历史记录-步骤4:上报给APP
  407. - (void)toothbrushtReceiveToothbrushOfflineData:(ToothbrushOfflineData)data {
  408. //离线历史记录-步骤5:通知模块发下一组数据
  409. [self addLog:@"app通知模块可以发送下一组离线历史记录数据"];
  410. [[ELToothbrushBleManager shareManager] getOfflineNextRecord];
  411. }
  412. //离线历史记录-步骤6:发送过程
  413. - (void)toothbrushReceiveRecordDataStatus:(enum ToothbrushOfflineRecrodStatusType)status {
  414. switch (status) {
  415. case ToothbrushOfflineRecrodStatusType_DataCompleted_00: {
  416. [self addLog:@"离线历史记录数据全部发送完成"];
  417. [[ELToothbrushBleManager shareManager] getOfflineDataCancel];
  418. [[ELToothbrushBleManager shareManager] getOfflineDataClear];
  419. }
  420. break;
  421. case ToothbrushOfflineRecrodStatusType_DataAbnormal_01: {
  422. [self addLog:@"离线历史记录数据发送异常"];
  423. [[ELToothbrushBleManager shareManager] getOfflineDataCancel];
  424. [[ELToothbrushBleManager shareManager] getOfflineDataClear];
  425. }
  426. break;
  427. case ToothbrushOfflineRecrodStatusType_SendNextData_02:
  428. break;
  429. default:
  430. break;
  431. }
  432. }
  433. //
  434. -(void)bluetoothManagerReceiveDeviceAuthorizeResult:(ELBluetoothDeviceAuthorizeResult)result{
  435. if (result == ELBluetoothDeviceAuthorizeResultNoAuth) {
  436. [self addLog:@"没有授权,请授权"];
  437. }else if (result == ELBluetoothDeviceAuthorizeResultAuthorized){
  438. [self addLog:@"已经授权"];
  439. }else if (result == ELBluetoothDeviceAuthorizeResultNoNeed){
  440. [self addLog:@"不需要授权"];
  441. }else if (result == ELBluetoothDeviceAuthorizeResultSuccess){
  442. [self addLog:@"授权成功"];
  443. }
  444. }
  445. //蓝牙和wifi连接状态
  446. -(void)bluetoothManagerReceiveMCUConnectedState:(struct ELMCUStateStruct)stateStrct{
  447. [self addLog:[NSString stringWithFormat:@"wifi连接状态:%lu \n0:没有连接\n1:尝试连接热点,但是失败\n2:连接热点,热点无网络\n3:连接热点,热点有网络\n4:有热点信息,未连接",(unsigned long)stateStrct.wifiState]];
  448. }
  449. //电量
  450. -(void)bluetoothManagerReceiveBattery:(struct ELBatteryStruct)battery{
  451. [self addLog:[NSString stringWithFormat:@"设备电量为:%d",battery.power]];
  452. }
  453. //版本号
  454. -(void)bluetoothManagerReceiveBMVersion:(NSString *)bmVersion{
  455. [self addLog:[NSString stringWithFormat:@"BM版本号为:%@",bmVersion]];
  456. }
  457. #pragma mark ============ ToothbrushDelegate ==============
  458. //试用
  459. -(void)toothbrushReceiveTryResult:(ToothbrushSetResult)result{
  460. [self addLog:[NSString stringWithFormat:@"试用结果:%lu",result]];
  461. }
  462. //设置二档默认
  463. -(void)toothbrushReceiveSettingTwoGearDefaultResult:(ELSetBluetoothResponseType)result{
  464. [self addLog:[NSString stringWithFormat:@"设置二档默认结果:%lu",result]];
  465. }
  466. //开关
  467. -(void)toothbrushReceiveSwitchResult:(ToothbrushSetResult)result{
  468. [self addLog:[NSString stringWithFormat:@"开关结果:%lu",result]];
  469. }
  470. //设置刷牙模式结果
  471. -(void)toothbrushReceiveSetWorkGearResult:(ELSetBluetoothResponseType)result{
  472. [self addLog:[NSString stringWithFormat:@"设置刷牙模式结果:%lu",(unsigned long)result]];
  473. }
  474. //刷牙数据
  475. -(void)toothbrushtReceiveToothbrushData:(ToothbrushData)data{
  476. [self addLog:[NSString stringWithFormat:@"刷牙数据:档位=%lu,工作时长=%d,左时长=%d,右时长=%d,电量=%d",(unsigned long)data.gear,data.workTime,data.leftTime,data.rightTime,data.battery]];
  477. }
  478. //上报工作阶段
  479. -(void)toothbrushReceiveWorkPhase:(ToothBrushWorkPhase)workPhase{
  480. [self addLog:[NSString stringWithFormat:@"当前工作阶段:%lu,档位:%lu, 第%lu档",workPhase.workPhase,workPhase.gear,workPhase.supportType]];
  481. }
  482. //获取二档档位信息
  483. -(void)toothbrushReceiveTwoGearDefault:(ToothbrushGearType)gear{
  484. [self addLog:[NSString stringWithFormat:@"二档默认档:%lu",gear]];
  485. }
  486. //手动设置信息
  487. -(void)toothbrushReceiveCustomData:(ToothbrushCustomData)custom{
  488. [self addLog:[NSString stringWithFormat:@"频率:%d 占空比:%d 时长:%d",custom.frequency,custom.duty,custom.interval]];
  489. }
  490. //默认档位和时长
  491. -(void)toothbrushReceiveWorkGear:(ToothbrushWorkGear)workGear{
  492. [self addLog:[NSString stringWithFormat:@"档位:%lu 时长:%d",workGear.gearType,workGear.interval]];
  493. }
  494. //三轴数据
  495. -(void)toothbrushReceiveTriaxialData:(BOOL)success triaxialX:(int)x triaxialY:(int)y triaxialZ:(int)z{
  496. if (success) {
  497. [self addLog:[NSString stringWithFormat:@"三轴数据:x = %d , y = %d , = %d",x,y,z]];
  498. }else{
  499. [self addLog:@"三轴数据获取失败"];
  500. }
  501. }
  502. //三轴方向
  503. -(void)toothbrushReceiveTriaxialDirection:(ToothbrushTriaxialDirection)direction{
  504. if (direction == ToothbrushTriaxialDirectionYTop_01) {
  505. [self addLog:@"Y轴朝上"];
  506. }else if (direction == ToothbrushTriaxialDirectionYBottom_02){
  507. [self addLog:@"Y轴朝下"];
  508. }else if (direction == ToothbrushTriaxialDirectionYLeft_03){
  509. [self addLog:@"Y轴朝左"];
  510. }else if (direction == ToothbrushTriaxialDirectionYRight_04){
  511. [self addLog:@"Y轴朝右"];
  512. }
  513. }
  514. -(void)toothbrushReceiveOneGear:(NSArray<NSNumber *> *)oneGear twoGear:(NSArray<NSNumber *> *)twoGear{
  515. NSString *onegearStr = @"一档:";
  516. for (NSNumber *one in oneGear) {
  517. onegearStr = [NSString stringWithFormat:@"%@,%@",onegearStr,one];
  518. }
  519. NSString *twogearStr = @"二档:";
  520. for (NSNumber *two in twoGear) {
  521. twogearStr = [NSString stringWithFormat:@"%@,%@",twogearStr,two];
  522. }
  523. [self addLog:[NSString stringWithFormat:@"%@\n%@",onegearStr,twogearStr]];
  524. }
  525. -(void)toothbrushReceiveDevices:(NSArray<ELPeripheralModel *> *)devices{
  526. ELPeripheralModel * connectModel;
  527. for (ELPeripheralModel *deviceModel in devices) {
  528. if ([deviceModel.macAddress isEqualToString:self.p.macAddress]) {
  529. connectModel = deviceModel;
  530. break;
  531. }
  532. }
  533. if (connectModel) {
  534. [[ELToothbrushBleManager shareManager] connectPeripheral:connectModel];
  535. }
  536. }
  537. -(void)dealloc{
  538. [[ELToothbrushBleManager shareManager] disconnectPeripheral];
  539. }
  540. @end