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.

EightScaleConnectionViewController.m 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. //
  2. // EightScaleConnectionViewController.m
  3. // AILinkBleSDKSourceCode
  4. //
  5. // Created by iot_user on 2020/9/12.
  6. // Copyright © 2020 IOT. All rights reserved.
  7. //
  8. #import "EightScaleConnectionViewController.h"
  9. #import "Masonry.h"
  10. #import <AILinkBleSDK/ELEightScaleBleManager.h>
  11. #import <AILinkBleSDK/ELEightScaleBleDataModel.h>
  12. #import <AILinkBleSDK/ELEightScaleSDKHeader.h>
  13. #import "ELEightScaleAlgorithmTool.h"
  14. @interface EightScaleConnectionViewController ()<ELBluetoothManagerDelegate,EightScaleBleDeletegate>
  15. @property (nonatomic, strong) UITextView *textView;
  16. @property (nonatomic, copy) NSArray<NSNumber *> *units;
  17. @end
  18. @implementation EightScaleConnectionViewController
  19. - (void)viewDidLoad {
  20. [super viewDidLoad];
  21. self.view.backgroundColor = [UIColor whiteColor];
  22. [ELEightScaleBleManager shareManager].eightScaleDelegate = self;
  23. [ELEightScaleBleManager shareManager].delegate =self;
  24. [[ELEightScaleBleManager shareManager] connectPeripheral:self.p];
  25. [self setupUIView];
  26. }
  27. -(void)viewWillDisappear:(BOOL)animated{
  28. [super viewWillDisappear:animated];
  29. [[ELEightScaleBleManager shareManager] disconnectPeripheral];
  30. }
  31. -(void)addLog:(NSString *)log{
  32. self.textView.text = [NSString stringWithFormat:@"%@\n%@",log,self.textView.text];
  33. }
  34. -(void)buttonAction:(UIButton *)sender{
  35. if ([ELEightScaleBleManager shareManager].state != ELBluetoothStateDidValidationPass) {
  36. [self addLog:@"Disconnected"];
  37. return;
  38. }
  39. [self addLog:sender.titleLabel.text];
  40. NSInteger tag = sender.tag;
  41. if (tag == 1) {
  42. //getBluetoothInfoWithELInetGetCmdType是获取设备信息的方法
  43. [[ELEightScaleBleManager shareManager] getBluetoothInfoWithELInetGetCmdType:(ELInetGetCmdTypeGetBMVersion)];
  44. }else if (tag == 2){
  45. [[ELEightScaleBleManager shareManager] getBluetoothInfoWithELInetGetCmdType:(ELInetGetCmdTypeGetC_V_P_ID)];
  46. }else if (tag == 3){
  47. [[ELEightScaleBleManager shareManager] getBluetoothInfoWithELInetGetCmdType:(ELInetGetCmdTypeGetName)];
  48. }else if (tag == 4){
  49. [[ELEightScaleBleManager shareManager] setBluetoothName:@"AILink"];
  50. }else if (tag == 5){
  51. if (self.units.count==0) {
  52. [self addLog:@"No units obtained"];
  53. return;
  54. }
  55. // [[ELEightScaleBleManager shareManager] changeBodyFatScaleUnit:(ELDeviceWeightUnit_KG)];
  56. UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Change Unit" message:@"" preferredStyle:(UIAlertControllerStyleAlert)];
  57. for (int i=0; i<self.units.count; i++) {
  58. UIAlertAction *action = [UIAlertAction actionWithTitle:AiLinkBleWeightUnitDic[self.units[i]] style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
  59. [[ELEightScaleBleManager shareManager] eightScaleSwitchWeightUnit:self.units[i].integerValue];
  60. }];
  61. [alert addAction:action];
  62. }
  63. [self presentViewController:alert animated:YES completion:nil];
  64. }else if (tag == 6){
  65. }else if (tag == 7){
  66. }else if (tag == 8){
  67. }else if (tag == 9){
  68. }
  69. }
  70. -(void)setupUIView{
  71. UIButton *button1 = [UIButton buttonWithType:(UIButtonTypeCustom)];
  72. [button1 setTitle:@"Get BM Version" forState:(UIControlStateNormal)];
  73. button1.tag = 1;
  74. button1.titleLabel.adjustsFontSizeToFitWidth = YES;
  75. button1.titleLabel.numberOfLines = 2;
  76. button1.backgroundColor = [UIColor blackColor];
  77. [button1 addTarget:self action:@selector(buttonAction:) forControlEvents:(UIControlEventTouchUpInside)];
  78. [self.view addSubview:button1];
  79. [button1 mas_makeConstraints:^(MASConstraintMaker *make) {
  80. make.left.mas_equalTo(10);
  81. make.top.mas_equalTo(80);
  82. make.width.mas_equalTo(100);
  83. make.height.mas_equalTo(40);
  84. }];
  85. UIButton *button2 = [UIButton buttonWithType:(UIButtonTypeCustom)];
  86. [button2 setTitle:@"Get C V P ID" forState:(UIControlStateNormal)];
  87. button2.tag = 2;
  88. button2.titleLabel.adjustsFontSizeToFitWidth = YES;
  89. button2.titleLabel.numberOfLines = 2;
  90. button2.backgroundColor = [UIColor blackColor];
  91. [button2 addTarget:self action:@selector(buttonAction:) forControlEvents:(UIControlEventTouchUpInside)];
  92. [self.view addSubview:button2];
  93. [button2 mas_makeConstraints:^(MASConstraintMaker *make) {
  94. make.left.equalTo(button1.mas_right).mas_offset(10);
  95. make.top.mas_equalTo(80);
  96. make.width.mas_equalTo(100);
  97. make.height.mas_equalTo(40);
  98. }];
  99. UIButton *button3 = [UIButton buttonWithType:(UIButtonTypeCustom)];
  100. [button3 setTitle:@"Get Name" forState:(UIControlStateNormal)];
  101. button3.tag = 3;
  102. button3.titleLabel.adjustsFontSizeToFitWidth = YES;
  103. button3.titleLabel.numberOfLines = 2;
  104. button3.backgroundColor = [UIColor blackColor];
  105. [button3 addTarget:self action:@selector(buttonAction:) forControlEvents:(UIControlEventTouchUpInside)];
  106. [self.view addSubview:button3];
  107. [button3 mas_makeConstraints:^(MASConstraintMaker *make) {
  108. make.left.equalTo(button2.mas_right).mas_offset(10);
  109. make.top.mas_equalTo(80);
  110. make.width.mas_equalTo(100);
  111. make.height.mas_equalTo(40);
  112. }];
  113. //
  114. UIButton *button4 = [UIButton buttonWithType:(UIButtonTypeCustom)];
  115. [button4 setTitle:@"Set Name" forState:(UIControlStateNormal)];
  116. button4.tag = 4;
  117. button4.titleLabel.adjustsFontSizeToFitWidth = YES;
  118. button4.titleLabel.numberOfLines = 2;
  119. button4.backgroundColor = [UIColor blackColor];
  120. [button4 addTarget:self action:@selector(buttonAction:) forControlEvents:(UIControlEventTouchUpInside)];
  121. [self.view addSubview:button4];
  122. [button4 mas_makeConstraints:^(MASConstraintMaker *make) {
  123. make.left.mas_equalTo(10);
  124. make.top.equalTo(button1.mas_bottom).mas_offset(10);
  125. make.width.mas_equalTo(100);
  126. make.height.mas_equalTo(40);
  127. }];
  128. //
  129. UIButton *button5 = [UIButton buttonWithType:(UIButtonTypeCustom)];
  130. [button5 setTitle:@"Change unit" forState:(UIControlStateNormal)];
  131. button5.tag = 5;
  132. button5.titleLabel.adjustsFontSizeToFitWidth = YES;
  133. button5.titleLabel.numberOfLines = 2;
  134. button5.backgroundColor = [UIColor blackColor];
  135. [button5 addTarget:self action:@selector(buttonAction:) forControlEvents:(UIControlEventTouchUpInside)];
  136. [self.view addSubview:button5];
  137. [button5 mas_makeConstraints:^(MASConstraintMaker *make) {
  138. make.left.equalTo(button4.mas_right).mas_offset(10);
  139. make.top.equalTo(button1.mas_bottom).mas_offset(10);
  140. make.width.mas_equalTo(100);
  141. make.height.mas_equalTo(40);
  142. }];
  143. self.textView = [[UITextView alloc] init];
  144. self.textView.backgroundColor = [UIColor blackColor];
  145. self.textView.text = @"Log";
  146. self.textView.textColor = [UIColor redColor];
  147. [self.view addSubview:self.textView];
  148. [self.textView mas_makeConstraints:^(MASConstraintMaker *make) {
  149. make.left.mas_equalTo(10);
  150. make.right.bottom.mas_equalTo(-10);
  151. make.height.mas_equalTo(200);
  152. }];
  153. }
  154. -(void)bluetoothManagerReceiceResponseType:(ELInetSetCmdType)type result:(ELSetBluetoothResponseType)result{
  155. if (type == ELInetSetCmdTypeSetName) {
  156. if (result == ELSetBluetoothResponseTypeSuccess) {
  157. [self addLog:@"Set name Success "];
  158. }else if (result == ELSetBluetoothResponseTypeFailure){
  159. [self addLog:@"Set name failure "];
  160. }else if (result == ELSetBluetoothResponseTypeNoSupport){
  161. [self addLog:@"Set name unsupport "];
  162. }
  163. }
  164. }
  165. -(void)bluetoothManagerReceiceName:(NSString *)name{
  166. [self addLog:name];
  167. }
  168. -(void)bluetoothManagerReceiveDID:(struct ELDIDStruct)did{
  169. [self addLog:[NSString stringWithFormat:@"CID = %d,VID = %d,PID = %d",did.deviceType,did.vendorID,did.productID]];
  170. }
  171. -(void)bluetoothManagerReceiveBMVersion:(NSString *)bmVersion{
  172. [self addLog:bmVersion];
  173. }
  174. -(void)bluetoothManagerBackDeviceSupportUnitWithWeight:(NSArray<NSNumber *> *)weightArray Height:(NSArray<NSNumber *> *)heightArray Temperature:(NSArray<NSNumber *> *)temperatureArray BloodPressure:(NSArray<NSNumber *> *)bloodPressureArray Pressure:(NSArray<NSNumber *> *)pressureArray{
  175. //这里只要体重数据
  176. self.units = weightArray;
  177. }
  178. -(void)eightScaleBleReceiveAdcData:(EightScaleAdcStruct)adcStruct{
  179. [self addLog:[NSString stringWithFormat:@"Impedance type = %lu adc = %d",adcStruct.adcType,adcStruct.adc]];
  180. }
  181. -(void)eightScaleBleReceiveHeartRateTestStep:(EightScaleHeartRateTestStep)testStep heartRate:(int)heartRate{
  182. [self addLog:[NSString stringWithFormat:@"HeartRate = %d",heartRate]];
  183. }
  184. /**
  185. Callback to set unit result(回调设置单位结果)
  186. @param result 结果
  187. */
  188. - (void)eightScaleBleReceiveSwitchWeightUnitResult:(EightScaleSwitchUnitResult)result{
  189. switch (result) {
  190. case EightScaleSwitchUnitResultSuccess:
  191. {
  192. [self addLog:@"Change Unit Success"];
  193. }
  194. break;
  195. case EightScaleSwitchUnitResultFailure:
  196. {
  197. [self addLog:@"Change Unit Failure"];
  198. }
  199. break;
  200. case EightScaleSwitchUnitResultOperation:
  201. {
  202. [self addLog:@"Operationing"];
  203. }
  204. break;
  205. default:
  206. break;
  207. }
  208. }
  209. /**
  210. Return weight and body fat model(返回重量及体脂model)
  211. */
  212. -(void)eightScaleBleReceiveWeightData:(EightScaleWeightStruct)weightStruct{
  213. switch (weightStruct.weightType) {
  214. case EightScaleWeightTypeAuto:
  215. {
  216. [self addLog:[NSString stringWithFormat:@"Unstable data weight = %d unit = %@ point = %d adc = %ld",weightStruct.weight,AiLinkBleWeightUnitDic[@(weightStruct.unit)],weightStruct.point,weightStruct.weightType]];
  217. }
  218. break;
  219. case EightScaleWeightTypeStable:
  220. {
  221. [self addLog:[NSString stringWithFormat:@"Unstable data weight = %d unit = %@ point = %d adc = %ld",weightStruct.weight,AiLinkBleWeightUnitDic[@(weightStruct.unit)],weightStruct.point,weightStruct.weightType]];
  222. }
  223. break;
  224. default:
  225. break;
  226. }
  227. }
  228. -(void)eightScaleBleReceiveTestComplete{
  229. [self addLog:@"Measurn Complete"];
  230. }
  231. #pragma mark ============ get all body fat data ==============
  232. -(void)eightScaleBleReceiveTestData:(ELEightScaleBleDataModel *)dataModel{
  233. [self addLog:[NSString stringWithFormat:@"weight = %@%@",dataModel.weight,AiLinkBleWeightUnitDic[@(dataModel.weightUnit)]]];
  234. //Please input the correct height, age, gender of your app user
  235. ELEightScaleRecordModel *bodyInfo = [ELEightScaleAlgorithmTool getRecordModelWithBleDataModel:dataModel withUserSex:1 height:175 age:25];
  236. [self addLog:[NSString stringWithFormat:@"---bodyInfo:%@",bodyInfo]];
  237. }
  238. -(void)eightScaleBleReceiveErrorCode:(int)errorCode{
  239. [self addLog:[NSString stringWithFormat:@"Error Code is %d",errorCode]];
  240. }
  241. -(void)eightScaleBleReceiveTempData:(EightScaleTempStruct)tempStruct{
  242. [self addLog:[NSString stringWithFormat:@"temperature is %d ",tempStruct.temp]];
  243. }
  244. -(void)eightScaleBleReceiveState:(ELBluetoothState)state{
  245. switch (state) {
  246. case ELBluetoothStateUnavailable:
  247. {
  248. self.title = @"Please open the bluetooth";
  249. }
  250. break;
  251. case ELBluetoothStateAvailable:
  252. {
  253. self.title = @"Bluetooth is open";
  254. }
  255. break;
  256. case ELBluetoothStateScaning:
  257. {
  258. self.title = @"Scaning";
  259. }
  260. break;
  261. case ELBluetoothStateConnectFail:
  262. {
  263. self.title = @"Connect fail";
  264. }
  265. break;
  266. case ELBluetoothStateDidDisconnect:
  267. {
  268. self.title = @"Disconnected";
  269. }
  270. break;
  271. case ELBluetoothStateDidValidationPass:
  272. {
  273. self.title = @"Connected";
  274. //获取设备支持的单位
  275. [[ELEightScaleBleManager shareManager] getBluetoothInfoWithELInetGetCmdType:(ELInetGetCmdTypeReadDeviceSupportUnit)];
  276. //将你自己设置的单位发给秤
  277. ELDeviceWeightUnit unit = ELDeviceWeightUnit_KG;
  278. [[ELEightScaleBleManager shareManager] eightScaleSwitchWeightUnit:unit];
  279. if (self.p.deviceType == ELSupportDeviceTypeBLE_WIFIScale) {
  280. //获取蓝牙连接状态
  281. [[ELEightScaleBleManager shareManager] getBluetoothInfoWithELInetGetCmdType:(ELInetGetCmdTypeGetConnectState)];
  282. }
  283. }
  284. break;
  285. case ELBluetoothStateFailedValidation:
  286. {
  287. self.title = @"Illegal equipment";
  288. }
  289. break;
  290. case ELBluetoothStateWillConnect:
  291. self.title = @"Connecting";
  292. break;
  293. default:
  294. break;
  295. }
  296. }
  297. -(void)dealloc{
  298. }
  299. @end