iOS蓝牙状态的处理(蓝牙关闭及未授权的处理)

原创
2021/09/10 00:00
阅读数 2.1K

前言

  • 【打印商品价格标签及打印交易小票】demo源码:https://download.csdn.net/download/u011018979/14920529

1、应用场景:打印商品价格标签、打印交易小票

2、特色功能:实现自动连接最近使用的打印机、统一处理蓝牙状态

3、原理文章:https://kunnan.blog.csdn.net/article/details/85684014

4、解决的问题:人民币¥符号乱码的问题

价格标签打印(品名支持多行显示)

票据打印(自动实现%ns 自动补齐空格的功能)

公众号:iOS逆向


I、检测

1.1 监听蓝牙状态


#define kConnecterManager [ConnecterManager sharedInstance]


#pragma mark - ******** 监听蓝牙状态
//block
+ (void)listenBluetoothpoweredState:(void(^)(CBPeripheral * _Nullable peripheral))peripheral {
    
    if (kConnecterManager.bleConnecter == nil) {
        [kConnecterManager centralmanager];

        [kConnecterManager didUpdateState:^(NSInteger state) {
            
            [self setupState:state peripheral:peripheral];


            
            
        }];
    }
         
         else {// 需要获取蓝牙的连接状态
        
             [self setupState:[kConnecterManager centralmanager].state peripheral:peripheral];

        

    }
}

1.2  统一处理蓝牙状态

  • 统一处理蓝牙状态的判断

/**
 用于统一处理蓝牙状态的判断
 @param state <#state description#>
 @param peripheral <#peripheral description#>
 */

+ (void)setupState:(NSInteger)state peripheral:(void(^)(CBPeripheral * _Nullable peripheral))peripheral{
    
    
    switch (state) {
        case CBCentralManagerStateUnsupported:
            NSLog(@"The platform/hardware doesn't support Bluetooth Low Energy.");
            

            
            
            break;
            // CBManagerStateUnauthorized,3//                NSLog(@"手机蓝牙功能关闭,请前往设置打开蓝牙及控制中心打开蓝牙。"); break;
            
#pragma mark - ******** 没有授权

        case CBCentralManagerStateUnauthorized:
            NSLog(@"The app is not authorized to use Bluetooth Low Energy.");
            
            //
            [self setupBLEUnauthorized];

            
            
            break;
            //CBManagerStatePoweredOff
            
            #pragma mark - ******** 电源关闭

        case CBCentralManagerStatePoweredOff:
            NSLog(@"Bluetooth is currently powered off.");
            
            //  第一次系统会提示
            if(QCTSession.shareQCTSession.isNOFirstShowBLEPowOff){
                
                
                [self setupBLEOff];

                
            }else{
                QCTSession.shareQCTSession.isNOFirstShowBLEPowOff= YES;
                
                
            }
            
            
            
            break;
            

        case CBCentralManagerStatePoweredOn:
            [self startScane:peripheral];
            NSLog(@"Bluetooth power on");
            break;
        case CBCentralManagerStateUnknown:
        default:
            break;
    }
    
}

+ (void) setupBLEUnauthorized{
    
    
    // 弹窗提示
    [QCTLocationServiceUtil setupCentralManagerState_not_authorized];
// 关闭蓝牙的时候更新蓝牙状态为断开
    
    [QCTSession clearPrintInfo];
    
    
    // 通知控制器更新以配对设备列表信息
    
    [[NSNotificationCenter defaultCenter] postNotificationName:QCTQCTBluetoothListViewControllerreloadDateNotification object: nil];
    
    
    
}



+ (void) setupBLEOff{
    
    // 弹窗提示
    [QCTLocationServiceUtil setupCentralManagerStatePoweredOff];
// 关闭蓝牙的时候更新蓝牙状态为断开
    
    [QCTSession clearPrintInfo];
    
    
    // 通知控制器更新以配对设备列表信息
    
    [[NSNotificationCenter defaultCenter] postNotificationName:QCTQCTBluetoothListViewControllerreloadDateNotification object: nil];
    
    
    
}

II、处理

2.0 处理未授权的情况

  • setupCentralManagerState_not_authorized
+ (void) setupCentralManagerState_not_authorized{
    

    
    NSString *tips = @"手机蓝牙功能没有权限,请前往设置。";
    
    
    [LBAlertController showAlertTitle:@"无法使用蓝牙" content:tips cancelString:@"确定" cancleBlock :^{
        

        
        
        //        322
        if([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]]){
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
        }
        
        
        
    }   sureString:nil sureBlock:nil currentController:[QCT_Common getCurrentVC]];
    
    
}

2.1 处理关闭的状态

  • setupCentralManagerStatePoweredOff
    NSString *tips = @"手机蓝牙功能关闭,请前往设置打开蓝牙及控制中心打开蓝牙。";
    
    
    [LBAlertController showAlertTitle:@"无法使用蓝牙" content:tips cancelString:@"确定" cancleBlock :^{
        

        
        
        //        322
        if([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]]){
            

            
//            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
        }
        
        
        
    }   sureString:nil sureBlock:nil currentController:[QCT_Common getCurrentVC]];

III、完整demo 源码

  • 【打印商品价格标签及打印交易小票】demo源码:https://download.csdn.net/download/u011018979/14920529

1、应用场景:打印商品价格标签、打印交易小票

2、特色功能:实现自动连接最近使用的打印机、统一处理蓝牙状态

3、原理文章:https://kunnan.blog.csdn.net/article/details/85684014

4、解决的问题:人民币¥符号乱码的问题

IV 、see also

4.1 跳转设置的3种方式

 方式一:prefs:root=某项服务 适用于 小于 iOS10的系统;

 方式二:prefs:root=bundleID 适用于 大于等于iOS8系统,小于iOS10的系统

 方式三: UIApplicationOpenSettingsURLString
 version >= iOS10,支持跳转到自己应用设置,不支持跳转到系统设置

  • 注意
 NSURL *url = [NSURL URLWithString:@"App-Prefs:root=Bluetooth"];

 
 以上写法,上传APPStore的时候,会被拒,可以修改成以下写法:


 // 将字符串转换成16进制,转换一下,再去调用,成不成功,就看脸了
 NSData *encryptString = [[NSData alloc] initWithBytes:(unsigned char []){0x410x700x700x2d0x500x720x650x660x730x3a0x720x6f0x6f0x740x3d0x420x6c0x750x650x740x6f0x6f0x740x68} length:24];
 NSString *string = [[NSString alloc] initWithData:encryptString encoding:NSUTF8StringEncoding];

4.2 ConnecterManager的完整代码

请关注公众号:iOS逆向,从原文查看完整代码。


本文分享自微信公众号 - iOS逆向(code4ios)。
如有侵权,请联系 support@oschina.cn 删除。
本文参与“OSC源创计划”,欢迎正在阅读的你也加入,一起分享。

展开阅读全文
ios
加载中
点击引领话题📣 发布并加入讨论🔥
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部