CommonScannerSettingViewController.m 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. //
  2. // CommonScannerSettingViewController.m
  3. // iSales-CONTRAST
  4. //
  5. // Created by Rui Zhang on 12/9/22.
  6. // Copyright © 2022 United Software Applications, Inc. All rights reserved.
  7. //
  8. #import "CommonScannerSettingViewController.h"
  9. #import "AppDelegate.h"
  10. #import <UserNotifications/UserNotifications.h>
  11. @interface CommonScannerSettingViewController ()
  12. @property (weak, nonatomic) IBOutlet UIButton *btnConnect;
  13. @property (weak, nonatomic) IBOutlet UILabel *labelDevice;
  14. @property (weak, nonatomic) IBOutlet UITableView *tableDiscover;
  15. @property (weak, nonatomic) IBOutlet UITextView *tvLog;
  16. @property (weak, nonatomic) IBOutlet UISwitch *swEnable;
  17. @property (weak, nonatomic) IBOutlet UISwitch *swAuto;
  18. //@property (strong, nonatomic) NSString *needVerifyUUID;
  19. @end
  20. @implementation CommonScannerSettingViewController
  21. - (IBAction)onDisconnect:(id)sender {
  22. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  23. if(appDelegate.peripheral)
  24. {
  25. [appDelegate.manager cancelPeripheralConnection:appDelegate.peripheral];
  26. appDelegate.peripheral = nil;
  27. }
  28. [appDelegate setBLEAutoReconnect:false];
  29. }
  30. - (void)onCloseClick:(UIButton *)sender {
  31. [self dismissViewControllerAnimated:true completion:^{
  32. // if(self.onDismissVC)
  33. // self.onDismissVC();
  34. }];
  35. }
  36. - (void)viewDidLoad {
  37. [super viewDidLoad];
  38. self.btnConnect.enabled = false;
  39. self.support_scanner = true;
  40. UIBarButtonItem *closeButton = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"close"] imageWithRenderingMode:UIImageRenderingModeAutomatic]
  41. style:UIBarButtonItemStylePlain
  42. target:self
  43. action:@selector( onCloseClick:)];
  44. self.navigationItem.rightBarButtonItem = closeButton;
  45. // NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
  46. // // CFShow(infoDictionary);
  47. // // app名称
  48. // NSString *app_Name = [infoDictionary objectForKey:@"CFBundleName"];
  49. // // app版本
  50. // NSString *app_Version = [infoDictionary objectForKey:@"CFBundleShortVersionString"];
  51. // // app build版本
  52. // NSString *app_build = [infoDictionary objectForKey:@"CFBundleVersion"];
  53. //
  54. //
  55. // NSString* appinfo =[NSString stringWithFormat:@"App info: %@ %@ build%@",app_Name,app_Version,app_build];
  56. // self.appinfoLabel.text = appinfo;
  57. self.tvLog.layer.borderWidth=1 ; //边框粗细
  58. self.tvLog.layer.borderColor=[UIColor darkGrayColor].CGColor; //边框颜色
  59. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  60. self.tvLog.text = appDelegate.log;
  61. [self.swAuto setOn:appDelegate.bAutoConnect];
  62. [self.swEnable setOn:appDelegate.bAppBTOn];
  63. self.btnConnect.enabled = (appDelegate.peripheral!=nil);
  64. self.labelDevice.text =appDelegate.peripheral.name;
  65. // Do any additional setup after loading the view.
  66. }
  67. - (void)viewDidAppear:(BOOL)animated {
  68. [super viewDidAppear:animated];
  69. if (@available(iOS 10.0, *)) {
  70. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  71. if(appDelegate.bBTAuthorize==false)
  72. [self opentBT];
  73. }
  74. }
  75. /** 跳转系统设置方法*/
  76. - (void)opentBT {
  77. /**< 弹出框 */
  78. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"App authorize"
  79. message:@"Please turn on Bluetooth access in the system setting"
  80. preferredStyle:UIAlertControllerStyleAlert];
  81. [alertController addAction:[UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  82. }]];
  83. [alertController addAction:[UIAlertAction actionWithTitle:@"Yes" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  84. [self.presentedViewController dismissViewControllerAnimated:YES completion:nil];
  85. // if (@available(iOS 10.0, *)) {
  86. [[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString] options:@{} completionHandler:nil];
  87. // } else {
  88. // [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"prefs:root=NOTIFICATIONS_ID&path=com.xxx.xxx"]];//prefs:root=服务&path=项目bundleID     }
  89. }]];
  90. [self presentViewController:alertController animated:YES completion:nil];
  91. }
  92. /*
  93. #pragma mark - Navigation
  94. // In a storyboard-based application, you will often want to do a little preparation before navigation
  95. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  96. // Get the new view controller using [segue destinationViewController].
  97. // Pass the selected object to the new view controller.
  98. }
  99. */
  100. - (IBAction)onEnable:(id)sender {
  101. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  102. UISwitch* sw = sender;
  103. [appDelegate enableBLE:sw.isOn];
  104. // if(sw.isOn)
  105. // {
  106. //
  107. //
  108. // [appDelegate scanBLE];
  109. // }
  110. // else
  111. // {
  112. // [appDelegate stopScanBLE];
  113. //
  114. // }
  115. }
  116. - (IBAction)onReconnect:(id)sender {
  117. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  118. UISwitch* sw = sender;
  119. // if(sw.isOn)
  120. // {
  121. //
  122. // [appDelegate setBLEAutoReconnect:sw.isOn]
  123. // }
  124. // else
  125. // {
  126. // appDelegate.bAutoConnect=false;
  127. //
  128. // }
  129. [appDelegate setBLEAutoReconnect:sw.isOn];
  130. }
  131. #pragma mark tableview
  132. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  133. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  134. return appDelegate.nDevices.count;
  135. }
  136. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  137. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  138. static NSString *CellIdentifier = @"Cell";
  139. // UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
  140. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  141. if (!cell) {
  142. cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
  143. }
  144. CBPeripheral *p=appDelegate.nDevices[indexPath.item];
  145. NSString* name = p.name;
  146. if(name.length==0)
  147. name=@"NO Name";
  148. cell.textLabel.text = name;
  149. return cell;
  150. }
  151. -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(nonnull NSIndexPath *)indexPath
  152. {
  153. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  154. CBPeripheral *p=appDelegate.nDevices[indexPath.item];
  155. // self.needVerifyUUID=p.identifier.UUIDString;
  156. appDelegate.peripheral = p;
  157. [appDelegate.manager connectPeripheral:appDelegate.peripheral options:nil];
  158. }
  159. #pragma mark scanner event
  160. -(void) OnUpdateDevicesList
  161. {
  162. [self.tableDiscover reloadData];
  163. }
  164. -(void) onDecodedData:(NSString*) value
  165. {
  166. }
  167. -(void) onErrorInfo:(NSString*) errorInfo
  168. {
  169. }
  170. -(void) onStatusChanged:(NSString*) status
  171. {
  172. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  173. if([status isEqualToString:@"Connected"])
  174. {
  175. self.btnConnect.enabled = true;
  176. self.labelDevice.text =appDelegate.peripheral.name;
  177. [appDelegate setBLEAutoReconnect:true];
  178. }
  179. else if([status isEqualToString:@"Disconnected"])
  180. {
  181. self.labelDevice.text = nil;
  182. self.btnConnect.enabled = false;
  183. }
  184. else if([status isEqualToString:@"Unauthorized"])
  185. {
  186. [self opentBT];
  187. }
  188. }
  189. -(void) onLogUpdate
  190. {
  191. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  192. self.tvLog.text = appDelegate.log;
  193. NSRange range = NSMakeRange(self.tvLog.text.length, 0);
  194. [self.tvLog scrollRangeToVisible:range];
  195. }
  196. @end