iOS AILinkBleSDK - 蓝牙SDK
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

AiFreshNutritionScaleConnectionViewController.m 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. }
  88. break;
  89. case ELBluetoothStateFailedValidation: {
  90. self.title = @"Illegal equipment";
  91. }
  92. break;
  93. case ELBluetoothStateWillConnect:
  94. self.title = @"Connecting";
  95. break;
  96. default:
  97. break;
  98. }
  99. }
  100. /// 附近的设备
  101. /// @param devices 设备列表
  102. - (void)deviceBleReceiveDevices:(NSArray<ELPeripheralModel *> *)devices {
  103. for (ELPeripheralModel *model in devices) {
  104. if ([model.macAddress isEqualToString:self.p.macAddress]) {
  105. [[ELAiFreshNutritionScaleBleManager shareManager] connectPeripheral:model];
  106. }
  107. }
  108. }
  109. //获得支持单位列表
  110. - (void)supportWeightUnits:(NSArray *_Nullable)weightArray {
  111. NSLog(@"weightArray: %@", weightArray);
  112. [self addLog:@"获得支持单位列表"];
  113. }
  114. //称重数据
  115. - (void)aiFreshNutritionScaleBleDataModel:(ELAiFreshNutritionScaleDataModel *_Nonnull)model {
  116. NSLog(@"%s model:%@", __FUNCTION__, model);
  117. NSString *testData = [NSString stringWithFormat:@"weight:%@ weightPoint:%@ weightUnit:%@", @(model.weight), @(model.weightPoint), @(model.weightUnit)];
  118. [self addLog:testData];
  119. }
  120. //超载
  121. - (void)overload:(BOOL)status {
  122. [self addLog:[NSString stringWithFormat:@"超载: %d", status]];
  123. }
  124. //低电
  125. - (void)lowPower:(BOOL)status {
  126. [self addLog:[NSString stringWithFormat:@"低电: %d", status]];
  127. }
  128. //切换单位
  129. - (void)uintDidChange:(AiFreshNutritionScaleWeightUnit)unit {
  130. [self addLog:[NSString stringWithFormat:@"切换单位: %d", unit]];
  131. }
  132. //ble version
  133. - (void)firmwareVersion:(NSString *)version {
  134. NSLog(@"%s version:%@", __FUNCTION__, version);
  135. }
  136. @end