iOS AILinkBleSDK - 蓝牙SDK
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

NutritionScaleConnectionViewController.m 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. //
  2. // NutritionScaleConnectionViewController.m
  3. // AILinkBleSDKSourceCode
  4. //
  5. // Created by LarryZhang on 2021/12/13.
  6. // Copyright © 2021 IOT. All rights reserved.
  7. //
  8. #import "NutritionScaleConnectionViewController.h"
  9. #import "Masonry.h"
  10. #import <AILinkBleSDK/ELNutritionScaleBleHeader.h>
  11. #import <AILinkBleSDK/ELNutritionScaleBleManager.h>
  12. @interface NutritionScaleConnectionViewController () <NutritionScaleBleDelegate, ELBluetoothManagerDelegate>
  13. @property(nonatomic, strong) UITextView *textView;
  14. @property(nonatomic, copy) NSArray<NSNumber *> *units;
  15. @property(nonatomic, strong) UIButton *connectButton;
  16. @end
  17. @implementation NutritionScaleConnectionViewController
  18. - (void)viewDidLoad {
  19. [super viewDidLoad];
  20. // Do any additional setup after loading the view.
  21. self.view.backgroundColor = [UIColor whiteColor];
  22. [ELNutritionScaleBleManager shareManager].nutritionScaleBleDelegate = self;
  23. [ELNutritionScaleBleManager shareManager].delegate = self;
  24. [[ELNutritionScaleBleManager shareManager] connectPeripheral:self.p];
  25. [self setupUIView];
  26. }
  27. - (void)viewWillDisappear:(BOOL)animated {
  28. [[ELNutritionScaleBleManager shareManager] disconnectPeripheral];
  29. }
  30. - (void)addLog:(NSString *)log {
  31. self.textView.text = [NSString stringWithFormat:@"%@\n\n%@", log, self.textView.text];
  32. }
  33. - (void)connectDevice {
  34. [[ELNutritionScaleBleManager shareManager] startScan];
  35. }
  36. - (void)setupUIView {
  37. self.connectButton = [[UIButton alloc] initWithFrame:CGRectMake((self.view.frame.size.width - 100) / 2, 88, 100, 40)];
  38. [self.connectButton setTitle:@"点击重连" forState:UIControlStateNormal];
  39. [self.connectButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
  40. [self.view addSubview:self.connectButton];
  41. [self.connectButton addTarget:self action:@selector(connectDevice) forControlEvents:UIControlEventTouchUpInside];
  42. self.connectButton.hidden = YES;
  43. self.textView = [[UITextView alloc] init];
  44. self.textView.backgroundColor = [UIColor blackColor];
  45. self.textView.text = @"Log";
  46. self.textView.textColor = [UIColor redColor];
  47. [self.view addSubview:self.textView];
  48. [self.textView mas_makeConstraints:^(MASConstraintMaker *make) {
  49. make.left.mas_equalTo(10);
  50. make.right.mas_equalTo(-10);
  51. make.bottom.offset(-44);
  52. // make.height.mas_equalTo(350);
  53. make.top.offset(150);
  54. }];
  55. }
  56. #pragma mark - ble Delegate
  57. /// 蓝牙连接状态
  58. /// @param state 连接状态
  59. - (void)deviceBleReceiveState:(ELBluetoothState)state {
  60. switch (state) {
  61. case ELBluetoothStateUnavailable: {
  62. self.title = @"Please open the bluetooth";
  63. }
  64. break;
  65. case ELBluetoothStateAvailable: {
  66. self.title = @"Bluetooth is open";
  67. }
  68. break;
  69. case ELBluetoothStateScaning: {
  70. self.title = @"Scanning";
  71. }
  72. break;
  73. case ELBluetoothStateConnectFail: {
  74. self.title = @"Connect fail";
  75. }
  76. break;
  77. case ELBluetoothStateDidDisconnect: {
  78. self.title = @"Disconnected";
  79. self.connectButton.hidden = NO;
  80. }
  81. break;
  82. case ELBluetoothStateDidValidationPass: {
  83. self.connectButton.hidden = YES;
  84. self.title = @"Connected";
  85. //连接成功,获取单位
  86. [[ELNutritionScaleBleManager shareManager] getBluetoothInfoWithELInetGetCmdType:ELInetGetCmdTypeReadDeviceSupportUnit];
  87. //获取版本号
  88. [[ELNutritionScaleBleManager shareManager] getBluetoothInfoWithELInetGetCmdType:ELInetGetCmdTypeGetBMVersion];
  89. //同步时间到设备
  90. [[ELNutritionScaleBleManager shareManager] syncMCUNowDate];
  91. }
  92. break;
  93. case ELBluetoothStateFailedValidation: {
  94. self.title = @"Illegal equipment";
  95. }
  96. break;
  97. case ELBluetoothStateWillConnect:
  98. self.title = @"Connecting";
  99. break;
  100. default:
  101. break;
  102. }
  103. }
  104. /// 附近的设备
  105. /// @param devices 设备列表
  106. - (void)deviceBleReceiveDevices:(NSArray<ELPeripheralModel *> *)devices {
  107. for (ELPeripheralModel *model in devices) {
  108. if ([model.macAddress isEqualToString:self.p.macAddress]) {
  109. [[ELNutritionScaleBleManager shareManager] connectPeripheral:model];
  110. }
  111. }
  112. }
  113. //获得支持单位列表
  114. - (void)supportWeightUnits:(NSArray *_Nullable)weightArray {
  115. NSLog(@"weightArray: %@", weightArray);
  116. [self addLog:@"获得支持单位列表"];
  117. }
  118. //称重数据
  119. - (void)nutritionScaleBleDataModel:(ELNutritionScaleDataModel *_Nonnull)model {
  120. NSLog(@"%s model:%@", __FUNCTION__, model);
  121. NSString *testData = [NSString stringWithFormat:@"weight:%@ weightPoint:%@ weightUnit:%@ sn:%d", @(model.weight), @(model.weightPoint), @(model.weightUnit), model.serialNumber];
  122. [self addLog:testData];
  123. }
  124. //超载
  125. - (void)overload:(BOOL)status {
  126. [self addLog:[NSString stringWithFormat:@"超载: %d", status]];
  127. }
  128. //低电
  129. - (void)lowPower:(BOOL)status {
  130. [self addLog:[NSString stringWithFormat:@"低电: %d", status]];
  131. }
  132. @end