iOS AILinkBleSDK - 蓝牙SDK
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

ELBfsWifiConnectVC.m 26KB

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