iOS AILinkBleSDK - 蓝牙SDK
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

ToothbrushConnectionViewController.m 23KB

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