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.

AiFreshNutritionScaleConnectionViewController.m 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. //
  2. // AiFreshNutritionScaleConnectionViewController.m
  3. // AILinkBleSDKSourceCode
  4. //
  5. // Created by LarryZhang on 2021/12/13.
  6. // Copyright © 2021 IOT. All rights reserved.
  7. //
  8. #import "AiFreshNutritionScaleConnectionViewController.h"
  9. #import "Masonry.h"
  10. #import <AILinkBleSDK/ELAiFreshNutritionScaleBleHeader.h>
  11. #import <AILinkBleSDK/ELAiFreshNutritionScaleBleManager.h>
  12. @interface AiFreshNutritionScaleConnectionViewController () <AiFreshNutritionScaleBleDelegate, ELBluetoothManagerDelegate>
  13. @property(nonatomic, strong) UITextView *textView;
  14. @property(nonatomic, copy) NSArray<NSNumber *> *units;
  15. @property(nonatomic, strong) UIButton *connectButton;
  16. @end
  17. @implementation AiFreshNutritionScaleConnectionViewController
  18. - (void)viewDidLoad {
  19. [super viewDidLoad];
  20. // Do any additional setup after loading the view.
  21. self.view.backgroundColor = [UIColor whiteColor];
  22. [ELAiFreshNutritionScaleBleManager shareManager].nutritionScaleBleDelegate = self;
  23. [ELAiFreshNutritionScaleBleManager shareManager].delegate = self;
  24. [[ELAiFreshNutritionScaleBleManager shareManager] connectPeripheral:self.p];
  25. [self setupUIView];
  26. }
  27. - (void)viewWillDisappear:(BOOL)animated {
  28. [[ELAiFreshNutritionScaleBleManager 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. [[ELAiFreshNutritionScaleBleManager 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. [[ELAiFreshNutritionScaleBleManager shareManager] sendRequestUnitSupported];
  87. // read RSSI
  88. [self readRSSI];
  89. }
  90. break;
  91. case ELBluetoothStateFailedValidation: {
  92. self.title = @"Illegal equipment";
  93. }
  94. break;
  95. case ELBluetoothStateWillConnect:
  96. self.title = @"Connecting";
  97. break;
  98. default:
  99. break;
  100. }
  101. }
  102. /// 附近的设备
  103. /// @param devices 设备列表
  104. - (void)deviceBleReceiveDevices:(NSArray<ELPeripheralModel *> *)devices {
  105. for (ELPeripheralModel *model in devices) {
  106. if ([model.macAddress isEqualToString:self.p.macAddress]) {
  107. [[ELAiFreshNutritionScaleBleManager shareManager] connectPeripheral:model];
  108. }
  109. }
  110. }
  111. //获得支持单位列表
  112. - (void)supportWeightUnits:(NSArray *_Nullable)weightArray {
  113. NSLog(@"weightArray: %@", weightArray);
  114. [self addLog:@"获得支持单位列表"];
  115. }
  116. //称重数据
  117. - (void)aiFreshNutritionScaleBleDataModel:(ELAiFreshNutritionScaleDataModel *_Nonnull)model {
  118. NSLog(@"%s model:%@", __FUNCTION__, model);
  119. NSString *testData = [NSString stringWithFormat:@"weight:%@ weightPoint:%@ weightUnit:%@", @(model.weight), @(model.weightPoint), @(model.weightUnit)];
  120. [self addLog:testData];
  121. }
  122. //超载
  123. - (void)overload:(BOOL)status {
  124. [self addLog:[NSString stringWithFormat:@"超载: %d", status]];
  125. }
  126. //低电
  127. - (void)lowPower:(BOOL)status {
  128. [self addLog:[NSString stringWithFormat:@"低电: %d", status]];
  129. }
  130. //切换单位
  131. - (void)uintDidChange:(AiFreshNutritionScaleWeightUnit)unit {
  132. [self addLog:[NSString stringWithFormat:@"切换单位: %ld", (long)unit]];
  133. }
  134. //ble version
  135. - (void)firmwareVersion:(NSString *)version {
  136. NSLog(@"%s version:%@", __FUNCTION__, version);
  137. }
  138. - (void)peripheralDidReadRSSI:(nonnull NSNumber *)RSSI {
  139. NSLog(@"%s peripheralDidReadRSSI: %@", __FUNCTION__, RSSI);
  140. // main thread
  141. dispatch_async(dispatch_get_main_queue(), ^{
  142. [self addLog:[NSString stringWithFormat:@"peripheralDidReadRSSI: %@", RSSI]];
  143. });
  144. [self readRSSI];
  145. }
  146. - (void)readRSSI {
  147. // delay
  148. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(4.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  149. // Read RSSI
  150. [[ELAiFreshNutritionScaleBleManager shareManager] readRSSI];
  151. });
  152. }
  153. @end