前言
- 【打印商品价格标签及打印交易小票】demo源码:https://download.csdn.net/download/u011018979/14920529
1、应用场景:打印商品价格标签、打印交易小票
2、特色功能:实现自动连接最近使用的打印机、统一处理蓝牙状态
3、原理文章:https://kunnan.blog.csdn.net/article/details/85684014
4、解决的问题:人民币¥符号乱码的问题
价格标签打印(品名支持多行显示)
票据打印(自动实现%ns 自动补齐空格的功能)
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 []){0x41, 0x70, 0x70, 0x2d, 0x50, 0x72, 0x65, 0x66, 0x73, 0x3a, 0x72, 0x6f, 0x6f, 0x74, 0x3d, 0x42, 0x6c, 0x75, 0x65, 0x74, 0x6f, 0x6f, 0x74, 0x68} length:24]; NSString *string = [[NSString alloc] initWithData:encryptString encoding:NSUTF8StringEncoding];
4.2 ConnecterManager的完整代码
请关注公众号:iOS逆向,从原文查看完整代码。