iOS蓝牙配对自定义弹框

  • 1、不弹窗的问题.
    (1) 蓝牙授权需在info.plist 中添加权限访问 【 Privacy - Bluetooth Peripheral Usage Description】-------- XX app想访问您的蓝牙 (备注:虽然不适用系统的,但是授权还是要加滴!)
    (2)应用中添加framework : 【CoreBluetooth.framework】,在使用的文件中【#import
    (3)配置系统授权弹窗提示:
属性:
 @property (nonatomic, strong) CBCentralManager *manager;

添加代理监听:
 NSDictionary *options = @{CBCentralManagerOptionShowPowerAlertKey:@NO};//不弹窗(配置)
    self.manager = [[CBCentralManager alloc] initWithDelegate:self queue:nil options:options];
实现代理方法:
- (void)centralManagerDidUpdateState:(CBCentralManager *)central {
    NSString *strMessage = @"";
    NSString *buttonTitle = nil;
    switch (central.state) {
        case CBManagerStatePoweredOn: {
            NSLog(@"蓝牙开启且可用");
            return;
        }
            break;
        case CBManagerStateUnknown: {
            strMessage = @"手机没有识别到蓝牙,请检查手机。";
            buttonTitle = @"前往设置";
        }
            break;
        case CBManagerStateResetting: {
            strMessage = @"手机蓝牙已断开连接,重置中...";
            buttonTitle = @"前往设置";
        }
            break;
        case CBManagerStateUnsupported: {
            strMessage = @"手机不支持蓝牙功能,请更换手机。";
        }
            break;
        case CBManagerStatePoweredOff: {
            strMessage = @"手机蓝牙功能关闭,请前往设置打开蓝牙及控制中心打开蓝牙。";
            buttonTitle = @"前往设置";
        }
            break;
        case CBManagerStateUnauthorized: {
            strMessage = @"手机蓝牙功能没有权限,请前往设置。";
            buttonTitle = @"前往设置";
        }
            break;
        default: { }
            break;
    }
    //通知没有打开蓝牙的自定义提示弹窗(弹窗代码自行实现)
    [self __broadAlertMessage:strMessage buttonTitle:buttonTitle];
}
//当使用完蓝牙时记得关闭蓝牙监听,减少资源消耗:
    self.manager.delegate = nil;
    self.manager = nil;
  • 蓝牙设置页面跳转
 NSURL *url = [NSURL URLWithString:@"App-Prefs:root=Bluetooth"];
    if (@available(iOS 10.0, *)) {
        [[UIApplication sharedApplication] openURL:url options:@{} completionHandler:nil];
    } else {
        [[UIApplication sharedApplication] openURL:url];
    }
//避免被拒
 //将字符串转换为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];
    NSURL *url = [NSURL URLWithString:string];
    if (@available(iOS 10.0, *)) {
        [[UIApplication sharedApplication] openURL:url options:@{} completionHandler:nil];
    } else {
        [[UIApplication sharedApplication] openURL:url];
    }

你可能感兴趣的:(iOS蓝牙配对自定义弹框)