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

ELBfsWifiConnectVC.m 27KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734
  1. //
  2. // BodyFatScaleConnectViewController.m
  3. // AILinkBleSDKSourceCode
  4. //
  5. // Created by iot_user on 2020/4/8.
  6. // Copyright © 2020 IOT. All rights reserved.
  7. //
  8. #import "ELBfsWifiConnectVC.h"
  9. #import "Masonry.h"
  10. #import <AILinkBleSDK/ELBodyFatScaleBleManager.h>
  11. #import <AILinkBleSDK/ELBodyFatScaleBleUserModel.h>
  12. #import <AILinkBleSDK/ELBodyFatScaleBleWeightModel.h>
  13. #import "ELBfsWifiConnectTableViewCell.h"
  14. #import <AILinkBleSDK/ELBluetoothManager+Settings.h>
  15. #import <AILinkBleSDK/ELBleWifiDetailModel.h>
  16. #import <AILinkBleSDK/ELBluetoothManager+BleWifi.h>
  17. #import <AILinkBleSDK/OEMAlgorithmSDK.h>
  18. #import <AILinkBleSDK/ELUnitConvertTool.h>
  19. @interface ELBfsWifiConnectVC ()<ELBluetoothManagerDelegate,ELBodyFatScaleBleDelegate,ElBleWifiDelegate,UITextFieldDelegate,UITableViewDelegate,UITableViewDataSource>
  20. @property (nonatomic, strong) UITableView *tableView;
  21. @property (nonatomic, strong) NSMutableArray<ELBleWifiDetailModel *> *dataSource;
  22. @property (nonatomic, strong) UITextView *textView;
  23. @property (nonatomic, strong) UIButton *changeIpBtn;
  24. @end
  25. @implementation ELBfsWifiConnectVC
  26. - (void)viewDidLoad {
  27. [super viewDidLoad];
  28. self.view.backgroundColor = [UIColor whiteColor];
  29. [ELBodyFatScaleBleManager shareManager].bodyFatScaleDelegate = self;
  30. [ELBodyFatScaleBleManager shareManager].delegate = self;
  31. [[ELBodyFatScaleBleManager shareManager] connectPeripheral:self.p];
  32. [ELBodyFatScaleBleManager shareManager].bleWifiDelegate = self;
  33. self.dataSource = [NSMutableArray array];
  34. [self setupUIView];
  35. }
  36. -(void)viewWillDisappear:(BOOL)animated{
  37. [super viewWillDisappear:animated];
  38. [[ELBodyFatScaleBleManager shareManager] disconnectPeripheral];
  39. }
  40. -(void)addLog:(NSString *)log{
  41. self.textView.text = [NSString stringWithFormat:@"%@\n%@",log,self.textView.text];
  42. }
  43. -(void)buttonAction:(UIButton *)sender{
  44. if ([ELBodyFatScaleBleManager shareManager].state != ELBluetoothStateDidValidationPass) {
  45. [self addLog:@"Disconnected"];
  46. return;
  47. }
  48. switch (sender.tag) {
  49. case 1: {
  50. //获取BM Version
  51. [[ELBodyFatScaleBleManager shareManager] getBluetoothInfoWithELInetGetCmdType:ELInetGetCmdTypeGetBMVersion];
  52. }
  53. break;
  54. case 2: {
  55. //Get CVP ID
  56. [self addLog:[NSString stringWithFormat:@"Cid:%zd, Vid:%zd, Pid:%zd",self.p.deviceType,self.p.vendorID,self.p.productID]];
  57. }
  58. break;
  59. case 3: {
  60. //Get Name
  61. [self addLog:[NSString stringWithFormat:@"DeviceName:%@",self.p.deviceName]];
  62. }
  63. break;
  64. case 4: {
  65. //Set name
  66. [[ELBodyFatScaleBleManager shareManager] setBluetoothName:@"Ailink_Dev"];
  67. }
  68. break;
  69. case 5: {
  70. //Change unit
  71. if (sender.selected) {
  72. [[ELBodyFatScaleBleManager shareManager] changeBodyFatScaleUnit:ELDeviceWeightUnit_LB];
  73. [self addLog:@"set lb unit"];
  74. } else {
  75. [[ELBodyFatScaleBleManager shareManager] changeBodyFatScaleUnit:ELDeviceWeightUnit_KG];
  76. [self addLog:@"set kg unit"];
  77. }
  78. sender.selected = !sender.selected;
  79. }
  80. break;
  81. default:
  82. break;
  83. }
  84. [self addLog:sender.titleLabel.text];
  85. }
  86. -(void)setupUIView{
  87. // UIButton *button1 = [UIButton buttonWithType:(UIButtonTypeCustom)];
  88. // [button1 setTitle:@"Get BM Version" forState:(UIControlStateNormal)];
  89. // button1.tag = 1;
  90. // button1.titleLabel.adjustsFontSizeToFitWidth = YES;
  91. // button1.titleLabel.numberOfLines = 2;
  92. // button1.backgroundColor = [UIColor blackColor];
  93. // [button1 addTarget:self action:@selector(buttonAction:) forControlEvents:(UIControlEventTouchUpInside)];
  94. // [self.view addSubview:button1];
  95. // [button1 mas_makeConstraints:^(MASConstraintMaker *make) {
  96. // make.left.mas_equalTo(10);
  97. // make.top.mas_equalTo(85);
  98. // make.width.mas_equalTo(100);
  99. // make.height.mas_equalTo(40);
  100. // }];
  101. //
  102. // UIButton *button2 = [UIButton buttonWithType:(UIButtonTypeCustom)];
  103. // [button2 setTitle:@"Get C V P ID" forState:(UIControlStateNormal)];
  104. // button2.tag = 2;
  105. // button2.titleLabel.adjustsFontSizeToFitWidth = YES;
  106. // button2.titleLabel.numberOfLines = 2;
  107. // button2.backgroundColor = [UIColor blackColor];
  108. // [button2 addTarget:self action:@selector(buttonAction:) forControlEvents:(UIControlEventTouchUpInside)];
  109. // [self.view addSubview:button2];
  110. // [button2 mas_makeConstraints:^(MASConstraintMaker *make) {
  111. // make.left.equalTo(button1.mas_right).mas_offset(10);
  112. // make.top.mas_equalTo(85);
  113. // make.width.mas_equalTo(100);
  114. // make.height.mas_equalTo(40);
  115. // }];
  116. //
  117. // UIButton *button3 = [UIButton buttonWithType:(UIButtonTypeCustom)];
  118. // [button3 setTitle:@"Get Name" forState:(UIControlStateNormal)];
  119. // button3.tag = 3;
  120. // button3.titleLabel.adjustsFontSizeToFitWidth = YES;
  121. // button3.titleLabel.numberOfLines = 2;
  122. // button3.backgroundColor = [UIColor blackColor];
  123. // [button3 addTarget:self action:@selector(buttonAction:) forControlEvents:(UIControlEventTouchUpInside)];
  124. // [self.view addSubview:button3];
  125. // [button3 mas_makeConstraints:^(MASConstraintMaker *make) {
  126. // make.left.equalTo(button2.mas_right).mas_offset(10);
  127. // make.top.mas_equalTo(85);
  128. // make.width.mas_equalTo(100);
  129. // make.height.mas_equalTo(40);
  130. // }];
  131. // //
  132. // UIButton *button4 = [UIButton buttonWithType:(UIButtonTypeCustom)];
  133. // [button4 setTitle:@"Set Name" forState:(UIControlStateNormal)];
  134. // button4.tag = 4;
  135. // button4.titleLabel.adjustsFontSizeToFitWidth = YES;
  136. // button4.titleLabel.numberOfLines = 2;
  137. // button4.backgroundColor = [UIColor blackColor];
  138. // [button4 addTarget:self action:@selector(buttonAction:) forControlEvents:(UIControlEventTouchUpInside)];
  139. // [self.view addSubview:button4];
  140. // [button4 mas_makeConstraints:^(MASConstraintMaker *make) {
  141. // make.left.mas_equalTo(10);
  142. // make.top.equalTo(button1.mas_bottom).mas_offset(10);
  143. // make.width.mas_equalTo(100);
  144. // make.height.mas_equalTo(40);
  145. // }];
  146. UIButton *button5 = [UIButton buttonWithType:(UIButtonTypeCustom)];
  147. [button5 setTitle:@"Change unit" forState:(UIControlStateNormal)];
  148. button5.tag = 5;
  149. button5.titleLabel.adjustsFontSizeToFitWidth = YES;
  150. button5.titleLabel.numberOfLines = 2;
  151. button5.backgroundColor = [UIColor blackColor];
  152. [button5 addTarget:self action:@selector(buttonAction:) forControlEvents:(UIControlEventTouchUpInside)];
  153. [self.view addSubview:button5];
  154. [button5 mas_makeConstraints:^(MASConstraintMaker *make) {
  155. // make.left.equalTo(button4.mas_right).mas_offset(10);
  156. // make.top.equalTo(button1.mas_bottom).mas_offset(10);
  157. make.left.offset(10);
  158. make.top.offset(90);
  159. make.width.mas_equalTo(100);
  160. make.height.mas_equalTo(40);
  161. }];
  162. //
  163. self.textView = [[UITextView alloc] init];
  164. self.textView.backgroundColor = [UIColor blackColor];
  165. self.textView.text = @"Log";
  166. self.textView.textColor = [UIColor redColor];
  167. [self.view addSubview:self.textView];
  168. [self.textView mas_makeConstraints:^(MASConstraintMaker *make) {
  169. make.left.mas_equalTo(10);
  170. make.right.bottom.mas_equalTo(-10);
  171. make.height.mas_equalTo(300);
  172. }];
  173. self.changeIpBtn = [[UIButton alloc] init];
  174. [self.changeIpBtn setTitle:@"Switch to the test environment" forState:UIControlStateNormal];
  175. [self.changeIpBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
  176. [self.changeIpBtn setTitle:@"Switch to the formal environment" forState:UIControlStateSelected];
  177. [self.changeIpBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
  178. self.changeIpBtn.adjustsImageWhenHighlighted = YES;
  179. self.changeIpBtn.backgroundColor = [UIColor blackColor];
  180. self.changeIpBtn.titleLabel.font = [UIFont systemFontOfSize:13];
  181. self.changeIpBtn.titleLabel.numberOfLines = 2;
  182. [self.changeIpBtn addTarget:self action:@selector(changeDevIpAddress:) forControlEvents:UIControlEventTouchUpInside];
  183. [self.view addSubview:self.changeIpBtn];
  184. [self.changeIpBtn mas_makeConstraints:^(MASConstraintMaker *make) {
  185. make.left.equalTo(button5.mas_right).mas_offset(10);
  186. make.centerY.equalTo(button5.mas_centerY);
  187. make.width.mas_equalTo(120);
  188. make.height.mas_equalTo(40);
  189. // make.left.offset(20);
  190. // make.top.offset(100);
  191. // make.size.mas_equalTo(CGSizeMake(120, 30));
  192. }];
  193. self.tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
  194. self.tableView.delegate = self;
  195. self.tableView.dataSource = self;
  196. [self.view addSubview:self.tableView];
  197. [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
  198. make.left.right.offset(0);
  199. make.top.equalTo(self.changeIpBtn.mas_bottom).offset(15);
  200. make.bottom.equalTo(self.textView.mas_top).offset(-20);
  201. }];
  202. }
  203. -(void)bodyFatScaleManagerUpdateState:(ELBluetoothState)state{
  204. switch (state) {
  205. case ELBluetoothStateUnavailable:
  206. {
  207. self.title = @"Please open the bluetooth";
  208. }
  209. break;
  210. case ELBluetoothStateAvailable:
  211. {
  212. self.title = @"Bluetooth is open";
  213. }
  214. break;
  215. case ELBluetoothStateScaning:
  216. {
  217. self.title = @"Scaning";
  218. }
  219. break;
  220. case ELBluetoothStateConnectFail:
  221. {
  222. self.title = @"Connect fail";
  223. }
  224. break;
  225. case ELBluetoothStateDidDisconnect:
  226. {
  227. self.title = @"Disconnected";
  228. }
  229. break;
  230. case ELBluetoothStateDidValidationPass:
  231. {
  232. self.title = @"Connected";
  233. if (self.p.deviceType == ELSupportDeviceTypeBLE_WIFIScale) {
  234. //获取蓝牙连接状态
  235. [[ELBodyFatScaleBleManager shareManager] getBluetoothInfoWithELInetGetCmdType:(ELInetGetCmdTypeGetConnectState)];
  236. }
  237. }
  238. break;
  239. case ELBluetoothStateFailedValidation:
  240. {
  241. self.title = @"Illegal equipment";
  242. }
  243. break;
  244. case ELBluetoothStateWillConnect:
  245. self.title = @"Connecting";
  246. break;
  247. default:
  248. break;
  249. }
  250. }
  251. -(void)bluetoothManagerReceiveMCUConnectedState:(struct ELMCUStateStruct)stateStrct{
  252. BleWiFiConnectState wifiState = stateStrct.wifiState;
  253. if (self.p.deviceType == ELSupportDeviceTypeBLE_WIFIScale) { //蓝牙WiFi体脂秤
  254. //请求会话码
  255. [[ELBodyFatScaleBleManager shareManager] getBluetoothInfoWithELInetGetCmdType:ELInetGetCmdTypeBleWifiGetSNNumber];
  256. //MARK:1.WiFi配置提示用户配网
  257. //TODO:注意:wifi配置过程必须保持蓝牙与设备的连接
  258. if (wifiState == BleWiFiConnectStateNoWiFi) {
  259. //1.如果没有配网,则提示用户去配网
  260. //MARK:2.WiFi配置先获取wifi列表
  261. [self.dataSource removeAllObjects];
  262. [self.tableView reloadData];
  263. [[ELBodyFatScaleBleManager shareManager] getBluetoothInfoWithELInetGetCmdType:ELInetGetCmdTypeBleWifiGetNearbyWifi];
  264. } else {
  265. //MARK:2.没网也可以获取Wi-Fi列表,如果需要更新Wi-Fi的话
  266. [self.dataSource removeAllObjects];
  267. [self.tableView reloadData];
  268. [[ELBodyFatScaleBleManager shareManager] getBluetoothInfoWithELInetGetCmdType:ELInetGetCmdTypeBleWifiGetNearbyWifi];
  269. }
  270. }
  271. ///MARK:9.wifi配置结果
  272. //wifi相关的其他指令和数据接收,请查看ELBluetoothManager.h文件
  273. if (stateStrct.wifiState == BleWiFiConnectStateSuccess) {
  274. //成功
  275. //MARK:10.获取wifi设备的SN号,即设备id
  276. [[ELBodyFatScaleBleManager shareManager] getBluetoothInfoWithELInetGetCmdType:(ELInetGetCmdTypeBleWifiGetSNNumber)];
  277. }else{
  278. //失败
  279. }
  280. }
  281. //MARK:回调Wifi列表
  282. -(void)bleWifiReceiveWifiDetailModel:(ELBleWifiDetailModel *)model{
  283. //用一个tableview显示喽
  284. [self addLog:[NSString stringWithFormat:@"wifi type---%lu name---%@ link state--%zd",(unsigned long)model.wifiState,model.wifiName,model.wifiState]];
  285. // MARK:3.WiFi配置得到Wifi列表
  286. if (model.wifiName.length > 0) {
  287. [self.dataSource addObject:model];
  288. }
  289. [self.tableView reloadData];
  290. }
  291. -(void)bleWifiReceiveScanedWifiCount:(int)count {
  292. //MARK:扫描Wifi结束
  293. //MARK:4.WiFi配置得到一个附近wifi的列表,点击选择一个wifi,进行配置
  294. [self.tableView reloadData];
  295. }
  296. -(void)bleWifiReceiceResponseType:(ELInetSetCmdType)type result:(ELSetBluetoothResponseType)result{
  297. [self addLog:[NSString stringWithFormat:@"bleWifiReceiceResponseType==%lu----%lu",type,result]];
  298. if (result == ELSetBluetoothResponseTypeSuccess) {
  299. //设置WiFi结果
  300. if (type == ELInetSetCmdTypeBleWifiSetWifiMac) {
  301. }else if (type == ELInetSetCmdTypeBleWifiSetWifiPwd){
  302. //MARK:8.然后发起连接Wifi
  303. [[ELBodyFatScaleBleManager shareManager] bleWifiSetupWifiConnect:YES];
  304. }else if (type == ELInetSetCmdTypeBleWifiSetConnectWifi){
  305. //获取连接状态,更新Wi-Fi列表
  306. [self.dataSource removeAllObjects];
  307. [self.tableView reloadData];
  308. [[ELBodyFatScaleBleManager shareManager] getBluetoothInfoWithELInetGetCmdType:ELInetGetCmdTypeBleWifiGetNearbyWifi];
  309. }
  310. }
  311. else{
  312. //TODO:Wifi配置过程中失败的情况自行处理
  313. }
  314. }
  315. -(void)bleWifiReceiveWifiSNCode:(int)code{
  316. //MARK:11.根据wifi设备的设备id从服务器获取离线数据(找自己后台要接口)
  317. }
  318. #pragma mark - textField Delegate
  319. - (void)textFieldDidEndEditing:(UITextField *)textField {
  320. NSLog(@"%@",textField.text);
  321. //MARK:7.再发送配置wifi的密码的指令
  322. [[ELBodyFatScaleBleManager shareManager] bleWifiSetWifiPwd:textField.text];
  323. }
  324. #pragma mark - tableView Delegate And DataSource
  325. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  326. return 1;
  327. }
  328. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  329. return self.dataSource.count;
  330. }
  331. - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
  332. return 0.f;
  333. }
  334. - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
  335. return nil;
  336. }
  337. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  338. static NSString *cellId = @"ELBfsWifiConnectTableViewCell";
  339. ELBfsWifiConnectTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
  340. if (!cell) {
  341. cell = [[ELBfsWifiConnectTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
  342. }
  343. ELBleWifiDetailModel *model = self.dataSource[indexPath.row];
  344. cell.wifiName = model.wifiName;
  345. if (model.wifiState == ELBleWifiUseStateConnected) {
  346. cell.isLink = YES;
  347. } else {
  348. cell.isLink = NO;
  349. }
  350. return cell;
  351. }
  352. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  353. [tableView deselectRowAtIndexPath:indexPath animated:YES];
  354. ELBleWifiDetailModel *model = self.dataSource[indexPath.row];
  355. //MARK:5.先发送配置wifi的mac地址的指令
  356. [[ELBodyFatScaleBleManager shareManager] bleWifiSetConnectWifiMac:model.macData];
  357. UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"请输入wifi密码" message:model.wifiName preferredStyle:UIAlertControllerStyleAlert];
  358. UIAlertAction *conform = [UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  359. NSLog(@"点击了确认按钮");
  360. }];
  361. UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
  362. NSLog(@"点击了取消按钮");
  363. }];
  364. //MARK:6.输入wifi密码
  365. [alert addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
  366. textField.placeholder = @"密码";
  367. textField.delegate = self;
  368. }];
  369. [alert addAction:conform];
  370. [alert addAction:cancel];
  371. [self presentViewController:alert animated:YES completion:nil];
  372. }
  373. #pragma mark - 切换秤环境
  374. static NSString *formalAddress = @"ailink.iot.aicare.net.cn";
  375. static NSString *testAddress = @"test.ailink.revice.aicare.net.cn";
  376. //static NSString *pathURL = @"/devivce/serverRedirect/";
  377. static NSInteger portNumber = 80;
  378. - (void)changeDevIpAddress:(UIButton *)sender {
  379. // if ([self.title isEqualToString:@"Connected"]) {
  380. if (sender.selected) {
  381. //切换到生产环境
  382. [[ELBodyFatScaleBleManager shareManager] setDevAccessIpAddress:formalAddress portNumber:portNumber path:@""];
  383. [self addLog:@"Start to configure as a production environment(开始配置为生产环境"];
  384. } else {
  385. //切到测试环境
  386. [[ELBodyFatScaleBleManager shareManager] setDevAccessIpAddress:testAddress portNumber:portNumber path:@""];
  387. // [[ELBodyFatScaleBleManager shareManager] setDevAccessIpAddress:@"ABCD1234567890" path:pathURL];
  388. [self addLog:@"Start to configure as a test environment(开始配置为测试环境"];
  389. }
  390. // sender.selected = !sender.selected;
  391. // } else {
  392. // [self addLog:@"当前设备未连接"];
  393. // }
  394. }
  395. /** 返回ip地址配置结果 */
  396. - (void)bodyFatScaleManagerCallBackSetIPAddressResult:(NSInteger)result {
  397. NSString *resultStr = @"";
  398. if (result == 0) {
  399. //失败
  400. resultStr = @"Environment configuration failed(环境配置失败";
  401. } else if (result == 1) {
  402. //成功
  403. resultStr = @"Environment configuration is successful(环境配置成功";
  404. self.changeIpBtn.selected = !self.changeIpBtn.selected;
  405. } else {
  406. //不支持
  407. resultStr = @"The current device does not support environment configuration(当前设备不支持环境配置";
  408. }
  409. [self addLog:resultStr];
  410. }
  411. #pragma mark -
  412. - (void)dealloc
  413. {
  414. [[ELBodyFatScaleBleManager shareManager] disconnectPeripheral];
  415. }
  416. #pragma mark ============ 如下是蓝牙交互部分,wifi不用管 ==============
  417. - (void)bluetoothManagerReceiveBMVersion:(NSString *)bmVersion {
  418. [self addLog:[NSString stringWithFormat:@"BMVersion :%@",bmVersion]];
  419. }
  420. /**
  421. MCU requests user information( MCU 请求用户信息)
  422. @param status 请求状态
  423. */
  424. - (void)bodyFatScaleManagerMCURequestAppUserInfoType:(BodyFatScaleMCURequestUserInfoStatus)status{
  425. if (status == BodyFatScaleMCURequestUserInfoStatus_Get) {
  426. //下发当前用户给秤
  427. [[ELBodyFatScaleBleManager shareManager] sendCurrentUserToBle:[self getOneUser]];
  428. }else if (status == BodyFatScaleMCURequestUserInfoStatus_Success){
  429. [self addLog:@"MCU requests user information success"];
  430. }else if (BodyFatScaleMCURequestUserInfoStatus_Fail){
  431. [self addLog:@"MCU requests user information failure"];
  432. }
  433. }
  434. /**
  435. Return weight and body fat model(返回重量及体脂model)
  436. */
  437. -(void)bodyFatScaleManagerCallBackWeight:(ELBodyFatScaleBleWeightModel *)weightModel state:(BodyFatScaleMeasureStatus)status{
  438. switch (status) {
  439. case BodyFatScaleMeasureStatus_Unstable:
  440. {
  441. [self addLog:[NSString stringWithFormat:@"Unstable data weight = %ld unit = %@ point = %ld adc = %ld",weightModel.weight,AiLinkBleWeightUnitDic[@(weightModel.weightUnit)],weightModel.weightPoint,weightModel.adc]];
  442. }
  443. break;
  444. case BodyFatScaleMeasureStatus_Stable:
  445. {
  446. [self addLog:[NSString stringWithFormat:@"Stable data weight = %ld unit = %@ point = %ld adc = %ld",weightModel.weight,AiLinkBleWeightUnitDic[@(weightModel.weightUnit)],weightModel.weightPoint,weightModel.adc]];
  447. }
  448. break;
  449. case BodyFatScaleMeasureStatus_Failed:
  450. {
  451. [self addLog:@"Test failure"];
  452. }
  453. break;
  454. case BodyFatScaleMeasureStatus_Complete:
  455. {
  456. [self addLog:[NSString stringWithFormat:@"Tested weight = %ld unit = %@ point = %ld adc = %ld",weightModel.weight,AiLinkBleWeightUnitDic[@(weightModel.weightUnit)],weightModel.weightPoint,weightModel.adc]];
  457. //更新用户信息给秤
  458. [[ELBodyFatScaleBleManager shareManager] sendCurrentUserToBle:[self getOneUser]];
  459. //下发用户列表
  460. [[ELBodyFatScaleBleManager shareManager] sendOfflineUserListToBle:[self get8Users]];
  461. //
  462. ELBodyFatScaleBleUserModel *user = [self getOneUser];
  463. NSString *weightKg = [ELUnitConvertTool getWeightShowStrWithTargetUnit:ELDeviceWeightUnit_KG bleWeightInt:weightModel.weight bleWeightUnit:weightModel.weightUnit bleWeightPoint:weightModel.weightPoint];
  464. OEMAlgorithmModel *algorithmModel = [OEMAlgorithmSDK getBodyfatWithWeight:weightKg.floatValue adc:(int) weightModel.adc sex:(OEMAlgUserSex)user.sex age:(int) user.age height:(int) user.height];
  465. NSLog(@"algorithmModel:%@", algorithmModel);
  466. }
  467. break;
  468. default:
  469. break;
  470. }
  471. }
  472. /**
  473. Return temperature (unit: ° C)(返回温度(单位:°C))
  474. @param temp 温度(单位:°C)
  475. */
  476. -(void)bodyFatScaleManagerCallBackTemp:(NSString *)temp{
  477. [self addLog:[NSString stringWithFormat:@"temperature is %@",temp]];
  478. }
  479. /**
  480. Back to impedance measurement(返回阻抗测量)
  481. @param status 阻抗测量状态
  482. @param adc 阻抗
  483. */
  484. -(void)bodyFatScaleManagerCallBackAdcMeasureStatus:(BodyFatScaleAdcMeasureStatus)status adcValue:(NSInteger)adc{
  485. switch (status) {
  486. case BodyFatScaleAdcMeasureStatus_Testing:
  487. {
  488. [self addLog:@"adc testing"];
  489. }
  490. break;
  491. case BodyFatScaleAdcMeasureStatus_Success:
  492. {
  493. [self addLog:[NSString stringWithFormat:@"adc is %ld",adc]];
  494. }
  495. break;
  496. case BodyFatScaleAdcMeasureStatus_Fail:
  497. {
  498. [self addLog:@"adc test failure"];
  499. }
  500. break;
  501. case BodyFatScaleAdcMeasureStatus_SuccessCustom:
  502. {
  503. [self addLog:[NSString stringWithFormat:@"adc is %ld and user app algum",adc]];
  504. }
  505. break;
  506. default:
  507. break;
  508. }
  509. }
  510. /**
  511. Callback heart rate measurement(回调心率测量)
  512. @param status 心率测量状态
  513. @param heartRate 心率
  514. */
  515. -(void)bodyFatScaleManagerCallBackHeartRateMeasureStatus:(BodyFatScaleHeartRateMeasureStatus)status heartRateValue:(NSInteger)heartRate{
  516. switch (status) {
  517. case BodyFatScaleHeartRateMeasureStatus_Testing:
  518. {
  519. [self addLog:@"Heart Rate Testing"];
  520. }
  521. break;
  522. case BodyFatScaleHeartRateMeasureStatus_Success:
  523. {
  524. [self addLog:[NSString stringWithFormat:@"Heart Rate is %ld",heartRate]];
  525. }
  526. break;
  527. case BodyFatScaleHeartRateMeasureStatus_Fail:
  528. {
  529. [self addLog:@"Heart Rate Test failure"];
  530. }
  531. break;
  532. default:
  533. break;
  534. }
  535. }
  536. /**
  537. Return error code(返回错误码)
  538. @param code 错误码
  539. 1:超重
  540. */
  541. -(void)bodyFatScaleManagerCallBackErrorCode:(NSInteger)code{
  542. [self addLog:[NSString stringWithFormat:@"error code = %ld",code]];
  543. }
  544. ////MARK:回调A6指令数据
  545. //
  546. ///**
  547. // Send success or failure callback for offline user list(发送离线用户列表的成功或失败回调)
  548. //
  549. // @param status 状态
  550. // */
  551. //-(void)bodyFatScaleManagerCallBackUpdateRecordStatus:(BodyFatScaleUpdateRecordStatus)status{
  552. // switch (status) {
  553. // case BodyFatScaleUpdateRecordStatus_AllSuccess:
  554. // {
  555. // [self addLog:@"Update All Users Success"];
  556. // }
  557. // break;
  558. // case BodyFatScaleUpdateRecordStatus_OneSuccess:
  559. // {
  560. // [self addLog:@"Update A User Success"];
  561. // }
  562. // break;
  563. // case BodyFatScaleUpdateRecordStatus_OneFail:
  564. // {
  565. // [self addLog:@"Update A User Failure"];
  566. // }
  567. // break;
  568. // case BodyFatScaleUpdateRecordStatus_AllFail:
  569. // {
  570. // [self addLog:@"Update All Users Failure"];
  571. // }
  572. // break;
  573. // default:
  574. // break;
  575. // }
  576. //}
  577. //
  578. ///**
  579. // Request callback for offline history(请求离线历史记录的回调)
  580. //
  581. // @param status 状态
  582. // */
  583. //-(void)bodyFatScaleManagerCallBackSendHistoryDataStatus:(BodyFatScaleSendHistoryDataStatus)status{
  584. // if (status == BodyFatScaleSendHistoryDataStatus_No) {
  585. // [self addLog:@"No offline datas"];
  586. // }else if (status == BodyFatScaleSendHistoryDataStatus_Begin){
  587. // [self addLog:@"Begin receive offline datas"];
  588. // }else if (status == BodyFatScaleSendHistoryDataStatus_End){
  589. // [self addLog:@"End receive offline datas"];
  590. // }
  591. //
  592. //}
  593. //
  594. ///**
  595. // APP algorithm-offline history data(APP 算法-离线历史记录数据)
  596. //
  597. // @param user 用户信息
  598. // */
  599. //-(void)bodyFatScaleManagerCallBackBleUserHistoryDataCustomADCModel:(ELBodyFatScaleBleUserModel *)user bodyDataModel:(ELBodyFatScaleBleWeightModel *)bodyModel{
  600. // //这里的身体指标等数据由app自定义算法计算得到,请区分bodyModel.algNum来调用算法
  601. // [self addLog:[NSString stringWithFormat:@"App algorithm-offline history data:userId = %ld weight = %ld unit =%@ point = %ld",user.usrID,bodyModel.weight,AiLinkBleWeightUnitDic[@(bodyModel.weightUnit)],bodyModel.weightPoint]];
  602. //}
  603. //
  604. ///**
  605. // MCU algorithm-offline history data(MCU 算法-离线历史记录数据)
  606. //
  607. // @param user 用户信息
  608. // @param bodyModel 体脂数据
  609. // */
  610. //-(void)bodyFatScaleManagerCallBackBleUserHistoryDataModel:(ELBodyFatScaleBleUserModel *)user bodyDataModel:(ELBodyFatScaleBleWeightModel *)bodyModel{
  611. // //这里的身体指标数据由秤计算得到
  612. // [self addLog:[NSString stringWithFormat:@"MCU algorithm-offline history data:userId = %ld weight = %ld unit =%@ point = %ld",user.usrID,bodyModel.weight,AiLinkBleWeightUnitDic[@(bodyModel.weightUnit)],bodyModel.weightPoint]];
  613. //}
  614. -(NSArray<ELBodyFatScaleBleUserModel *> *)get8Users{
  615. NSMutableArray *users = [[NSMutableArray alloc] init];
  616. for (int i=0; i<8; i++) {
  617. ELBodyFatScaleBleUserModel *user = [[ELBodyFatScaleBleUserModel alloc] init];
  618. user.createTime = [[NSDate date] timeIntervalSince1970];
  619. user.usrID = i;
  620. user.role = i%4;
  621. user.sex = i%2;
  622. user.age = 26+i;
  623. user.height = 170+i;
  624. user.weight = 600+i;
  625. user.adc = 560+i;
  626. [users addObject:user];
  627. }
  628. return users.copy;
  629. }
  630. -(ELBodyFatScaleBleUserModel *)getOneUser{
  631. ELBodyFatScaleBleUserModel *user = [[ELBodyFatScaleBleUserModel alloc] init];
  632. user.createTime = [[NSDate date] timeIntervalSince1970];
  633. user.usrID = 0;
  634. user.role = BodyFatScaleRole_Ordinary;
  635. user.sex = ELBluetoothUserSex_Woman;
  636. user.age = 26;
  637. user.height = 170;
  638. user.weight = 600;
  639. user.adc = 560;
  640. return user;
  641. }
  642. @end