蓝牙LE询问权限?
问题描述:
我有一个应用程序使用BLE。在某些情况下,例如安装在iPhone6上时,该应用程序正在运行,并未要求使用BLE的权限。蓝牙LE询问权限?
在其他情况下,例如我的iPad Air,应用程序开始运行,它不要求用户许可BLE,并且BLE不工作(虽然设备上的蓝牙已打开)。
我不明白,当自动许可请求发生? 如何启用特定的应用程序在iDevice设置中使用BLE? 我该如何申请应用程序内的权限?
答
使用下面的代码并调用函数[self detectBluetooth];
要检查蓝牙连接
#import <CoreBluetooth/CoreBluetooth.h>
#pragma mark - setUp bluetoothManager
//check bluetooth connection
- (void)detectBluetooth
{
if(!self.bluetoothManager)
{
// Put on main queue so we can call UIAlertView from delegate callbacks.
self.bluetoothManager = [[CBCentralManager alloc] initWithDelegate:self queue:dispatch_get_main_queue()] ;
}
[self centralManagerDidUpdateState:self.bluetoothManager]; // Show initial state
}
- (void)centralManagerDidUpdateState:(CBCentralManager *)central
{
NSString *stateString = nil;
switch(bluetoothManager.state)
{
case CBCentralManagerStateResetting:
[self alertStatus:@"The connection with the system service was momentarily lost, update imminent." :@"update imminent" :0];
break;
case CBCentralManagerStateUnsupported:
[self alertStatus:@"The platform doesn't support Bluetooth Low Energy.":@"weak Bluetooth Connection" :0];
break;
case CBCentralManagerStateUnauthorized:
[self alertStatus:@"The app is not authorized to use Bluetooth Low Energy.":@"weak Bluetooth Connection" :0];
break;
case CBCentralManagerStatePoweredOff:
stateString = @"Bluetooth is currently powered off.";
[self alertStatus:@"Bluetooth is currently powered off , powered ON first.":@"No Bluetooth Connection" :0];
break;
case CBCentralManagerStatePoweredOn:
stateString = @"Bluetooth is currently powered on and available to use.";
// [self alertStatus:@"Bluetooth is currently powered ON.":@" Bluetooth available" :0];
break;
default:
stateString = @"State unknown, update imminent.";
break;
}
NSLog(@"Bluetooth State %@",stateString);
}
@end
答
它不是一个权限问题,但你可能会遇到在您的蓝牙是不是还没有通电的情况。
我需要看看你在哪里放置你的scanForPeripheralsWithServices
调用,因为它调用得太早总会导致失败的调用。
相反,这里把它确保蓝牙已准备好开始扫描:
- (void) centralManagerDidUpdateState: (CBCentralManager *) central {
if (self.centralManager.state == CBCentralManagerStatePoweredOn) {
NSArray *services = @[];
[self.centralManager scanForPeripheralsWithServices:services options:nil];
} else {
NSLog(@"Failed to start scan, returned error code: %li",(long)self.centralManager.state);
}
}
真的吗?这个问题缺乏任何代码,但我知道,当BT处于未知状态时,你不能请求某些东西,这是始终开始的地方。 – CodeBender 2016-05-06 21:18:23