gpt4 book ai didi

ios - xcode:如何在条件操作中使用蓝牙状态

转载 作者:行者123 更新时间:2023-11-29 01:47:54 25 4
gpt4 key购买 nike

逻辑是这样的:

if (blueTooth is on){
performSegueToPageA
}
else{
performSegueToPageB
}

但我一辈子都不知道该怎么做。首先,这些过于复杂(就 Stack Overflow 而言,并不总是有效,具体取决于 iOS 版本或风向)CBCentralManager 废话。

其次,我该如何调用和构造它以使其返回 BOOL 值?

假设我只针对 iOS7+。

这是我到目前为止得到的:

登录.h:

@interface bla bla blah <AmongOtherThings, CBCentralManagerDelegate>
@property (nonatomic, strong) CBCentralManager* blueToothManager;

登录.m:

#import <CoreBluetooth/CoreBluetooth.h>
- (void)viewDidLoad {
[self detectBluetooth]
}

- (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: 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;
}
NSLog(@"%@", stateString);
}

作为引用,关于代码,看here .值得注意的是,我必须向 centralManagerDidUpdateState 中的 blueToothManager.state 添加下划线,因为没有它代码将无法构建。

照原样,它运行良好。现在,如何让它返回 BOOL 值?

我认为这并不像用 void 代替 BOOL(我试过)那么简单。

谢谢。

最佳答案

在您的 centralManagerDidUpdateState 函数中,您可以简单地检查:

if (self.blueToothManager.state == CBCentralManagerStatePoweredOn)
{
//Do something
}
else
{
//Do something else
}

关于ios - xcode:如何在条件操作中使用蓝牙状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31696431/

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