iOS AILinkBleSDK - 蓝牙SDK
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

AilinkSuperConnectViewController.m 8.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. //
  2. // AilinkSuperConnectViewController.m
  3. // AILinkBleSDKSourceCode
  4. //
  5. // Created by iot_user on 2020/4/7.
  6. // Copyright © 2020 IOT. All rights reserved.
  7. //
  8. #import "AilinkSuperConnectViewController.h"
  9. #import <AILinkBleSDK/ELBluetoothManager.h>
  10. #import "Masonry.h"
  11. //#import <AILinkBleSDK/ELBluetoothManager+Settings.h>
  12. @interface AilinkSuperConnectViewController ()<ELBluetoothManagerDelegate>
  13. @property (nonatomic, strong) UITextView *textView;
  14. @end
  15. @implementation AilinkSuperConnectViewController
  16. - (void)viewDidLoad {
  17. [super viewDidLoad];
  18. self.view.backgroundColor = [UIColor whiteColor];
  19. [ELBluetoothManager shareManager].delegate = self;
  20. [[ELBluetoothManager shareManager] connectPeripheral:self.p];
  21. [self setupUIView];
  22. }
  23. -(void)viewWillDisappear:(BOOL)animated{
  24. [super viewWillDisappear:animated];
  25. [[ELBluetoothManager shareManager] disconnectPeripheral];
  26. }
  27. -(void)addLog:(NSString *)log{
  28. self.textView.text = [NSString stringWithFormat:@"%@\n%@",log,self.textView.text];
  29. }
  30. -(void)buttonAction:(UIButton *)sender{
  31. if ([ELBluetoothManager shareManager].state != ELBluetoothStateDidValidationPass) {
  32. [self addLog:@"Disconnected"];
  33. return;
  34. }
  35. [self addLog:sender.titleLabel.text];
  36. NSInteger tag = sender.tag;
  37. if (tag == 1) {
  38. //getBluetoothInfoWithELInetGetCmdType是获取设备信息的方法
  39. [[ELBluetoothManager shareManager] getBluetoothInfoWithELInetGetCmdType:(ELInetGetCmdTypeGetBMVersion)];
  40. }else if (tag == 2){
  41. [[ELBluetoothManager shareManager] getBluetoothInfoWithELInetGetCmdType:(ELInetGetCmdTypeGetC_V_P_ID)];
  42. }else if (tag == 3){
  43. [[ELBluetoothManager shareManager] getBluetoothInfoWithELInetGetCmdType:(ELInetGetCmdTypeGetName)];
  44. }else if (tag == 4){
  45. //[[ELBluetoothManager shareManager] setBluetoothName:@"AILink"];
  46. }else if (tag == 5){
  47. //MARK:发送透传数据
  48. Byte b[5] = {0x0A,0x00,0x01,0x02,0xA0};
  49. NSData *data = [[NSData alloc] initWithBytes:b length:5];
  50. [self addLog:data.description];
  51. [[ELBluetoothManager shareManager] sendData:data];
  52. }
  53. }
  54. -(void)setupUIView{
  55. UIButton *button1 = [UIButton buttonWithType:(UIButtonTypeCustom)];
  56. [button1 setTitle:@"Get BM Version" forState:(UIControlStateNormal)];
  57. button1.tag = 1;
  58. button1.titleLabel.adjustsFontSizeToFitWidth = YES;
  59. button1.titleLabel.numberOfLines = 2;
  60. button1.backgroundColor = [UIColor blackColor];
  61. [button1 addTarget:self action:@selector(buttonAction:) forControlEvents:(UIControlEventTouchUpInside)];
  62. [self.view addSubview:button1];
  63. [button1 mas_makeConstraints:^(MASConstraintMaker *make) {
  64. make.left.mas_equalTo(10);
  65. make.top.mas_equalTo(80);
  66. make.width.mas_equalTo(100);
  67. make.height.mas_equalTo(40);
  68. }];
  69. UIButton *button2 = [UIButton buttonWithType:(UIButtonTypeCustom)];
  70. [button2 setTitle:@"Get C V P ID" forState:(UIControlStateNormal)];
  71. button2.tag = 2;
  72. button2.titleLabel.adjustsFontSizeToFitWidth = YES;
  73. button2.titleLabel.numberOfLines = 2;
  74. button2.backgroundColor = [UIColor blackColor];
  75. [button2 addTarget:self action:@selector(buttonAction:) forControlEvents:(UIControlEventTouchUpInside)];
  76. [self.view addSubview:button2];
  77. [button2 mas_makeConstraints:^(MASConstraintMaker *make) {
  78. make.left.equalTo(button1.mas_right).mas_offset(10);
  79. make.top.mas_equalTo(80);
  80. make.width.mas_equalTo(100);
  81. make.height.mas_equalTo(40);
  82. }];
  83. UIButton *button3 = [UIButton buttonWithType:(UIButtonTypeCustom)];
  84. [button3 setTitle:@"Get Name" forState:(UIControlStateNormal)];
  85. button3.tag = 3;
  86. button3.titleLabel.adjustsFontSizeToFitWidth = YES;
  87. button3.titleLabel.numberOfLines = 2;
  88. button3.backgroundColor = [UIColor blackColor];
  89. [button3 addTarget:self action:@selector(buttonAction:) forControlEvents:(UIControlEventTouchUpInside)];
  90. [self.view addSubview:button3];
  91. [button3 mas_makeConstraints:^(MASConstraintMaker *make) {
  92. make.left.equalTo(button2.mas_right).mas_offset(10);
  93. make.top.mas_equalTo(80);
  94. make.width.mas_equalTo(100);
  95. make.height.mas_equalTo(40);
  96. }];
  97. //
  98. UIButton *button4 = [UIButton buttonWithType:(UIButtonTypeCustom)];
  99. [button4 setTitle:@"Set Name" forState:(UIControlStateNormal)];
  100. button4.tag = 4;
  101. button4.titleLabel.adjustsFontSizeToFitWidth = YES;
  102. button4.titleLabel.numberOfLines = 2;
  103. button4.backgroundColor = [UIColor blackColor];
  104. [button4 addTarget:self action:@selector(buttonAction:) forControlEvents:(UIControlEventTouchUpInside)];
  105. [self.view addSubview:button4];
  106. [button4 mas_makeConstraints:^(MASConstraintMaker *make) {
  107. make.left.mas_equalTo(10);
  108. make.top.equalTo(button1.mas_bottom).mas_offset(10);
  109. make.width.mas_equalTo(100);
  110. make.height.mas_equalTo(40);
  111. }];
  112. //
  113. UIButton *button5 = [UIButton buttonWithType:(UIButtonTypeCustom)];
  114. [button5 setTitle:@"Send Data" forState:(UIControlStateNormal)];
  115. button5.tag = 5;
  116. button5.titleLabel.adjustsFontSizeToFitWidth = YES;
  117. button5.titleLabel.numberOfLines = 2;
  118. button5.backgroundColor = [UIColor blackColor];
  119. [button5 addTarget:self action:@selector(buttonAction:) forControlEvents:(UIControlEventTouchUpInside)];
  120. [self.view addSubview:button5];
  121. [button5 mas_makeConstraints:^(MASConstraintMaker *make) {
  122. make.left.equalTo(button4.mas_right).mas_offset(10);
  123. make.top.equalTo(button1.mas_bottom).mas_offset(10);
  124. make.width.mas_equalTo(100);
  125. make.height.mas_equalTo(40);
  126. }];
  127. //
  128. self.textView = [[UITextView alloc] init];
  129. self.textView.backgroundColor = [UIColor blackColor];
  130. self.textView.text = @"Log";
  131. self.textView.textColor = [UIColor redColor];
  132. [self.view addSubview:self.textView];
  133. [self.textView mas_makeConstraints:^(MASConstraintMaker *make) {
  134. make.left.mas_equalTo(10);
  135. make.right.bottom.mas_equalTo(-10);
  136. make.height.mas_equalTo(200);
  137. }];
  138. }
  139. -(void)bluetoothManagerReceiceResponseType:(ELInetSetCmdType)type result:(ELSetBluetoothResponseType)result{
  140. if (type == ELInetSetCmdTypeSetName) {
  141. if (result == ELSetBluetoothResponseTypeSuccess) {
  142. [self addLog:@"Set name Success "];
  143. }else if (result == ELSetBluetoothResponseTypeFailure){
  144. [self addLog:@"Set name failure "];
  145. }else if (result == ELSetBluetoothResponseTypeNoSupport){
  146. [self addLog:@"Set name unsupport "];
  147. }
  148. }
  149. }
  150. -(void)bluetoothManagerReceiceName:(NSString *)name{
  151. [self addLog:name];
  152. }
  153. -(void)bluetoothManagerReceiveDID:(struct ELDIDStruct)did{
  154. [self addLog:[NSString stringWithFormat:@"CID = %d,VID = %d,PID = %d",did.deviceType,did.vendorID,did.productID]];
  155. }
  156. -(void)bluetoothManagerReceiveBMVersion:(NSString *)bmVersion{
  157. [self addLog:bmVersion];
  158. }
  159. //MARK:接收透传数据(不符合AILink协议的数据,需要做透传的客户可在此根据自己的协议解析数据,透传数据不可以"A6"或"A7"开头)
  160. -(void)bluetoothManagerReceivePassData:(NSData *)data{
  161. NSLog(@"bluetoothManagerReceivePassData = %@",data.description);
  162. [self addLog:[NSString stringWithFormat:@"Penetrate:%@",data.description]];
  163. }
  164. //MARK:获取设备支持的单位
  165. -(void)bluetoothManagerBackDeviceSupportUnitWithWeight:(NSArray<NSNumber *> *)weightArray Height:(NSArray<NSNumber *> *)heightArray Temperature:(NSArray<NSNumber *> *)temperatureArray BloodPressure:(NSArray<NSNumber *> *)bloodPressureArray Pressure:(NSArray<NSNumber *> *)pressureArray{
  166. //这里要压力和温度单位
  167. //设置单位时,要设置设备支持的单位
  168. }
  169. -(void)bluetoothManagerUpdateBleState:(ELBluetoothState)state{
  170. switch (state) {
  171. case ELBluetoothStateUnavailable:
  172. {
  173. self.title = @"Please open the bluetooth";
  174. }
  175. break;
  176. case ELBluetoothStateAvailable:
  177. {
  178. self.title = @"Bluetooth is open";
  179. }
  180. break;
  181. case ELBluetoothStateScaning:
  182. {
  183. self.title = @"Scaning";
  184. }
  185. break;
  186. case ELBluetoothStateConnectFail:
  187. {
  188. self.title = @"Connect fail";
  189. }
  190. break;
  191. case ELBluetoothStateDidDisconnect:
  192. {
  193. self.title = @"Disconnected";
  194. }
  195. break;
  196. case ELBluetoothStateDidValidationPass:
  197. {
  198. self.title = @"Connected";
  199. [[ELBluetoothManager shareManager] getBluetoothInfoWithELInetGetCmdType:(ELInetGetCmdTypeReadDeviceSupportUnit)];
  200. }
  201. break;
  202. case ELBluetoothStateFailedValidation:
  203. {
  204. self.title = @"Illegal equipment";
  205. }
  206. break;
  207. case ELBluetoothStateWillConnect:
  208. self.title = @"Connecting";
  209. break;
  210. default:
  211. break;
  212. }
  213. }
  214. -(void)dealloc{
  215. }
  216. @end