iOS AILinkBleSDK - 蓝牙SDK
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

BabyScaleConnectViewController.m 12KB

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