iOS AILinkBleSDK - 蓝牙SDK
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

FaceMaskConnectionViewController.m 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. //
  2. // FaceMaskConnectionViewController.m
  3. // AILinkBleSDKSourceCode
  4. //
  5. // Created by LarryZhang on 2021/12/13.
  6. // Copyright © 2021 IOT. All rights reserved.
  7. //
  8. #import "FaceMaskConnectionViewController.h"
  9. #import "Masonry.h"
  10. #import <AILinkBleSDK/ELFaceMaskBleHeader.h>
  11. #import <AILinkBleSDK/ELFaceMaskBleManager.h>
  12. @interface FaceMaskConnectionViewController () <FaceMaskBleDelegate, ELBluetoothManagerDelegate>
  13. @property(nonatomic, strong) UITextView *textView;
  14. @property(nonatomic, copy) NSArray<NSNumber *> *units;
  15. @property(nonatomic, strong) UIButton *connectButton;
  16. @end
  17. @implementation FaceMaskConnectionViewController
  18. - (void)viewDidLoad {
  19. [super viewDidLoad];
  20. // Do any additional setup after loading the view.
  21. self.view.backgroundColor = [UIColor whiteColor];
  22. [ELFaceMaskBleManager shareManager].faceMaskDelegate = self;
  23. [ELFaceMaskBleManager shareManager].delegate = self;
  24. [[ELFaceMaskBleManager shareManager] connectPeripheral:self.p];
  25. [self setupUIView];
  26. }
  27. - (void)viewWillDisappear:(BOOL)animated {
  28. [[ELFaceMaskBleManager 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. [[ELFaceMaskBleManager 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)faceMaskBleReceiveState:(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. [[ELFaceMaskBleManager shareManager] getBluetoothInfoWithELInetGetCmdType:ELInetGetCmdTypeReadDeviceSupportUnit];
  87. //获取版本号
  88. [[ELFaceMaskBleManager shareManager] getBluetoothInfoWithELInetGetCmdType:ELInetGetCmdTypeGetBMVersion];
  89. //同步时间到设备
  90. [[ELFaceMaskBleManager 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)faceMaskBleReceiveDevices:(NSArray<ELPeripheralModel *> *)devices {
  107. for (ELPeripheralModel *model in devices) {
  108. if ([model.macAddress isEqualToString:self.p.macAddress]) {
  109. [[ELFaceMaskBleManager shareManager] connectPeripheral:model];
  110. }
  111. }
  112. }
  113. - (void)faceMaskBleReceiveStatusDataModel:(ELFaceMaskBleDataModel *_Nonnull)model {
  114. NSString *str = [NSString stringWithFormat:@"空气质量指数:%@", @(model.index)];
  115. str = [str stringByAppendingFormat:@" 风扇状态:%@", @(model.fanStatus)];
  116. str = [str stringByAppendingFormat:@" 电池状态:%@", @(model.batteryStatus)];
  117. str = [str stringByAppendingFormat:@" 电池续航:%@", @(model.batteryLife)];
  118. str = [str stringByAppendingFormat:@" 呼吸频率:%@", @(model.breathRate)];
  119. str = [str stringByAppendingFormat:@" 呼吸状态:%@", @(model.breathStatus)];
  120. str = [str stringByAppendingFormat:@" 滤网的总工作时长:%@", @(model.workTime)];
  121. [self addLog:[NSString stringWithFormat:@"ELFaceMaskBleDataModel() %@", str]];
  122. }
  123. - (void)faceMaskReplaceSuccess:(BOOL)success {
  124. [self addLog:[NSString stringWithFormat:@"faceMaskReplaceSuccess() success:%@", @(success)]];
  125. }
  126. - (void)faceMaskSwitchFanResult:(FaceMaskFanControlResult)result {
  127. [self addLog:[NSString stringWithFormat:@"faceMaskSwitchFanResult() result:%@", @(result)]];
  128. }
  129. - (void)faceMaskPoweroffSuccess:(BOOL)success {
  130. [self addLog:[NSString stringWithFormat:@"faceMaskPoweroffSuccess() success:%@", @(success)]];
  131. }
  132. @end