iOS AILinkBleSDK - 蓝牙SDK
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. //
  2. // BodyScaleVC.m
  3. // AILinkBleSDKSourceCode
  4. //
  5. // Created by LarryZhang on 2023/3/27.
  6. // Copyright © 2020 IOT. All rights reserved.
  7. //
  8. #import <AILinkBleSDK/ELAILinkBleManager.h>
  9. #import <AILinkBleSDK/ELBluetoothManager+Settings.h>
  10. #import <AILinkBleSDK/ELBodyFatScaleBleManager.h>
  11. #import <AILinkBleSDK/ELBodyFatScaleBleUserModel.h>
  12. #import <AILinkBleSDK/ELBodyFatScaleBleWeightModel.h>
  13. #import "BodyScaleVC.h"
  14. #import "Masonry.h"
  15. #import <AILinkBleSDK/ELUnitConvertTool.h>
  16. #import <AILinkBleSDK/OEMAlgorithmSDK.h>
  17. @interface BodyScaleVC () <ELBluetoothManagerDelegate, ELBodyFatScaleBleDelegate>
  18. @property (nonatomic, strong) UITextView *textView;
  19. @property (nonatomic, copy) NSArray<NSNumber *> *units;
  20. @end
  21. @implementation BodyScaleVC
  22. - (void)viewDidLoad {
  23. [super viewDidLoad];
  24. self.view.backgroundColor = [UIColor whiteColor];
  25. [ELBodyFatScaleBleManager shareManager].delegate = self;
  26. [ELBodyFatScaleBleManager shareManager].bodyFatScaleDelegate = self;
  27. [[ELBodyFatScaleBleManager shareManager] startScan];
  28. [self setupUIView];
  29. }
  30. - (void)viewWillDisappear:(BOOL)animated {
  31. [super viewWillDisappear:animated];
  32. [[ELBodyFatScaleBleManager shareManager] disconnectPeripheral];
  33. }
  34. - (void)dealloc {
  35. }
  36. #pragma mark - Init UI
  37. - (void)setupUIView {
  38. UIButton *button1 = [UIButton buttonWithType:(UIButtonTypeCustom)];
  39. [button1 setTitle:@"Get BM Version" forState:(UIControlStateNormal)];
  40. button1.tag = 1;
  41. button1.titleLabel.adjustsFontSizeToFitWidth = YES;
  42. button1.titleLabel.numberOfLines = 2;
  43. button1.backgroundColor = [UIColor blackColor];
  44. [button1 addTarget:self action:@selector(buttonAction:) forControlEvents:(UIControlEventTouchUpInside)];
  45. [self.view addSubview:button1];
  46. [button1 mas_makeConstraints:^(MASConstraintMaker *make) {
  47. make.left.mas_equalTo(10);
  48. make.top.mas_equalTo(100);
  49. make.width.mas_equalTo(100);
  50. make.height.mas_equalTo(40);
  51. }];
  52. UIButton *button2 = [UIButton buttonWithType:(UIButtonTypeCustom)];
  53. [button2 setTitle:@"Get C V P ID" forState:(UIControlStateNormal)];
  54. button2.tag = 2;
  55. button2.titleLabel.adjustsFontSizeToFitWidth = YES;
  56. button2.titleLabel.numberOfLines = 2;
  57. button2.backgroundColor = [UIColor blackColor];
  58. [button2 addTarget:self action:@selector(buttonAction:) forControlEvents:(UIControlEventTouchUpInside)];
  59. [self.view addSubview:button2];
  60. [button2 mas_makeConstraints:^(MASConstraintMaker *make) {
  61. make.left.equalTo(button1.mas_right).mas_offset(10);
  62. make.top.mas_equalTo(100);
  63. make.width.mas_equalTo(100);
  64. make.height.mas_equalTo(40);
  65. }];
  66. UIButton *button3 = [UIButton buttonWithType:(UIButtonTypeCustom)];
  67. [button3 setTitle:@"Get Name" forState:(UIControlStateNormal)];
  68. button3.tag = 3;
  69. button3.titleLabel.adjustsFontSizeToFitWidth = YES;
  70. button3.titleLabel.numberOfLines = 2;
  71. button3.backgroundColor = [UIColor blackColor];
  72. [button3 addTarget:self action:@selector(buttonAction:) forControlEvents:(UIControlEventTouchUpInside)];
  73. [self.view addSubview:button3];
  74. [button3 mas_makeConstraints:^(MASConstraintMaker *make) {
  75. make.left.equalTo(button2.mas_right).mas_offset(10);
  76. make.top.mas_equalTo(100);
  77. make.width.mas_equalTo(100);
  78. make.height.mas_equalTo(40);
  79. }];
  80. //
  81. UIButton *button4 = [UIButton buttonWithType:(UIButtonTypeCustom)];
  82. [button4 setTitle:@"Set Name" forState:(UIControlStateNormal)];
  83. button4.tag = 4;
  84. button4.titleLabel.adjustsFontSizeToFitWidth = YES;
  85. button4.titleLabel.numberOfLines = 2;
  86. button4.backgroundColor = [UIColor blackColor];
  87. [button4 addTarget:self action:@selector(buttonAction:) forControlEvents:(UIControlEventTouchUpInside)];
  88. [self.view addSubview:button4];
  89. [button4 mas_makeConstraints:^(MASConstraintMaker *make) {
  90. make.left.mas_equalTo(10);
  91. make.top.equalTo(button1.mas_bottom).mas_offset(10);
  92. make.width.mas_equalTo(100);
  93. make.height.mas_equalTo(40);
  94. }];
  95. //
  96. UIButton *button5 = [UIButton buttonWithType:(UIButtonTypeCustom)];
  97. [button5 setTitle:@"Change unit" forState:(UIControlStateNormal)];
  98. button5.tag = 5;
  99. button5.titleLabel.adjustsFontSizeToFitWidth = YES;
  100. button5.titleLabel.numberOfLines = 2;
  101. button5.backgroundColor = [UIColor blackColor];
  102. [button5 addTarget:self action:@selector(buttonAction:) forControlEvents:(UIControlEventTouchUpInside)];
  103. [self.view addSubview:button5];
  104. [button5 mas_makeConstraints:^(MASConstraintMaker *make) {
  105. make.left.equalTo(button4.mas_right).mas_offset(10);
  106. make.top.equalTo(button1.mas_bottom).mas_offset(10);
  107. make.width.mas_equalTo(100);
  108. make.height.mas_equalTo(40);
  109. }];
  110. //
  111. self.textView = [[UITextView alloc] init];
  112. self.textView.backgroundColor = [UIColor blackColor];
  113. self.textView.text = @"Log";
  114. self.textView.textColor = [UIColor redColor];
  115. [self.view addSubview:self.textView];
  116. [self.textView mas_makeConstraints:^(MASConstraintMaker *make) {
  117. make.left.mas_equalTo(10);
  118. make.right.bottom.mas_equalTo(-10);
  119. make.top.equalTo(button5.mas_bottom).mas_offset(10);
  120. }];
  121. }
  122. #pragma mark - Action
  123. - (void)buttonAction:(UIButton *)sender {
  124. if ([ELBodyFatScaleBleManager shareManager].state != ELBluetoothStateDidValidationPass) {
  125. [self addLog:@"Disconnected"];
  126. return;
  127. }
  128. [self addLog:sender.titleLabel.text];
  129. NSInteger tag = sender.tag;
  130. if (tag == 1) {
  131. //getBluetoothInfoWithELInetGetCmdType是获取设备信息的方法
  132. [[ELBodyFatScaleBleManager shareManager] getBluetoothInfoWithELInetGetCmdType:(ELInetGetCmdTypeGetBMVersion)];
  133. } else if (tag == 2) {
  134. [[ELBodyFatScaleBleManager shareManager] getBluetoothInfoWithELInetGetCmdType:(ELInetGetCmdTypeGetC_V_P_ID)];
  135. } else if (tag == 3) {
  136. [[ELBodyFatScaleBleManager shareManager] getBluetoothInfoWithELInetGetCmdType:(ELInetGetCmdTypeGetName)];
  137. } else if (tag == 4) {
  138. [[ELBodyFatScaleBleManager shareManager] setBluetoothName:@"AILink"];
  139. } else if (tag == 5) {
  140. if (self.units.count == 0) {
  141. [self addLog:@"No units obtained"];
  142. return;
  143. }
  144. // [[ELBodyFatScaleBleManager shareManager] changeBodyFatScaleUnit:(ELDeviceWeightUnit_KG)];
  145. UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Change Unit" message:@"" preferredStyle:(UIAlertControllerStyleAlert)];
  146. for (int i = 0; i < self.units.count; i++) {
  147. UIAlertAction *action = [UIAlertAction actionWithTitle:AiLinkBleWeightUnitDic[self.units[i]]
  148. style:(UIAlertActionStyleDefault)
  149. handler:^(UIAlertAction *_Nonnull action) {
  150. [[ELBodyFatScaleBleManager shareManager] changeBodyFatScaleUnit:self.units[i].integerValue];
  151. }];
  152. [alert addAction:action];
  153. }
  154. [self presentViewController:alert animated:YES completion:nil];
  155. }
  156. }
  157. #pragma mark - ELBodyFatScaleBleDelegate
  158. - (void)bodyFatScaleManagerScanDevices:(NSArray<ELPeripheralModel *> *)scaleDevices {
  159. for (ELPeripheralModel *per in scaleDevices) {
  160. if ([per.macAddress isEqualToString:self.per.macAddressString]) {
  161. [[ELBodyFatScaleBleManager shareManager] stopScan];
  162. [[ELBodyFatScaleBleManager shareManager] connectPeripheral:per];
  163. }
  164. }
  165. }
  166. - (void)bodyFatScaleManagerUpdateState:(ELBluetoothState)state {
  167. switch (state) {
  168. case ELBluetoothStateUnavailable: {
  169. self.title = @"Please open the bluetooth";
  170. }
  171. break;
  172. case ELBluetoothStateAvailable: {
  173. self.title = @"Bluetooth is open";
  174. }
  175. break;
  176. case ELBluetoothStateScaning: {
  177. self.title = @"Scaning";
  178. }
  179. break;
  180. case ELBluetoothStateConnectFail: {
  181. self.title = @"Connect fail";
  182. }
  183. break;
  184. case ELBluetoothStateDidDisconnect: {
  185. self.title = @"Disconnected";
  186. }
  187. break;
  188. case ELBluetoothStateDidValidationPass: {
  189. self.title = @"Connected";
  190. //获取设备支持的单位
  191. [[ELBodyFatScaleBleManager shareManager] getBluetoothInfoWithELInetGetCmdType:(ELInetGetCmdTypeReadDeviceSupportUnit)];
  192. //将你自己设置的单位发给秤
  193. ELDeviceWeightUnit unit = ELDeviceWeightUnit_KG;
  194. [[ELBodyFatScaleBleManager shareManager] changeBodyFatScaleUnit:unit];
  195. }
  196. break;
  197. case ELBluetoothStateFailedValidation: {
  198. self.title = @"Illegal equipment";
  199. }
  200. break;
  201. case ELBluetoothStateWillConnect:
  202. self.title = @"Connecting";
  203. break;
  204. default:
  205. break;
  206. }
  207. }
  208. - (void)bluetoothManagerReceiceResponseType:(ELInetSetCmdType)type result:(ELSetBluetoothResponseType)result {
  209. if (type == ELInetSetCmdTypeSetName) {
  210. if (result == ELSetBluetoothResponseTypeSuccess) {
  211. [self addLog:@"Set name Success "];
  212. } else if (result == ELSetBluetoothResponseTypeFailure) {
  213. [self addLog:@"Set name failure "];
  214. } else if (result == ELSetBluetoothResponseTypeNoSupport) {
  215. [self addLog:@"Set name unsupport "];
  216. }
  217. }
  218. }
  219. - (void)bluetoothManagerReceiceName:(NSString *)name {
  220. [self addLog:name];
  221. }
  222. - (void)bluetoothManagerReceiveDID:(struct ELDIDStruct)did {
  223. [self addLog:[NSString stringWithFormat:@"CID = %lu, VID = %d, PID = %d", (unsigned long)did.deviceType, did.vendorID, did.productID]];
  224. }
  225. - (void)bluetoothManagerReceiveBMVersion:(NSString *)bmVersion {
  226. [self addLog:bmVersion];
  227. }
  228. - (void)bluetoothManagerBackDeviceSupportUnitWithWeight:(NSArray<NSNumber *> *)weightArray Height:(NSArray<NSNumber *> *)heightArray Temperature:(NSArray<NSNumber *> *)temperatureArray BloodPressure:(NSArray<NSNumber *> *)bloodPressureArray Pressure:(NSArray<NSNumber *> *)pressureArray {
  229. //这里只要体重数据
  230. self.units = weightArray;
  231. }
  232. /**
  233. Callback to set unit result(回调设置单位结果)
  234. @param result 结果
  235. */
  236. - (void)bodyFatScaleManagerCallBackSetUnitResult:(ELSetBluetoothResponseType)result {
  237. switch (result) {
  238. case ELSetBluetoothResponseTypeSuccess: {
  239. [self addLog:@"Change Unit Success"];
  240. }
  241. break;
  242. case ELSetBluetoothResponseTypeFailure: {
  243. [self addLog:@"Change Unit Failure"];
  244. }
  245. break;
  246. case ELSetBluetoothResponseTypeNoSupport: {
  247. [self addLog:@"Unsupport Change Unit"];
  248. }
  249. break;
  250. default:
  251. break;
  252. }
  253. }
  254. /**
  255. Return weight and body fat model(返回重量及体脂model)
  256. */
  257. - (void)bodyFatScaleManagerCallBackWeight:(ELBodyFatScaleBleWeightModel *)weightModel state:(BodyFatScaleMeasureStatus)status {
  258. switch (status) {
  259. case BodyFatScaleMeasureStatus_Unstable: {
  260. [self addLog:[NSString stringWithFormat:@"Unstable data weight = %ld unit = %@ point = %ld", weightModel.weight, AiLinkBleWeightUnitDic[@(weightModel.weightUnit)], weightModel.weightPoint]];
  261. }
  262. break;
  263. case BodyFatScaleMeasureStatus_Stable: {
  264. [self addLog:[NSString stringWithFormat:@"Stable data weight = %ld unit = %@ point = %ld", weightModel.weight, AiLinkBleWeightUnitDic[@(weightModel.weightUnit)], weightModel.weightPoint]];
  265. }
  266. break;
  267. case BodyFatScaleMeasureStatus_Failed: {
  268. [self addLog:@"Test failure"];
  269. }
  270. break;
  271. case BodyFatScaleMeasureStatus_Complete: {
  272. [self addLog:[NSString stringWithFormat:@"Tested weight = %ld unit = %@ point = %ld", weightModel.weight, AiLinkBleWeightUnitDic[@(weightModel.weightUnit)], weightModel.weightPoint]];
  273. NSLog(@"weightModel:%@", weightModel);
  274. //App算法
  275. [self algorithmWithBleModel:weightModel user:self.getOneUser];
  276. }
  277. break;
  278. default:
  279. break;
  280. }
  281. }
  282. - (void)algorithmWithBleModel:(ELBodyFatScaleBleWeightModel *)weightModel user:(ELBodyFatScaleBleUserModel *)user {
  283. NSString *weightKg = [ELUnitConvertTool getWeightShowStrWithTargetUnit:ELDeviceWeightUnit_KG bleWeightInt:weightModel.weight bleWeightUnit:weightModel.weightUnit bleWeightPoint:weightModel.weightPoint];
  284. OEMAlgorithmModel *algorithmModel = [OEMAlgorithmSDK getBodyfatWithWeight:weightKg.floatValue adc:(int)weightModel.adc sex:user.sex age:(int)user.age height:(int)user.height];
  285. NSLog(@"algorithmModel.bmi:%@", algorithmModel.bmi);
  286. }
  287. /**
  288. Return error code(返回错误码)
  289. @param code 错误码
  290. 1:超重
  291. */
  292. - (void)bodyFatScaleManagerCallBackErrorCode:(NSInteger)code {
  293. [self addLog:[NSString stringWithFormat:@"error code = %ld", code]];
  294. }
  295. #pragma mark - private
  296. - (void)addLog:(NSString *)log {
  297. self.textView.text = [NSString stringWithFormat:@"%@\n%@", log, self.textView.text];
  298. }
  299. - (NSArray<ELBodyFatScaleBleUserModel *> *)get8Users {
  300. NSMutableArray *users = [[NSMutableArray alloc] init];
  301. for (int i = 0; i < 8; i++) {
  302. ELBodyFatScaleBleUserModel *user = [[ELBodyFatScaleBleUserModel alloc] init];
  303. user.createTime = [[NSDate date] timeIntervalSince1970];
  304. user.usrID = i;
  305. user.role = i % 4;
  306. user.sex = i % 2;
  307. user.age = 26 + i;
  308. user.height = 170 + i;
  309. user.weight = 600 + i;
  310. user.adc = 560 + i;
  311. [users addObject:user];
  312. }
  313. return users.copy;
  314. }
  315. - (ELBodyFatScaleBleUserModel *)getOneUser {
  316. ELBodyFatScaleBleUserModel *user = [[ELBodyFatScaleBleUserModel alloc] init];
  317. user.createTime = [[NSDate date] timeIntervalSince1970];
  318. user.usrID = 0;
  319. user.role = BodyFatScaleRole_Ordinary;
  320. user.sex = ELBluetoothUserSex_Woman;
  321. user.age = 26;
  322. user.height = 170;
  323. user.weight = 600;
  324. user.adc = 560;
  325. return user;
  326. }
  327. @end