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.

BabyScaleConnectViewController.m 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. //
  2. // BabyScaleConnectViewController.m
  3. // AILinkBleSDKSourceCode
  4. //
  5. // Created by iot_user on 2020/4/7.
  6. // Copyright © 2020 IOT. All rights reserved.
  7. //
  8. #import "BabyScaleConnectViewController.h"
  9. #import "Masonry.h"
  10. #import <AILinkBleSDK/ELBabyScaleBleManager.h>
  11. @interface BabyScaleConnectViewController ()<ELBluetoothManagerDelegate,BabyScaleBleDelegate>
  12. @property (nonatomic, strong) UITextView *textView;
  13. @end
  14. @implementation BabyScaleConnectViewController
  15. - (void)viewDidLoad {
  16. [super viewDidLoad];
  17. self.view.backgroundColor = [UIColor whiteColor];
  18. [ELBabyScaleBleManager shareManager].babyScaleDelegate = self;
  19. [ELBabyScaleBleManager shareManager].delegate =self;
  20. [[ELBabyScaleBleManager shareManager] connectPeripheral:self.p];
  21. [self setupUIView];
  22. }
  23. -(void)viewWillDisappear:(BOOL)animated{
  24. [super viewWillDisappear:animated];
  25. [[ELBabyScaleBleManager shareManager] disconnectPeripheral];
  26. }
  27. -(void)addLog:(NSString *)log{
  28. self.textView.text = [NSString stringWithFormat:@"%@\n%@",log,self.textView.text];
  29. }
  30. -(void)buttonAction:(UIButton *)sender{
  31. if ([ELBabyScaleBleManager shareManager].state != ELBluetoothStateDidValidationPass) {
  32. [self addLog:@"Disconnected"];
  33. return;
  34. }
  35. [self addLog:sender.titleLabel.text];
  36. NSInteger tag = sender.tag;
  37. if (tag == 1) {
  38. //getBluetoothInfoWithELInetGetCmdType是获取设备信息的方法
  39. [[ELBabyScaleBleManager shareManager] getBluetoothInfoWithELInetGetCmdType:(ELInetGetCmdTypeGetBMVersion)];
  40. }else if (tag == 2){
  41. [[ELBabyScaleBleManager shareManager] getBluetoothInfoWithELInetGetCmdType:(ELInetGetCmdTypeGetC_V_P_ID)];
  42. }else if (tag == 3){
  43. [[ELBabyScaleBleManager shareManager] getBluetoothInfoWithELInetGetCmdType:(ELInetGetCmdTypeGetName)];
  44. }else if (tag == 4){
  45. [[ELBabyScaleBleManager shareManager] setBluetoothName:@"AILink"];
  46. }else if (tag == 5){
  47. [[ELBabyScaleBleManager shareManager] changeHold:YES];
  48. }else if (tag == 6){
  49. [[ELBabyScaleBleManager shareManager] changeHold:NO];
  50. }else if (tag == 7){
  51. [[ELBabyScaleBleManager shareManager] changeHeightUnit:(ELDeviceHeightUnit_CM) AndWeightUnit:(ELDeviceWeightUnit_KG)];
  52. }
  53. }
  54. -(void)setupUIView{
  55. UIButton *button1 = [UIButton buttonWithType:(UIButtonTypeCustom)];
  56. [button1 setTitle:@"Get BM Version" forState:(UIControlStateNormal)];
  57. button1.tag = 1;
  58. button1.titleLabel.adjustsFontSizeToFitWidth = YES;
  59. button1.titleLabel.numberOfLines = 2;
  60. button1.backgroundColor = [UIColor blackColor];
  61. [button1 addTarget:self action:@selector(buttonAction:) forControlEvents:(UIControlEventTouchUpInside)];
  62. [self.view addSubview:button1];
  63. [button1 mas_makeConstraints:^(MASConstraintMaker *make) {
  64. make.left.mas_equalTo(10);
  65. make.top.mas_equalTo(80);
  66. make.width.mas_equalTo(100);
  67. make.height.mas_equalTo(40);
  68. }];
  69. UIButton *button2 = [UIButton buttonWithType:(UIButtonTypeCustom)];
  70. [button2 setTitle:@"Get C V P ID" forState:(UIControlStateNormal)];
  71. button2.tag = 2;
  72. button2.titleLabel.adjustsFontSizeToFitWidth = YES;
  73. button2.titleLabel.numberOfLines = 2;
  74. button2.backgroundColor = [UIColor blackColor];
  75. [button2 addTarget:self action:@selector(buttonAction:) forControlEvents:(UIControlEventTouchUpInside)];
  76. [self.view addSubview:button2];
  77. [button2 mas_makeConstraints:^(MASConstraintMaker *make) {
  78. make.left.equalTo(button1.mas_right).mas_offset(10);
  79. make.top.mas_equalTo(80);
  80. make.width.mas_equalTo(100);
  81. make.height.mas_equalTo(40);
  82. }];
  83. UIButton *button3 = [UIButton buttonWithType:(UIButtonTypeCustom)];
  84. [button3 setTitle:@"Get Name" forState:(UIControlStateNormal)];
  85. button3.tag = 3;
  86. button3.titleLabel.adjustsFontSizeToFitWidth = YES;
  87. button3.titleLabel.numberOfLines = 2;
  88. button3.backgroundColor = [UIColor blackColor];
  89. [button3 addTarget:self action:@selector(buttonAction:) forControlEvents:(UIControlEventTouchUpInside)];
  90. [self.view addSubview:button3];
  91. [button3 mas_makeConstraints:^(MASConstraintMaker *make) {
  92. make.left.equalTo(button2.mas_right).mas_offset(10);
  93. make.top.mas_equalTo(80);
  94. make.width.mas_equalTo(100);
  95. make.height.mas_equalTo(40);
  96. }];
  97. //
  98. UIButton *button4 = [UIButton buttonWithType:(UIButtonTypeCustom)];
  99. [button4 setTitle:@"Set Name" forState:(UIControlStateNormal)];
  100. button4.tag = 4;
  101. button4.titleLabel.adjustsFontSizeToFitWidth = YES;
  102. button4.titleLabel.numberOfLines = 2;
  103. button4.backgroundColor = [UIColor blackColor];
  104. [button4 addTarget:self action:@selector(buttonAction:) forControlEvents:(UIControlEventTouchUpInside)];
  105. [self.view addSubview:button4];
  106. [button4 mas_makeConstraints:^(MASConstraintMaker *make) {
  107. make.left.mas_equalTo(10);
  108. make.top.equalTo(button1.mas_bottom).mas_offset(10);
  109. make.width.mas_equalTo(100);
  110. make.height.mas_equalTo(40);
  111. }];
  112. //
  113. UIButton *button5 = [UIButton buttonWithType:(UIButtonTypeCustom)];
  114. [button5 setTitle:@"Hold" forState:(UIControlStateNormal)];
  115. button5.tag = 5;
  116. button5.titleLabel.adjustsFontSizeToFitWidth = YES;
  117. button5.titleLabel.numberOfLines = 2;
  118. button5.backgroundColor = [UIColor blackColor];
  119. [button5 addTarget:self action:@selector(buttonAction:) forControlEvents:(UIControlEventTouchUpInside)];
  120. [self.view addSubview:button5];
  121. [button5 mas_makeConstraints:^(MASConstraintMaker *make) {
  122. make.left.equalTo(button4.mas_right).mas_offset(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. //
  129. UIButton *button6 = [UIButton buttonWithType:(UIButtonTypeCustom)];
  130. [button6 setTitle:@"Zero" forState:(UIControlStateNormal)];
  131. button6.tag = 6;
  132. button6.titleLabel.adjustsFontSizeToFitWidth = YES;
  133. button6.titleLabel.numberOfLines = 2;
  134. button6.backgroundColor = [UIColor blackColor];
  135. [button6 addTarget:self action:@selector(buttonAction:) forControlEvents:(UIControlEventTouchUpInside)];
  136. [self.view addSubview:button6];
  137. [button6 mas_makeConstraints:^(MASConstraintMaker *make) {
  138. make.left.equalTo(button5.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. //
  144. UIButton *button7 = [UIButton buttonWithType:(UIButtonTypeCustom)];
  145. [button7 setTitle:@"Change Unit" forState:(UIControlStateNormal)];
  146. button7.tag = 7;
  147. button7.titleLabel.adjustsFontSizeToFitWidth = YES;
  148. button7.titleLabel.numberOfLines = 2;
  149. button7.backgroundColor = [UIColor blackColor];
  150. [button7 addTarget:self action:@selector(buttonAction:) forControlEvents:(UIControlEventTouchUpInside)];
  151. [self.view addSubview:button7];
  152. [button7 mas_makeConstraints:^(MASConstraintMaker *make) {
  153. make.left.mas_equalTo(10);
  154. make.top.equalTo(button4.mas_bottom).mas_offset(10);
  155. make.width.mas_equalTo(100);
  156. make.height.mas_equalTo(40);
  157. }];
  158. //
  159. self.textView = [[UITextView alloc] init];
  160. self.textView.backgroundColor = [UIColor blackColor];
  161. self.textView.text = @"Log";
  162. self.textView.textColor = [UIColor redColor];
  163. [self.view addSubview:self.textView];
  164. [self.textView mas_makeConstraints:^(MASConstraintMaker *make) {
  165. make.left.mas_equalTo(10);
  166. make.right.bottom.mas_equalTo(-10);
  167. make.height.mas_equalTo(200);
  168. }];
  169. }
  170. //错误
  171. -(void)babyScaleBackDeviceErrorCode:(BabyScaleDeviceErrorCode)code{
  172. [self addLog:[NSString stringWithFormat:@"error code = %ld",code]];
  173. }
  174. //体重
  175. -(void)babyScaleManagerReceiveWeightModel:(ELBabyScaleBleWeightModel *)model{
  176. [self addLog:[NSString stringWithFormat:@"weight = %ld unit=%@ point = %ld",model.weight,model.unitStr,model.weightPoint]];
  177. }
  178. //身长数据
  179. -(void)babyScaleManagerHeightModel:(ELBabyScaleBleHeightModel *)model{
  180. [self addLog:[NSString stringWithFormat:@"height = %ld unit=%@ point = %ld",model.height,model.unitStr,model.heightPoint]];
  181. }
  182. -(void)babyScaleBackCtrlType:(NSInteger)type Results:(BabyScaleResultType)result{
  183. // 0去皮,1锁定)
  184. if (type == 0) {
  185. if (result == BabyScaleResultType_Succeed) {
  186. [self addLog:@"Zero Unit success"];
  187. }else if (result == BabyScaleResultType_Failure){
  188. [self addLog:@"Zero Unit failure"];
  189. }else if (result == BabyScaleResultType_Unsupported){
  190. [self addLog:@"Zero Unit unsupported"];
  191. }
  192. }else{
  193. if (result == BabyScaleResultType_Succeed) {
  194. [self addLog:@"Hold Unit success"];
  195. }else if (result == BabyScaleResultType_Failure){
  196. [self addLog:@"Hold Unit failure"];
  197. }else if (result == BabyScaleResultType_Unsupported){
  198. [self addLog:@"Hold Unit unsupported"];
  199. }
  200. }
  201. }
  202. -(void)babyScaleBackSetUnitResults:(BabyScaleResultType)result{
  203. if (result == BabyScaleResultType_Succeed) {
  204. [self addLog:@"Change Unit success"];
  205. }else if (result == BabyScaleResultType_Failure){
  206. [self addLog:@"Change Unit failure"];
  207. }else if (result == BabyScaleResultType_Unsupported){
  208. [self addLog:@"Change Unit unsupported"];
  209. }
  210. }
  211. -(void)bluetoothManagerReceiceResponseType:(ELInetSetCmdType)type result:(ELSetBluetoothResponseType)result{
  212. if (type == ELInetSetCmdTypeSetName) {
  213. if (result == ELSetBluetoothResponseTypeSuccess) {
  214. [self addLog:@"Set name Success "];
  215. }else if (result == ELSetBluetoothResponseTypeFailure){
  216. [self addLog:@"Set name failure "];
  217. }else if (result == ELSetBluetoothResponseTypeNoSupport){
  218. [self addLog:@"Set name unsupport "];
  219. }
  220. }
  221. }
  222. -(void)bluetoothManagerReceiceName:(NSString *)name{
  223. [self addLog:name];
  224. }
  225. -(void)bluetoothManagerReceiveDID:(struct ELDIDStruct)did{
  226. [self addLog:[NSString stringWithFormat:@"CID = %d,VID = %d,PID = %d",did.deviceType,did.vendorID,did.productID]];
  227. }
  228. -(void)bluetoothManagerReceiveBMVersion:(NSString *)bmVersion{
  229. [self addLog:bmVersion];
  230. }
  231. //MARK:获取设备支持的单位
  232. -(void)bluetoothManagerBackDeviceSupportUnitWithWeight:(NSArray<NSNumber *> *)weightArray Height:(NSArray<NSNumber *> *)heightArray Temperature:(NSArray<NSNumber *> *)temperatureArray BloodPressure:(NSArray<NSNumber *> *)bloodPressureArray Pressure:(NSArray<NSNumber *> *)pressureArray{
  233. //这里体重和身高单位
  234. //设置单位时,要设置设备支持的单位
  235. }
  236. -(void)babyScaleManagerUpdateState:(ELBluetoothState)state{
  237. switch (state) {
  238. case ELBluetoothStateUnavailable:
  239. {
  240. self.title = @"Please open the bluetooth";
  241. }
  242. break;
  243. case ELBluetoothStateAvailable:
  244. {
  245. self.title = @"Bluetooth is open";
  246. }
  247. break;
  248. case ELBluetoothStateScaning:
  249. {
  250. self.title = @"Scaning";
  251. }
  252. break;
  253. case ELBluetoothStateConnectFail:
  254. {
  255. self.title = @"Connect fail";
  256. }
  257. break;
  258. case ELBluetoothStateDidDisconnect:
  259. {
  260. self.title = @"Disconnected";
  261. }
  262. break;
  263. case ELBluetoothStateDidValidationPass:
  264. {
  265. self.title = @"Connected";
  266. [[ELBabyScaleBleManager shareManager] getBluetoothInfoWithELInetGetCmdType:(ELInetGetCmdTypeReadDeviceSupportUnit)];
  267. }
  268. break;
  269. case ELBluetoothStateFailedValidation:
  270. {
  271. self.title = @"Illegal equipment";
  272. }
  273. break;
  274. case ELBluetoothStateWillConnect:
  275. self.title = @"Connecting";
  276. break;
  277. default:
  278. break;
  279. }
  280. }
  281. -(void)dealloc{
  282. }
  283. @end