iOS AILinkBleSDK - 蓝牙SDK
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

EightScaleConnectionViewController.m 13KB

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