蓝牙授权需要在Info.plist中加入
Privacy - Bluetooth Peripheral Usage Description --------XXX想要访问您的蓝牙
1.在项目中引入库
CoreBluetooth.framework
2.引入头文件
#import
3.创建对象
@property (nonatomic, strong) CBCentralManager *centralManager;
4.继承代理
CBCentralManagerDelegate
5.初始化对象,并设置代理
self.centralManager = [[CBCentralManager alloc] initWithDelegate:self queue:nil options:nil];
6.代理方法实现
当设备开关蓝牙,都会走这个回调,程序启动也会走,必须调用的方法
- (void)centralManagerDidUpdateState:(CBCentralManager *)central {
switch (central.state) {
case CBManagerStatePoweredOn:
NSLog(@"蓝牙开启且可用"); break;
case CBManagerStateUnknown:
NSLog(@"手机没有识别到蓝牙,请检查手机。"); break;
case CBManagerStateResetting:
NSLog(@"手机蓝牙已断开连接,重置中。"); break;
case CBManagerStateUnsupported:
NSLog(@"手机不支持蓝牙功能,请更换手机。"); break;
case CBManagerStatePoweredOff:
NSLog(@"手机蓝牙功能关闭,请前往设置打开蓝牙及控制中心打开蓝牙。"); break;
case CBManagerStateUnauthorized:
NSLog(@"手机蓝牙功能没有权限,请前往设置。"); break;
default: break;
}
}
扫描到设备会进入方法
- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI {
NSLog(@"当扫描到设备:%@",peripheral.name);
//接下来可以连接设备
}
目前这么写的情况下,会出现一种问题,当关闭蓝牙的时候,会一直提示,当前项目需要开启蓝牙,并要求去设置画面开启蓝牙。
修改添加代理部分:
//不弹窗(配置)
NSDictionary *options = @{CBCentralManagerOptionShowPowerAlertKey:@NO};
self.centralManager = [[CBCentralManager alloc] initWithDelegate:self queue:nil options:options];
// 因为弹窗给禁止掉了,需要自己写弹窗
- (void)centralManagerDidUpdateState:(CBCentralManager *)central {
NSString *strMessage = 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;
}
// 自己写的弹窗处理
if (strMessage != nil && strMessage.length != 0) {
NSLog(@"strMessage == %@", strMessage);
NSLog(@"buttonTitle == %@", buttonTitle);
// --------自行处理
}
}
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];
}
以上写法,上传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];
NSURL *url = [NSURL URLWithString:string];
if (@available(iOS 10.0, *)) {
[[UIApplication sharedApplication] openURL:url options:@{} completionHandler:nil];
} else {
[[UIApplication sharedApplication] openURL:url];
}