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.

InheritConnectViewController.m 9.6KB

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