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.

MeatProbeBoxVC.m 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. //
  2. // MeatProbeBoxVC.m
  3. // AILinkBleSDK_Example
  4. //
  5. // Created by LarryZhang on 2023/3/7.
  6. // Copyright © 2023 zhengzida. All rights reserved.
  7. //
  8. #import "MeatProbeBoxVC.h"
  9. #import <AILinkBleSDK/ELAILinkBleManager.h>
  10. #import <AILinkBleSDK/NSData+AILinkBle.h>
  11. #import "ELAILinkBleManager+MeatProbeBox.h"
  12. #import "ELMeatProbeBoxBleParser.h"
  13. @interface MeatProbeBoxVC () <ELAILinkBleManagerDelegate, ELMeatProbeBoxBleParserDelegate>
  14. @property (weak, nonatomic) IBOutlet UILabel *bleStatusLabel;
  15. @property (weak, nonatomic) IBOutlet UITextView *logTextView;
  16. @property (nonatomic, strong) ELAILinkBleManager *bleManager;
  17. @property (nonatomic, assign) NELBleManagerConnectState bleConnectState;
  18. @property (nonatomic, strong) ELMeatProbeBoxBleParser *bleParser;
  19. @property (nonatomic, strong) NSMutableSet *probeList;
  20. @property (nonatomic, strong) ELMeatProbeBoxBleBoxStatusModel *boxStatus;
  21. @end
  22. @implementation MeatProbeBoxVC
  23. - (void)viewDidLoad {
  24. [super viewDidLoad];
  25. // Do any additional setup after loading the view from its nib.
  26. self.probeList = [NSMutableSet set];
  27. [self initBle];
  28. }
  29. - (void)dealloc {
  30. [self deinitBle];
  31. }
  32. #pragma mark - Action
  33. - (IBAction)customDataRequestAction:(id)sender {
  34. for (NSData *mac in self.probeList) {
  35. NSData *payload = [self.bleManager meatProbeBoxRequestCustomDataWithMac:mac];
  36. NSString *log = [NSString stringWithFormat:@"发: payload: %@", payload];
  37. [self addLog:log];
  38. }
  39. }
  40. - (IBAction)customDataSendAction:(id)sender {
  41. for (NSData *mac in self.probeList) {
  42. NSData *payload = [self.bleManager meatProbeBoxSendCustomData:[self coustumData] withMac:mac];
  43. NSString *log = [NSString stringWithFormat:@"发: payload: %@", payload];
  44. [self addLog:log];
  45. }
  46. }
  47. - (IBAction)customDataClearAction:(id)sender {
  48. for (NSData *mac in self.probeList) {
  49. NSData *payload = [self.bleManager meatProbeBoxClearCustomDataWithMac:mac];
  50. NSString *log = [NSString stringWithFormat:@"发: payload: %@", payload];
  51. [self addLog:log];
  52. }
  53. }
  54. - (IBAction)switchUnitAction:(id)sender {
  55. if (self.boxStatus.unit == 1) {
  56. NSData *payload = [self.bleManager meatProbeBoxSwitchUint:0];
  57. NSString *log = [NSString stringWithFormat:@"发: payload: %@ -- 切换单位 to C", payload];
  58. [self addLog:log];
  59. } else {
  60. NSData *payload = [self.bleManager meatProbeBoxSwitchUint:1];
  61. NSString *log = [NSString stringWithFormat:@"发: payload: %@ -- 切换单位 to F", payload];
  62. [self addLog:log];
  63. }
  64. }
  65. - (IBAction)clearAction:(id)sender {
  66. self.logTextView.text = @"";
  67. }
  68. - (IBAction)shareLogAction:(id)sender {
  69. }
  70. - (NSData *)coustumData {
  71. //自定义数据
  72. //*******要验证设备必须支持128字节payload
  73. Byte bytes[128];
  74. memset(bytes, 0, sizeof(bytes));
  75. for (int i=0; i<sizeof(bytes); i++) {
  76. bytes[i] = 0x80 + i;
  77. }
  78. NSData *data = [NSData dataWithBytes:bytes length:sizeof(bytes)];
  79. NSLog(@"meatProbeAction() data:%@", data);
  80. return data;
  81. }
  82. #pragma mark - ELAILinkBleManagerDelegate
  83. - (void)initBle {
  84. self.bleManager = [[ELAILinkBleManager alloc] init];
  85. self.bleManager.ailinkDelegate = self;
  86. self.bleParser = [ELMeatProbeBoxBleParser new];
  87. self.bleParser.delegate = self;
  88. }
  89. - (void)scanBle {
  90. [self.bleManager scanFilterWithCidArray:@[@(self.per.cid)]];
  91. }
  92. - (void)connect:(ELAILinkPeripheral *)peripheral {
  93. [self.bleManager connectAILinkPeripheral:peripheral];
  94. [self updateBleStatusView:NELBleManagerConnectStateConnecting];
  95. }
  96. - (void)deinitBle {
  97. [self.bleManager stopScan];
  98. self.bleManager.ailinkDelegate = nil;
  99. [self.bleManager disconnectPeripheral];
  100. }
  101. - (void)updateBleStatusView:(NELBleManagerConnectState)state {
  102. if (self.bleManager.central.state == CBManagerStatePoweredOff) {
  103. self.bleStatusLabel.text = @"蓝牙关闭";
  104. [self addLog:self.bleStatusLabel.text];
  105. return;
  106. }
  107. switch (state) {
  108. case NELBleManagerConnectStateDisconnected:
  109. self.bleStatusLabel.text = @"断开连接";
  110. break;
  111. case NELBleManagerConnectStateFailed:
  112. case NELBleManagerConnectStateFailedValidation:
  113. self.bleStatusLabel.text = @"连接失败";
  114. break;
  115. case NELBleManagerConnectStateConnecting:
  116. self.bleStatusLabel.text = @"正在连接...";
  117. break;
  118. case NELBleManagerConnectStatePassed:
  119. self.bleStatusLabel.text = @"连接成功";
  120. [self requestDeviceData];
  121. // read RSSI
  122. // [self readRSSI];
  123. break;
  124. case NELBleManagerConnectStateCentralScanning:
  125. self.bleStatusLabel.text = @"正在扫描...";
  126. break;
  127. default:
  128. break;
  129. }
  130. [self addLog:self.bleStatusLabel.text];
  131. }
  132. - (void)requestDeviceData {
  133. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 1.0), dispatch_get_main_queue(), ^{
  134. // //APP获取设备功能列表
  135. // [self.bleManager airDetectorRequestDeviceFunctions];
  136. // //APP获取设备状态 //0x03 CMD:获取设备状态
  137. // [self.bleManager airDetectorRequestDeviceStatus];
  138. });
  139. }
  140. // 设备状态变更
  141. - (void)managerDidUpdateState:(CBCentralManager *)central {
  142. NSLog(@"%s state:%@", __func__, @(central.state));
  143. if (central.state == CBManagerStatePoweredOn) {
  144. [self scanBle];
  145. } else if (central.state == CBManagerStatePoweredOff) {
  146. self.bleConnectState = NELBleManagerConnectStateCentralPowerOff;
  147. [self updateBleStatusView:NELBleManagerConnectStateCentralPowerOff];
  148. }
  149. }
  150. - (void)managerScanState:(BOOL)scanning {
  151. NSLog(@"%s scanning:%@", __func__, @(scanning));
  152. if (scanning) {
  153. [self updateBleStatusView:NELBleManagerConnectStateCentralScanning];
  154. }
  155. }
  156. // 扫描到设备
  157. - (void)managerDidDiscoverPeripheral:(ELAILinkPeripheral *)peripheral {
  158. NSLog(@"managerDidDiscoverPeripheral cid:%02x vid:%02x pid:%02x mac:%@", peripheral.cid, peripheral.vid, peripheral.pid, peripheral.macAddressString);
  159. if ([peripheral.macAddressString isEqualToString:self.per.macAddressString]) {
  160. [self.bleManager stopScan];
  161. [self connect:peripheral];
  162. }
  163. }
  164. - (void)managerDidUpdateConnect:(NELBleManagerConnectState)state {
  165. NSLog(@"%s NELBleManagerConnectState:%@", __func__, @(state));
  166. [self updateBleStatusView:state];
  167. }
  168. //A7数据
  169. - (void)aiLinkBleReceiveA7Data:(NSData *)payload {
  170. NSLog(@"%s #### payload:%@", __func__, payload.elHexString);
  171. NSString *log = [NSString stringWithFormat:@"收: payload: %@", payload.elHexString];
  172. [self addLog:log];
  173. [self.bleParser parseA7Payload:payload];
  174. }
  175. //A6数据
  176. - (void)aiLinkBleReceiveA6Data:(NSData *)packet {
  177. NSLog(@"%s ##### packet:%@", __func__, packet.elHexString);
  178. Byte *bytes = (Byte *)packet.bytes;
  179. Byte cmd = bytes[2];
  180. if (cmd == ELInetGetCmdTypeGetBatteryState) {
  181. int power = self.bleManager.battery.power;
  182. ELBatteryChargingState state = self.bleManager.battery.state;
  183. NSLog(@"##### state: %lu power: %d", (unsigned long)state, power);
  184. }
  185. if (cmd == ELInetGetCmdTypeGetBMVersion) {
  186. NSString *bmVersion = self.bleManager.bmVersion;
  187. NSLog(@"##### bmVersion: %@", bmVersion);
  188. }
  189. if (cmd == ELInetGetCmdTypeGetBMVersionPro) {
  190. NSString *bmVersionPro = self.bleManager.bmVersionPro;
  191. NSLog(@"##### bmVersionPro: %@", bmVersionPro);
  192. }
  193. }
  194. #pragma mark - ELMeatProbeBoxBleParserDelegate
  195. //MCU 上报设备状态数据(Type:02)
  196. - (void)meatProbeBoxBleParser:(ELMeatProbeBoxBleParser *)bleParser boxStatus:(ELMeatProbeBoxBleBoxStatusModel *)boxStatus probeStatus:(ELMeatProbeBoxBleProbeStatusModel *)probeStatus offline:(BOOL)offline {
  197. NSLog(@"##### boxStatus(): %@ probeStatus(): %@", boxStatus, probeStatus);
  198. self.boxStatus = boxStatus;
  199. NSString *log = [NSString stringWithFormat:@"boxStatus: { probeMaxCount:%hhu, probeCount:%hhu, batteryStatus:%hhu batteryPercent:%hhu, unit:%ld }", boxStatus.probeMaxCount, boxStatus.probeCount, boxStatus.batteryStatus, boxStatus.batteryPercent, boxStatus.unit];
  200. [self addLog:log];
  201. NSString *log2 = [NSString stringWithFormat:@"probeStatus: { index:%hhu, macString:%@, internalTemperature:%ld, ambientTemperature:%ld, batteryPercent:%hhu }", probeStatus.index, probeStatus.macString, probeStatus.internalTemperature, probeStatus.ambientTemperature, probeStatus.batteryPercent];
  202. [self addLog:log2];
  203. if (![self.probeList containsObject:probeStatus.macData]) {
  204. [self.probeList addObject:probeStatus.macData];
  205. NSData *payload = [self.bleManager meatProbeBoxRequestCustomDataWithMac:probeStatus.macData];
  206. NSString *log = [NSString stringWithFormat:@"发: payload: %@", payload];
  207. [self addLog:log];
  208. }
  209. }
  210. //设置、获取设备参数数据(Type:03)
  211. - (void)meatProbeBoxBleParser:(ELMeatProbeBoxBleParser *)bleParser state:(BOOL)state mac:(NSData *)mac customData:(NSData *)customData {
  212. NSLog(@"##### state:%d mac:%@ customData():%@", state, mac, customData);
  213. NSLog(@"");
  214. }
  215. //切换单位(Type:04)
  216. - (void)meatProbeBoxBleParser:(ELMeatProbeBoxBleParser *)bleParser unit:(ELDeviceTemperatureUnit)unit {
  217. NSLog(@"##### unit(): %@", @(unit));
  218. NSLog(@"");
  219. }
  220. - (void)peripheralDidReadRSSI:(nonnull NSNumber *)RSSI {
  221. NSLog(@"%s peripheralDidReadRSSI: %@", __FUNCTION__, RSSI);
  222. // main thread
  223. dispatch_async(dispatch_get_main_queue(), ^{
  224. [self addLog:[NSString stringWithFormat:@"peripheralDidReadRSSI: %@", RSSI]];
  225. });
  226. [self readRSSI];
  227. }
  228. - (void)readRSSI {
  229. // delay
  230. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(4.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  231. // Read RSSI
  232. [self.bleManager readRSSI];
  233. });
  234. }
  235. #pragma mark - addLog
  236. - (void)addLog:(NSString *)log {
  237. [self addLog:log newline:YES];
  238. }
  239. - (void)addWithoutNewlineLog:(NSString *)log {
  240. [self addLog:log newline:NO];
  241. }
  242. - (void)addLog:(NSString *)log newline:(BOOL)newline {
  243. NSDateFormatter *format = [[NSDateFormatter alloc] init];
  244. format.dateFormat = @"HH:mm:ss.SSS";
  245. NSString *time = [format stringFromDate:[NSDate date]];
  246. if (newline) {
  247. self.logTextView.text = [self.logTextView.text stringByAppendingFormat:@"%@ %@\n", time, log];
  248. } else {
  249. self.logTextView.text = [self.logTextView.text stringByAppendingFormat:@"%@ %@", time, log];
  250. }
  251. [self.logTextView scrollRangeToVisible:NSMakeRange(self.logTextView.text.length, 1)];
  252. }
  253. @end