// // CommonScannerSettingViewController.m // iSales-CONTRAST // // Created by Rui Zhang on 12/9/22. // Copyright © 2022 United Software Applications, Inc. All rights reserved. // #import "CommonScannerSettingViewController.h" #import "AppDelegate.h" #import @interface CommonScannerSettingViewController () @property (weak, nonatomic) IBOutlet UIButton *btnConnect; @property (weak, nonatomic) IBOutlet UILabel *labelDevice; @property (weak, nonatomic) IBOutlet UITableView *tableDiscover; @property (weak, nonatomic) IBOutlet UITextView *tvLog; @property (weak, nonatomic) IBOutlet UISwitch *swEnable; @property (weak, nonatomic) IBOutlet UISwitch *swAuto; //@property (strong, nonatomic) NSString *needVerifyUUID; @end @implementation CommonScannerSettingViewController - (IBAction)onDisconnect:(id)sender { AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate]; if(appDelegate.peripheral) { [appDelegate.manager cancelPeripheralConnection:appDelegate.peripheral]; appDelegate.peripheral = nil; } [appDelegate setBLEAutoReconnect:false]; } - (void)onCloseClick:(UIButton *)sender { [self dismissViewControllerAnimated:true completion:^{ // if(self.onDismissVC) // self.onDismissVC(); }]; } - (void)viewDidLoad { [super viewDidLoad]; self.btnConnect.enabled = false; self.support_scanner = true; UIBarButtonItem *closeButton = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"close"] imageWithRenderingMode:UIImageRenderingModeAutomatic] style:UIBarButtonItemStylePlain target:self action:@selector( onCloseClick:)]; self.navigationItem.rightBarButtonItem = closeButton; // NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary]; // // CFShow(infoDictionary); // // app名称 // NSString *app_Name = [infoDictionary objectForKey:@"CFBundleName"]; // // app版本 // NSString *app_Version = [infoDictionary objectForKey:@"CFBundleShortVersionString"]; // // app build版本 // NSString *app_build = [infoDictionary objectForKey:@"CFBundleVersion"]; // // // NSString* appinfo =[NSString stringWithFormat:@"App info: %@ %@ build%@",app_Name,app_Version,app_build]; // self.appinfoLabel.text = appinfo; self.tvLog.layer.borderWidth=1 ; //边框粗细 self.tvLog.layer.borderColor=[UIColor darkGrayColor].CGColor; //边框颜色 AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate]; self.tvLog.text = appDelegate.log; [self.swAuto setOn:appDelegate.bAutoConnect]; [self.swEnable setOn:appDelegate.bAppBTOn]; self.btnConnect.enabled = (appDelegate.peripheral!=nil); self.labelDevice.text =appDelegate.peripheral.name; // Do any additional setup after loading the view. } - (void)viewDidAppear:(BOOL)animated { [super viewDidAppear:animated]; if (@available(iOS 10.0, *)) { AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate]; if(appDelegate.bBTAuthorize==false) [self opentBT]; } } /** 跳转系统设置方法*/ - (void)opentBT { /**< 弹出框 */ UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"App authorize" message:@"Please turn on Bluetooth access in the system setting" preferredStyle:UIAlertControllerStyleAlert]; [alertController addAction:[UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { }]]; [alertController addAction:[UIAlertAction actionWithTitle:@"Yes" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { [self.presentedViewController dismissViewControllerAnimated:YES completion:nil]; // if (@available(iOS 10.0, *)) { [[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString] options:@{} completionHandler:nil]; // } else { // [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"prefs:root=NOTIFICATIONS_ID&path=com.xxx.xxx"]];//prefs:root=服务&path=项目bundleID     } }]]; [self presentViewController:alertController animated:YES completion:nil]; } /* #pragma mark - Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { // Get the new view controller using [segue destinationViewController]. // Pass the selected object to the new view controller. } */ - (IBAction)onEnable:(id)sender { AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate]; UISwitch* sw = sender; [appDelegate enableBLE:sw.isOn]; // if(sw.isOn) // { // // // [appDelegate scanBLE]; // } // else // { // [appDelegate stopScanBLE]; // // } } - (IBAction)onReconnect:(id)sender { AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate]; UISwitch* sw = sender; // if(sw.isOn) // { // // [appDelegate setBLEAutoReconnect:sw.isOn] // } // else // { // appDelegate.bAutoConnect=false; // // } [appDelegate setBLEAutoReconnect:sw.isOn]; } #pragma mark tableview - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate]; return appDelegate.nDevices.count; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate]; static NSString *CellIdentifier = @"Cell"; // UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (!cell) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; } CBPeripheral *p=appDelegate.nDevices[indexPath.item]; NSString* name = p.name; if(name.length==0) name=@"NO Name"; cell.textLabel.text = name; return cell; } -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(nonnull NSIndexPath *)indexPath { AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate]; CBPeripheral *p=appDelegate.nDevices[indexPath.item]; // 如果点到当前连接设备,则忽略。 if([appDelegate.peripheral.identifier.UUIDString isEqualToString:p.identifier.UUIDString]) return; [self onDisconnect:nil]; // self.needVerifyUUID=p.identifier.UUIDString; appDelegate.peripheral = p; [appDelegate.manager connectPeripheral:appDelegate.peripheral options:nil]; } #pragma mark scanner event -(void) OnUpdateDevicesList { [self.tableDiscover reloadData]; } -(void) onDecodedData:(NSString*) value { } -(void) onErrorInfo:(NSString*) errorInfo { } -(void) onStatusChanged:(NSString*) status { AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate]; if([status isEqualToString:@"Connected"]) { self.btnConnect.enabled = true; self.labelDevice.text =appDelegate.peripheral.name; [appDelegate setBLEAutoReconnect:true]; } else if([status isEqualToString:@"Disconnected"]) { self.labelDevice.text = nil; self.btnConnect.enabled = false; } else if([status isEqualToString:@"Unauthorized"]) { [self opentBT]; } } -(void) onLogUpdate { AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate]; self.tvLog.text = appDelegate.log; NSRange range = NSMakeRange(self.tvLog.text.length, 0); [self.tvLog scrollRangeToVisible:range]; } @end