iOS AILinkBleSDK - 蓝牙SDK
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

InheritConnectViewController.m 9.5KB

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