iOS AILinkBleSDK - 蓝牙SDK
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

InheritConnectViewController.m 9.9KB

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