我有一個應用程序使用BLE。在某些情況下,例如安裝在iPhone6上時,該應用程序正在運行,並未要求使用BLE的權限。藍牙LE詢問權限?
在其他情況下,例如我的iPad Air,應用程序開始運行,它不要求用戶許可BLE,並且BLE不工作(雖然設備上的藍牙已打開)。
我不明白,當自動許可請求發生? 如何啓用特定的應用程序在iDevice設置中使用BLE? 我該如何申請應用程序內的權限?
我有一個應用程序使用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