Commit c61c367e authored by lujunye's avatar lujunye

蓝牙接入完成

parent 37b07214
......@@ -40,13 +40,13 @@ import ESTabBarController_swift
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate,loginDelegate,UITabBarControllerDelegate, JPUSHRegisterDelegate{
var blueToothVC:UIViewController!
var blueToothVC:BtViewController!
var window:UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// 启动图,调整展示时间为3秒
Thread .sleep(forTimeInterval: 2.0)
blueToothVC = UIViewController()
blueToothVC = BtViewController()
let UserToken = UserDefaults.standard.value(forKey: "user_token")
if UserToken == nil {
......
......@@ -29,5 +29,5 @@
#import "HLBLEManager.h"
#import "SVProgressHUD.h"
#import "BtViewController.h"
#endif /* Bridge_Header_h */
......@@ -9,21 +9,32 @@
#import "BtViewController.h"
#import "HLBLEManager.h"
#import "SVProgressHUD.h"
@interface BtViewController ()
#import "HLPrinter.h"
#import "UIWebView+UIImage.h"
#import "Bt_Cell.h"
@interface BtViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (strong, nonatomic)NSMutableArray *deviceArray; /**< 蓝牙设备个数 */
@property (strong, nonatomic)NSMutableArray *selectArray;//选择蓝牙
@property (copy, nonatomic) void(^printBlock)(HLPrinter *printer);
@property (nonatomic, strong) HLBLEManager *manager;
@end
@implementation BtViewController
- (IBAction)backAction:(id)sender {
[self.navigationController popViewControllerAnimated:YES];
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
_deviceArray = [[NSMutableArray alloc] init];
_selectArray = [[NSMutableArray alloc]init];
_tableView.rowHeight = 44;
[_tableView registerNib:[UINib nibWithNibName:@"Bt_Cell" bundle:NULL] forCellReuseIdentifier:@"Bt_Cell"];
HLBLEManager *manager = [HLBLEManager sharedInstance];
__weak HLBLEManager *weakManager = manager;
manager.stateUpdateBlock = ^(CBCentralManager *central) {
......@@ -93,6 +104,171 @@
};
}
#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (section == 0) {
return _selectArray.count;
}
return _deviceArray.count;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[self.selectArray removeAllObjects];
if (indexPath.section == 1){
NSDictionary *dict = [self.deviceArray objectAtIndex:indexPath.row];
CBPeripheral *peripheral = dict[@"peripheral"];
[self.selectArray addObject:peripheral];
[self loadBLEInfo];
[tableView reloadData];
}else{
[_manager cancelPeripheralConnection];
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
Bt_Cell *cell = [tableView dequeueReusableCellWithIdentifier:@"Bt_Cell"];
NSDictionary *dict = [self.deviceArray objectAtIndex:indexPath.row];
CBPeripheral *peripherral = dict[@"peripheral"];
cell.statusImg.hidden = YES;
if (indexPath.section == 0) {
cell.statusImg.hidden = NO;
}
cell.title_lbl.text = peripherral.name;
return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
return 46;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
return 46;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 46)];
view.backgroundColor = [UIColor colorNamed:@"app底色"];
UILabel *lbl = [[UILabel alloc]initWithFrame:CGRectMake(15, 0, self.view.bounds.size.width, 46)];
lbl.font = [UIFont boldSystemFontOfSize:15];
lbl.textColor = [UIColor colorNamed:@"灰色字体颜色"];
[view addSubview:lbl];
switch (section) {
case 0:
{
lbl.text = @"已连接设备";
}
break;
default:
{
lbl.text = @"未连接设备";
}
break;
}
return view;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 46)];
view.backgroundColor = [UIColor colorNamed:@"app底色"];
UILabel *lbl = [[UILabel alloc]initWithFrame:CGRectMake(15, 0, self.view.bounds.size.width, 46)];
lbl.font = [UIFont boldSystemFontOfSize:11];
lbl.textColor = [UIColor colorNamed:@"灰色字体颜色"];
[view addSubview:lbl];
switch (section) {
case 0:
{
lbl.text = @"选择已经连接的设备作为默认打印设备";
}
break;
default:
{
lbl.text = @"选择设备进行连接";
}
break;
}
return view;
}
// UIWebView *webView = [[UIWebView alloc]init];
// webView.delegate = self;
// [webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"https://c.gelifood.com/market/src/views/Order/codeDetail.html?order_id=12767"]]];
//- (void)webViewDidFinishLoad:(UIWebView *)webView
//{
// HLPrinter *printer = [[HLPrinter alloc] init];
// UIImage *image = [webView imageForWebView];
// [printer appendImage:image alignment:HLTextAlignmentLeft maxWidth:450];
//
// if (_printBlock) {
// _printBlock(printer);
// }
//}
- (void)loadBLEInfo
{
CBPeripheral *perpheral = _selectArray.firstObject;
HLBLEManager *manager = [HLBLEManager sharedInstance];
_manager = manager;
[manager connectPeripheral:perpheral
connectOptions:@{CBConnectPeripheralOptionNotifyOnDisconnectionKey:@(YES)}
stopScanAfterConnected:YES
servicesOptions:nil
characteristicsOptions:nil
completeBlock:^(HLOptionStage stage, CBPeripheral *peripheral, CBService *service, CBCharacteristic *character, NSError *error) {
switch (stage) {
case HLOptionStageConnection:
{
if (error) {
[SVProgressHUD showErrorWithStatus:@"连接失败"];
} else {
[SVProgressHUD showSuccessWithStatus:@"连接成功"];
}
break;
}
case HLOptionStageSeekServices:
{
if (error) {
// [SVProgressHUD showSuccessWithStatus:@"查找服务失败"];
} else {
// [SVProgressHUD showSuccessWithStatus:@"查找服务成功"];
}
break;
}
case HLOptionStageSeekCharacteristics:
{
// 该block会返回多次,每一个服务返回一次
if (error) {
NSLog(@"查找特性失败");
} else {
NSLog(@"查找特性成功");
[_tableView reloadData];
}
break;
}
case HLOptionStageSeekdescriptors:
{
// 该block会返回多次,每一个特性返回一次
if (error) {
NSLog(@"查找特性的描述失败");
} else {
// NSLog(@"查找特性的描述成功");
}
break;
}
default:
break;
}
}];
}
@end
......@@ -45,6 +45,9 @@
<constraint firstAttribute="width" relation="greaterThanOrEqual" constant="46" id="BLS-5O-HaU"/>
<constraint firstAttribute="width" constant="46" id="hrV-57-cL5"/>
</constraints>
<connections>
<action selector="backAction:" destination="-1" eventType="touchUpInside" id="JMm-Xi-knv"/>
</connections>
</button>
</subviews>
<color key="backgroundColor" systemColor="systemBackgroundColor"/>
......@@ -88,7 +91,8 @@
</view>
<tableView clipsSubviews="YES" contentMode="scaleToFill" alwaysBounceVertical="YES" style="plain" separatorStyle="default" rowHeight="-1" estimatedRowHeight="-1" sectionHeaderHeight="28" sectionFooterHeight="28" translatesAutoresizingMaskIntoConstraints="NO" id="1gI-Ls-VbE">
<rect key="frame" x="0.0" y="108" width="414" height="705"/>
<color key="backgroundColor" systemColor="systemBackgroundColor"/>
<color key="backgroundColor" name="app底色"/>
<color key="tintColor" white="0.0" alpha="0.0" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
<connections>
<outlet property="dataSource" destination="-1" id="d6a-dm-laN"/>
<outlet property="delegate" destination="-1" id="KQt-4I-gy6"/>
......@@ -115,6 +119,9 @@
<resources>
<image name="fanhui" width="18" height="35"/>
<image name="guanbi" width="37" height="18"/>
<namedColor name="app底色">
<color red="0.94509803921568625" green="0.94509803921568625" blue="0.94509803921568625" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
</namedColor>
<namedColor name="标题字颜色">
<color red="0.18000000715255737" green="0.18000000715255737" blue="0.18000000715255737" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
</namedColor>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment