//藍牙搜索的類
@interface QueryPrinterViewController (){
? ? ? UITableView *_tableView;
?? ? ?NSMutableArray *_dataArr;//存儲所有搜索到得設備
}
@property (nonatomic,strong) CBPeripheral *peripheral;
@property (nonatomic,strong) CBCharacteristic *characteristic;
@property (nonatomic,strong) CBCentralManager *centralManager;
@end
@implementation QueryPrinterViewController
- (void)viewDidLoad {
[super viewDidLoad];
_dataArr = [[NSMutableArray alloc] init];
_charactersArr = [[NSMutableArray alloc] init];
isPrinting = YES;
//默認已連接設備
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
peripheralArr = [NSMutableArray arrayWithArray:[userDefaults valueForKey:@"havePares"]];
if (peripheralArr.count > 0) {
_haveConnectPeripherIdentify = peripheralArr[0][@"identify"];
}
self.centralManager = [[CBCentralManager alloc]initWithDelegate:self queue:dispatch_get_main_queue()];
[self initUI];
}
- (void)initUI {
self.navigationItem.title = @"藍牙打印";
[self setRightButtonWithTitle:@[@"搜索設備"]];
_tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, VIEWWIDTH, VIEWHEIGHT) style:UITableViewStyleGrouped];
_tableView.sectionHeaderHeight = 0.01;
_tableView.sectionFooterHeight = 0.01;
_tableView.delegate = self;
_tableView.dataSource = self;
_tableView.backgroundColor = kBackGroundColor;
self.automaticallyAdjustsScrollViewInsets = NO;
[self.view addSubview:_tableView];
if ([_tableView respondsToSelector:@selector(setSeparatorInset:)]) {
[_tableView? setSeparatorInset:UIEdgeInsetsMake(0, 0, 0, 0)];
}
if ([_tableView respondsToSelector:@selector(setLayoutMargins:)]) {
[_tableView setLayoutMargins:UIEdgeInsetsMake(0, 0, 0, 0)];
}
//底部測試打印
UIButton *footBtn = [UIButton buttonWithType:UIButtonTypeCustom];
footBtn.frame = CGRectMake(0, VIEWHEIGHT - 114, VIEWWIDTH, 50);
[footBtn setTitle:@"測試打印" forState:UIControlStateNormal];
[footBtn setImage:[UIImage imageNamed:@"藍牙測試打印圖標"] forState:UIControlStateNormal];
footBtn.imageEdgeInsets = UIEdgeInsetsMake(0, -20, 0, 0);
footBtn.backgroundColor = helpColor1;
footBtn.tag = 100;
[footBtn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:footBtn];
//當沒有商品時
_notGoodsView = [[[NSBundle mainBundle] loadNibNamed:@"HPNotGoodsView" owner:nil options:nil] lastObject];
_notGoodsView.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight);
[_notGoodsView reloadImageName:@"無藍牙打印設備" title:@"暫無設備,點擊右上角搜索哦" btnTitle:nil];
[self.view addSubview:_notGoodsView];
}
- (void)doRightAction:(UIButton *)sender {
[self startScan];
}
- (void)startScan {
[self.centralManager stopScan];
[self.centralManager scanForPeripheralsWithServices:nil options:nil];
if (timer) {
[timer invalidate];
}
timer = [NSTimer scheduledTimerWithTimeInterval:8
target:self
selector:@selector(scanStop)
userInfo:nil
repeats:NO];
}
//停止搜索
- (void)scanStop {
[_centralManager stopScan];
[timer invalidate];
}
#pragma mark - 藍牙狀態(tài)
-(void)centralManagerDidUpdateState:(CBCentralManager *)central{
switch (central.state) {
case CBCentralManagerStateUnknown:
YMDLog(@">>>CBCentralManagerStateUnknown");
break;
case CBCentralManagerStateResetting:
YMDLog(@">>>CBCentralManagerStateResetting");
break;
case CBCentralManagerStateUnsupported:
YMDLog(@">>>CBCentralManagerStateUnsupported");
break;
case CBCentralManagerStateUnauthorized:
YMDLog(@">>>CBCentralManagerStateUnauthorized");
break;
case CBCentralManagerStatePoweredOff:
YMDLog(@">>>CBCentralManagerStatePoweredOff");
break;
case CBCentralManagerStatePoweredOn:
{
YMDLog(@">>>CBCentralManagerStatePoweredOn");
//當前藍牙已打開
if ([UIApplication sharedApplication].applicationState == UIApplicationStateBackground) {
} else {
[self startScan];
}
}
break;
default:
break;
}
}
//掃描到設備會進入方法
-(void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI{
YMDLog(@"Did discover peripheral. peripheral: %@ rssi: %@, UUID: %@ advertisementData: %@ ", peripheral, RSSI, peripheral.identifier, advertisementData[@"kCBAdvDataServiceUUIDs"]);
if ([_haveConnectPeripherIdentify isEqualToString:peripheral.identifier.UUIDString]) {
self.peripheral = peripheral;
[self.centralManager connectPeripheral:self.peripheral options:nil];
[_centralManager connectPeripheral:peripheral? options:nil];
if ([_dataArr containsObject:peripheral]) {
[_dataArr removeObject:peripheral];
}
} else {
if (![_dataArr containsObject:peripheral]) {
[_dataArr addObject:peripheral];
}
}
[_tableView reloadData];
}
//連接到Peripherals-成功
- (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral
{
[self scanStop];
//保存
uuidsArr = [[NSMutableArray alloc] init];
peripheralDic = [[NSMutableDictionary alloc] init];
peripheral.delegate = self;
[central stopScan];
[peripheral discoverServices:nil];
YMDLog(@">>>外設連接 %@\n", [peripheral name]);
}
//連接到Peripherals-失敗
-(void)centralManager:(CBCentralManager *)central didFailToConnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error
{
_characteristic = nil;
_peripheral = nil;
YMDLog(@">>>連接到名稱為(%@)的設備-失敗,原因:%@",[peripheral name],[error localizedDescription]);
}
//Peripherals斷開連接
- (void)centralManager:(CBCentralManager *)central didDisconnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error{
_characteristic = nil;
_peripheral = nil;
YMDLog(@">>>外設連接斷開連接 %@: %@\n", [peripheral name], [error localizedDescription]);
//停止掃描
[central stopScan];
//斷開連接
[central cancelPeripheralConnection:peripheral];
[self startScan];
}
#pragma mark - 掃描到服務
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error
{
if (error)
{
YMDLog(@"Discovered services for %@ with error: %@", peripheral.name, [error localizedDescription]);
return;
}
for (CBService *service in peripheral.services)
{
[peripheral discoverCharacteristics:nil forService:service];
YMDLog(@"Service found with UUID: %@", service.UUID);
}
}
//掃描到特征
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error
{
if (error)
{
YMDLog(@"Discovered characteristics for %@ with error: %@", service.UUID, [error localizedDescription]);
return;
}
for (CBCharacteristic * characteristic in service.characteristics)
{
if ((characteristic.properties & CBCharacteristicPropertyWrite) && isPrinting) {
[peripheralArr removeAllObjects];
_peripheral = peripheral;
_characteristic = characteristic;
[_charactersArr addObject:characteristic];
YMDLog(@"Discovered characteristics for%@",characteristic.UUID.UUIDString);
for (CBService *servic in peripheral.services) {
[uuidsArr addObject:servic.UUID.UUIDString];
}
[peripheralDic? setObject:uuidsArr forKey:@"uuid"];
[peripheralDic setObject:peripheral.name ? peripheral.name : peripheral.identifier.UUIDString forKey:@"name"];
[peripheralDic? setObject:peripheral.identifier.UUIDString forKey:@"identify"];
_haveConnectPeripherIdentify = peripheral.identifier.UUIDString;
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
[peripheralArr insertObject:peripheralDic atIndex:0];
[userDefaults setValue:peripheralArr forKey:@"havePares"];
[userDefaults synchronize];
[_dataArr removeObject:peripheral];
[_tableView reloadData];
isPrinting = NO;
[self startScan];
}
}
}
//寫入成功的回調
- (void)peripheral:(CBPeripheral *)peripheral didWriteValueForCharacteristic:(CBCharacteristic *)characteristic error:(nullable NSError *)error {
YMDLog(@"---%@---",characteristic.UUID);
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
if (peripheralArr.count > 0 || _dataArr.count > 0) {
_notGoodsView.hidden = YES;
} else {
_notGoodsView.hidden = NO;
}
return 2;
}
#pragma mark - tableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (section == 0) {
return peripheralArr.count;
} else {
return _dataArr.count;
}
}
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
[cell setSeparatorInset:UIEdgeInsetsZero];
}
if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
[cell setLayoutMargins:UIEdgeInsetsZero];
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellID = @"cellID";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
}
cell.textLabel.font = Font15;
cell.textLabel.textColor = blackTextColor;
if (indexPath.section == 0) {
if (peripheralArr.count > 0) {
NSMutableDictionary *perDic = peripheralArr[0];
cell.textLabel.text = perDic[@"name"];
}
} else {
CBPeripheral *per = _dataArr[indexPath.row];
if (per.name) {
cell.textLabel.text = per.name;
} else {
cell.textLabel.text = [NSString stringWithFormat:@"未知設備%ld",(long)indexPath.row];
}
}
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
if (section == 0) {
if (peripheralArr.count > 0) {
return 40;
} else {
return 0.01;
}
} else {
if (_dataArr.count > 0) {
return 40;
} else {
return 0.01;
}
}
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.section == 0) {
if (peripheralArr.count > 0) {
return 50;
} else {
return 0;
}
} else {
if (_dataArr.count > 0) {
return 50;
} else {
return 0;
}
}
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
return 0.01;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.section == 1) {
isPrinting = YES;
[_centralManager connectPeripheral:_dataArr[indexPath.row]? options:nil];
}
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
if (section == 0) {
if (peripheralArr.count > 0) {
return @"? 默認連接的設備";
} else {
return @"";
}
} else {
if (_dataArr.count > 0) {
return @"? 搜索到的設備";
} else {
return @"";
}
}
}
#pragma mark - 刪除已配對設備
-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.section == 0) {
return UITableViewCellEditingStyleDelete;
} else {
return UITableViewCellEditingStyleNone;
}
}
/*改變刪除按鈕的title*/
-(NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
return @"刪除";
}
/*刪除用到的函數(shù)*/
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
_haveConnectPeripherIdentify = @"";
[peripheralArr removeAllObjects];
[[NSUserDefaults standardUserDefaults] setValue:peripheralArr forKey:@"havePares"];
[[NSUserDefaults standardUserDefaults] synchronize];
[tableView reloadData];
if (_peripheral) {
[_centralManager cancelPeripheralConnection:_peripheral];
}
}
#pragma mark - 測試打印
- (void)btnClick:(UIButton *)btn {
NSMutableArray *sendDataArray = [[NSMutableArray alloc] init];
[sendDataArray addObject:@"云門店打印測試:iOS"];
[sendDataArray addObject:[NSDate date]];
[sendDataArray addObject:@""];
[sendDataArray addObject:@""];
if (btn.tag == 100) {
if (_peripheral) {
for (CBCharacteristic * characteristic in _charactersArr) {
for (int i = 0; i < sendDataArray.count; i++) {
NSStringEncoding enc = CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingGB_18030_2000);
NSString *curPrintContent = [NSString stringWithFormat:@"%@",sendDataArray[i]];
NSString *printed = [curPrintContent stringByAppendingFormat:@"%c", '\n'];
NSData? *data= [printed dataUsingEncoding: enc];
[_peripheral writeValue:data forCharacteristic:characteristic type:CBCharacteristicWriteWithResponse];
}
}
return;
}
}
[Utils showMsg:@"親,藍牙連接失敗哦"];
}