iOS AILinkBleSDK - 蓝牙SDK
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

CoffeeScaleConnectionViewController.m 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. //
  2. // CoffeeScaleConnectionViewController.m
  3. // AILinkBleSDKSourceCode
  4. //
  5. // Created by LarryZhang on 2021/12/13.
  6. // Copyright © 2021 IOT. All rights reserved.
  7. //
  8. #import "CoffeeScaleConnectionViewController.h"
  9. #import "Masonry.h"
  10. #import <AILinkBleSDK/ELCoffeeScaleBleHeader.h>
  11. #import <AILinkBleSDK/ELCoffeeScaleBleManager.h>
  12. @interface CoffeeScaleConnectionViewController () <CoffeeScaleBleDelegate, ELBluetoothManagerDelegate>
  13. @property(nonatomic, strong) UITextView *textView;
  14. @property(nonatomic, copy) NSArray<NSNumber *> *units;
  15. @property(nonatomic, strong) UIButton *connectButton;
  16. @end
  17. @implementation CoffeeScaleConnectionViewController
  18. - (void)viewDidLoad {
  19. [super viewDidLoad];
  20. // Do any additional setup after loading the view.
  21. self.view.backgroundColor = [UIColor whiteColor];
  22. [ELCoffeeScaleBleManager shareManager].coffeeScaleDelegate = self;
  23. [ELCoffeeScaleBleManager shareManager].delegate = self;
  24. [[ELCoffeeScaleBleManager shareManager] connectPeripheral:self.p];
  25. [self setupUIView];
  26. }
  27. - (void)viewWillDisappear:(BOOL)animated {
  28. [[ELCoffeeScaleBleManager 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. [[ELCoffeeScaleBleManager 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. [[ELCoffeeScaleBleManager shareManager] getBluetoothInfoWithELInetGetCmdType:ELInetGetCmdTypeReadDeviceSupportUnit];
  87. //获取版本号
  88. [[ELCoffeeScaleBleManager shareManager] getBluetoothInfoWithELInetGetCmdType:ELInetGetCmdTypeGetBMVersion];
  89. //同步时间到设备
  90. [[ELCoffeeScaleBleManager shareManager] syncMCUNowDate];
  91. //切换到app 冲煮 模式
  92. [[ELCoffeeScaleBleManager shareManager] sendSwithAppMode:YES subMode:0];
  93. [self addLog:@"切换到app 冲煮 模式"];
  94. //切换到app 称重 模式
  95. // [[ELCoffeeScaleBleManager shareManager] sendSwithAppMode:YES subMode:1];
  96. // [self addLog:@"切换到app 称重 模式"];
  97. }
  98. break;
  99. case ELBluetoothStateFailedValidation: {
  100. self.title = @"Illegal equipment";
  101. }
  102. break;
  103. case ELBluetoothStateWillConnect:
  104. self.title = @"Connecting";
  105. break;
  106. default:
  107. break;
  108. }
  109. }
  110. /// 附近的设备
  111. /// @param devices 设备列表
  112. - (void)deviceBleReceiveDevices:(NSArray<ELPeripheralModel *> *)devices {
  113. for (ELPeripheralModel *model in devices) {
  114. if ([model.macAddress isEqualToString:self.p.macAddress]) {
  115. [[ELCoffeeScaleBleManager shareManager] connectPeripheral:model];
  116. }
  117. }
  118. }
  119. //获得支持单位列表
  120. - (void)supportWeightUnits:(NSArray *_Nullable)weightArray {
  121. NSLog(@"weightArray: %@", weightArray);
  122. [self addLog:@"获得支持单位列表"];
  123. }
  124. //正在连接中倒计时
  125. - (void)deviceBleCountDown:(NSInteger)count {
  126. NSLog(@"count: %@", @(count));
  127. [self addLog:@"正在连接中倒计时"];
  128. }
  129. /** 返回咖啡秤称量的数据模型 */
  130. - (void)coffeeScaleBleReceiveStatusDataModel:(ELCoffeeScaleBleDataModel *_Nonnull)model {
  131. NSLog(@"model.weightString: %@", model.weightString);
  132. [self addLog:[@"当前重量: " stringByAppendingFormat:@"%@", model.weightString]];
  133. }
  134. /** 返回设备电量 */
  135. - (void)coffeeScaleBleRechargeState:(CoffeeChargingType)type powerNumber:(NSInteger)power {
  136. NSLog(@"coffeeScaleBleRechargeState() type: %@ power: %@", @(type), @(power));
  137. [self addLog:[@"设备电量: " stringByAppendingFormat:@"type: %@ power: %@", @(type), @(power)]];
  138. }
  139. // 计时功能控制
  140. - (void)coffeeScaleBleReceiveTimerControl:(BOOL)countdown duration:(NSUInteger)duration control:(ELCoffeeScaleTimerControl)control {
  141. NSLog(@"coffeeScaleBleReceiveTimerControl() countdown: %@ duration: %@ control: %@", @(countdown), @(duration), @(control));
  142. [self addLog:[@"计时功能: " stringByAppendingFormat:@"countdown: %@ duration: %@ control: %@", @(countdown), @(duration), @(control)]];
  143. }
  144. @end