iOS AILinkBleSDK - 蓝牙SDK
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

EightScaleConnectionViewController.m 15KB

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