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.

BloodSugarConnectionViewController.m 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. //
  2. // BloodSugarConnectionViewController.m
  3. // AILinkBleSDKSourceCode
  4. //
  5. // Created by cliCk on 2021/1/28.
  6. // Copyright © 2021 IOT. All rights reserved.
  7. //
  8. #import "BloodSugarConnectionViewController.h"
  9. #import "Masonry.h"
  10. #import <AILinkBleSDK/ELBloodSugarBleHeader.h>
  11. #import <AILinkBleSDK/ELBloodSugarBleManager.h>
  12. @interface BloodSugarConnectionViewController () <BloodSugarBleDelegate, ELBluetoothManagerDelegate>
  13. @property (nonatomic, strong) UITextView *textView;
  14. @property (nonatomic, copy) NSArray<NSNumber *> *units;
  15. @property (nonatomic, strong) UIButton *connectButton;
  16. @end
  17. @implementation BloodSugarConnectionViewController
  18. - (void)viewDidLoad {
  19. [super viewDidLoad];
  20. // Do any additional setup after loading the view.
  21. self.view.backgroundColor = [UIColor whiteColor];
  22. [ELBloodSugarBleManager shareManager].bloodSugarDelegate = self;
  23. [ELBloodSugarBleManager shareManager].delegate = self;
  24. [[ELBloodSugarBleManager shareManager] connectPeripheral:self.p];
  25. [self setupUIView];
  26. }
  27. - (void)viewWillDisappear:(BOOL)animated {
  28. [[ELBloodSugarBleManager 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. [[ELBloodSugarBleManager 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)bloodSugarBleReceiveState:(ELBluetoothState)state {
  58. switch (state) {
  59. case ELBluetoothStateUnavailable:
  60. {
  61. self.title = @"Please open the bluetooth";
  62. }
  63. break;
  64. case ELBluetoothStateAvailable:
  65. {
  66. self.title = @"Bluetooth is open";
  67. }
  68. break;
  69. case ELBluetoothStateScaning:
  70. {
  71. self.title = @"Scaning";
  72. }
  73. break;
  74. case ELBluetoothStateConnectFail:
  75. {
  76. self.title = @"Connect fail";
  77. }
  78. break;
  79. case ELBluetoothStateDidDisconnect:
  80. {
  81. self.title = @"Disconnected";
  82. self.connectButton.hidden = NO;
  83. }
  84. break;
  85. case ELBluetoothStateDidValidationPass:
  86. {
  87. self.connectButton.hidden = YES;
  88. self.title = @"Connected";
  89. //连接成功,获取单位
  90. [[ELBloodSugarBleManager shareManager] getBluetoothInfoWithELInetGetCmdType:ELInetGetCmdTypeReadDeviceSupportUnit];
  91. //获取版本号
  92. [[ELBloodSugarBleManager shareManager] getBluetoothInfoWithELInetGetCmdType:ELInetGetCmdTypeGetBMVersion];
  93. //同步时间到设备
  94. [[ELBloodSugarBleManager shareManager] syncMCUNowDate];
  95. //查询设备状态
  96. [[ELBloodSugarBleManager shareManager] bloodSugarQueryDeviceStatus];
  97. }
  98. break;
  99. case ELBluetoothStateFailedValidation:
  100. {
  101. self.title = @"Illegal equipment";
  102. }
  103. break;
  104. case ELBluetoothStateWillConnect:
  105. self.title = @"Connecting";
  106. break;
  107. default:
  108. break;
  109. }
  110. }
  111. - (void)bloodSugarBleReceiveDevices:(NSArray<ELPeripheralModel *> *)devices {
  112. for (ELPeripheralModel *model in devices) {
  113. if ([model.macAddress isEqualToString:self.p.macAddress]) {
  114. [[ELBloodSugarBleManager shareManager] connectPeripheral:model];
  115. }
  116. }
  117. }
  118. -(void)bloodSugarBleReceiveDeviceStatus:(BloodSugarDeviceStatus)status{
  119. if (status == BloodSugarDeviceStatusNoStatus) {
  120. // self.measureView.status = BloodSugarMeasureViewStatusDefault;
  121. } else if (status == BloodSugarDeviceStatusTestWaiting) {
  122. [self addLog:@"请插入试纸"];
  123. } else if (status == BloodSugarDeviceStatusSamplingWaiting){
  124. [self addLog:@"获取血样中..."];
  125. } else if (status == BloodSugarDeviceStatusAnalysising){
  126. [self addLog:@"血样分析中..."];
  127. } else if (status == BloodSugarDeviceStatusTestComplete){
  128. [self addLog:@"测量完成"];
  129. }
  130. }
  131. - (void)bloodSugarBleReceiveTestData:(BloodSugarDataStruct *)data {
  132. NSString *unit = [NSString string];
  133. if (data.unit == ELDeviceBloodSugarUnit_mmol_L) {
  134. unit = @"mmol/L";
  135. }else {
  136. unit = @"mg/dl";
  137. }
  138. [self addLog:[NSString stringWithFormat:@"测量结果 数值 : %d, 单位 : %@ point : %d",data.value,unit,data.point]];
  139. }
  140. - (void)bloodSugarBleReceiveErrorCode:(BloodSugarErrorCode)errorCode {
  141. NSString *errorMsg = @"测量失败";
  142. switch (errorCode) {
  143. case BloodSugarErrorCodeLowBattery:
  144. {
  145. errorMsg = @"电量不足";
  146. }
  147. break;
  148. case BloodSugarErrorCodeUsedTestPaper: {
  149. errorMsg = @"使用了已使用过的试纸";
  150. break;
  151. }
  152. case BloodSugarErrorCodeTempHigh: {
  153. errorMsg = @"环境温度超出使用范围";
  154. break;
  155. }
  156. case BloodSugarErrorCodeCancelTest: {
  157. errorMsg = @"试纸施加血样后测试未完成,被退出试纸";
  158. break;
  159. }
  160. case BloodSugarErrorCodePassError: {
  161. errorMsg = @"机器自检未通过";
  162. break;
  163. }
  164. case BloodSugarErrorCodeLow: {
  165. errorMsg = @"测量结果过低,超出测量范围";
  166. break;
  167. }
  168. case BloodSugarErrorCodeHigh: {
  169. errorMsg = @"测量结果过高,超出测量范围";
  170. break;
  171. }
  172. }
  173. [self addLog:[NSString stringWithFormat:@"测量结果 :%@",errorMsg]];
  174. }
  175. - (void)bloodSugarBleBackManufactureData:(NSData *)data {
  176. [self addLog:[NSString stringWithFormat:@"收到的原始数据 : %@",data]];
  177. }
  178. @end