gpt4 book ai didi

ios - CBManager 状态始终未知

转载 作者:行者123 更新时间:2023-11-29 12:37:22 25 4
gpt4 key购买 nike

我正在尝试检查设备的蓝牙是打开还是关闭。这是我到目前为止编写的代码

   CBCentralManager *cbManager = [[CBCentralManager alloc] initWithDelegate:self
queue:nil
options:
[NSDictionary dictionaryWithObject:[NSNumber numberWithInt:0]
forKey:CBCentralManagerOptionShowPowerAlertKey]];


[cbManager scanForPeripheralsWithServices:nil options:
[NSDictionary dictionaryWithObject:[NSNumber numberWithInt:0]
forKey:CBCentralManagerOptionShowPowerAlertKey]];

if (cbManager.state==CBCentralManagerStatePoweredOff)
{
//do stuff
}

- (void)centralManagerDidUpdateState:(CBCentralManager *)central
{
NSString *stateString = nil;
switch(bluetoothManager.state)
{
case CBCentralManagerStateResetting: stateString = @"The connection with the system service was momentarily lost, update imminent."; break;
case CBCentralManagerStateUnsupported: stateString = @"The platform doesn't support Bluetooth Low Energy."; break;
case CBCentralManagerStateUnauthorized: stateString = @"The app is not authorized to use Bluetooth Low Energy."; break;
case CBCentralManagerStatePoweredOff: stateString = @"Bluetooth is currently powered off."; break;
case CBCentralManagerStatePoweredOn: stateString = @"Bluetooth is currently powered on and available to use."; break;
default: stateString = @"State unknown, update imminent."; break;
}
UIAlertView *alert = [[[UIAlertView alloc] initWithTitle:@"Bluetooth state"
message:stateString
delegate:nil
cancelButtonTitle:@"Okay" otherButtonTitleArray:nil] autorelease];
[alert show];
}

问题是无论设备上的蓝牙是否打开,管理器的状态始终为未知。关于为什么会发生这种情况的任何想法?

最佳答案

两件事-

  1. 您的CBCentralManager 应该是一个属性,否则它会在您初始化它的方法退出时立即释放

  2. 在您处于开机状态之前,您不应该调用 scanForPeripheralsWithServices

    @property (strong,nonatomic) CBCentralManager *cbManager;



    self.cbManager = [[CBCentralManager alloc] initWithDelegate:self
    queue:nil
    options:
    [NSDictionary dictionaryWithObject:[NSNumber numberWithInt:0]
    forKey:CBCentralManagerOptionShowPowerAlertKey]];


    - (void)centralManagerDidUpdateState:(CBCentralManager *)central
    {
    NSString *stateString = nil;
    switch(central.state)
    {
    case CBCentralManagerStateResetting:
    stateString = @"The connection with the system service was momentarily lost, update imminent.";
    break;
    case CBCentralManagerStateUnsupported:
    stateString = @"The platform doesn't support Bluetooth Low Energy.";
    break;
    case CBCentralManagerStateUnauthorized:
    stateString = @"The app is not authorized to use Bluetooth Low Energy.";
    break;
    case CBCentralManagerStatePoweredOff:
    stateString = @"Bluetooth is currently powered off.";
    break;
    case CBCentralManagerStatePoweredOn:
    stateString = @"Bluetooth is currently powered on and available to use.";
    [central scanForPeripheralsWithServices:nil options:
    [NSDictionary dictionaryWithObject:[NSNumber numberWithInt:0]
    forKey:CBCentralManagerOptionShowPowerAlertKey]];
    break;
    default:
    stateString = @"State unknown, update imminent.";
    break;
    }
    UIAlertView *alert = [[[UIAlertView alloc] initWithTitle:@"Bluetooth state"
    message:stateString
    delegate:nil
    cancelButtonTitle:@"Okay" otherButtonTitleArray:nil] autorelease];
    [alert show];
    }

关于ios - CBManager 状态始终未知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25932366/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com