iOS AILinkBleSDK - 蓝牙SDK
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

FoodThermometerConnectionViewController.m 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. //
  2. // FoodThermometerConnectionViewController.m
  3. // AILinkBleSDKSourceCode
  4. //
  5. // Created by LarryZhang on 2021/12/13.
  6. // Copyright © 2021 IOT. All rights reserved.
  7. //
  8. #import "FoodThermometerConnectionViewController.h"
  9. #import "Masonry.h"
  10. #import <AILinkBleSDK/ELFoodThermometerHead.h>
  11. #import <AILinkBleSDK/ELFoodThermometerBleManager.h>
  12. @interface FoodThermometerConnectionViewController () <FoodThermometerBleDelegate, ELBluetoothManagerDelegate>
  13. @property(nonatomic, strong) UITextView *textView;
  14. @property(nonatomic, copy) NSArray<NSNumber *> *units;
  15. @property(nonatomic, strong) UIButton *connectButton;
  16. @end
  17. @implementation FoodThermometerConnectionViewController
  18. - (void)viewDidLoad {
  19. [super viewDidLoad];
  20. // Do any additional setup after loading the view.
  21. self.view.backgroundColor = [UIColor whiteColor];
  22. [ELFoodThermometerBleManager shareManager].foodThermometerBleDelegate = self;
  23. [ELFoodThermometerBleManager shareManager].delegate = self;
  24. [[ELFoodThermometerBleManager shareManager] connectPeripheral:self.p];
  25. [self setupUIView];
  26. }
  27. - (void)viewWillDisappear:(BOOL)animated {
  28. [[ELFoodThermometerBleManager 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. [[ELFoodThermometerBleManager 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. - (void)deviceBleReceiveState:(ELBluetoothState)state {
  58. switch (state) {
  59. case ELBluetoothStateUnavailable: {
  60. self.title = @"Please open the bluetooth";
  61. }
  62. break;
  63. case ELBluetoothStateAvailable: {
  64. self.title = @"Bluetooth is open";
  65. }
  66. break;
  67. case ELBluetoothStateScaning: {
  68. self.title = @"Scaning";
  69. }
  70. break;
  71. case ELBluetoothStateConnectFail: {
  72. self.title = @"Connect fail";
  73. }
  74. break;
  75. case ELBluetoothStateDidDisconnect: {
  76. self.title = @"Disconnected";
  77. self.connectButton.hidden = NO;
  78. }
  79. break;
  80. case ELBluetoothStateDidValidationPass: {
  81. self.connectButton.hidden = YES;
  82. self.title = @"Connected";
  83. //连接成功,获取单位
  84. [[ELFoodThermometerBleManager shareManager] getBluetoothInfoWithELInetGetCmdType:ELInetGetCmdTypeReadDeviceSupportUnit];
  85. //获取版本号
  86. [[ELFoodThermometerBleManager shareManager] getBluetoothInfoWithELInetGetCmdType:ELInetGetCmdTypeGetBMVersion];
  87. //同步时间到设备
  88. [[ELFoodThermometerBleManager shareManager] syncMCUNowDate];
  89. //查询设备状态
  90. [[ELFoodThermometerBleManager shareManager] checkDeviceInfo];
  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. - (void)deviceBleReceiveDevices:(NSArray<ELPeripheralModel *> *)devices {
  105. for (ELPeripheralModel *model in devices) {
  106. if ([model.macAddress isEqualToString:self.p.macAddress]) {
  107. [[ELFoodThermometerBleManager shareManager] connectPeripheral:model];
  108. }
  109. }
  110. }
  111. //设备返回基础信息
  112. - (void)foodThermometerBasicInfo:(BasicInfo)basicInfo {
  113. NSLog(@"foodThermometerBasicInfo() basicInfo.batteryLevel:%@ basicInfo.charging:%@", @(basicInfo.batteryLevel), @(basicInfo.charging));
  114. [self addLog:[@"设备返回基础信息" stringByAppendingFormat:@"basicInfo.batteryLevel:%@ basicInfo.charging:%@", @(basicInfo.batteryLevel), @(basicInfo.charging)]];
  115. }
  116. //设备返回数据状态
  117. - (void)foodThermometerProbeStatus:(ProbeStatus)probeStatus {
  118. NSLog(@"foodThermometerProbeStatus() probeStatus.index:%@ probeStatus.internalTemperature:%@", @(probeStatus.index), @(probeStatus.internalRawTemperature));
  119. [self addLog:[@"设备返回数据状态" stringByAppendingFormat:@"probeStatus.index:%@ probeStatus.internalTemperature:%@", @(probeStatus.index), @(probeStatus.internalRawTemperature)]];
  120. }
  121. //设备设置温度
  122. - (void)foodThermometerSwitchTemperatureUnit:(ELDeviceTemperatureUnit)unit {
  123. NSLog(@"foodThermometerSwitchTemperatureUnit() unit: %@", @(unit));
  124. [self addLog:[@"设备设置温度" stringByAppendingFormat:@"unit: %@", @(unit)]];
  125. }
  126. @end