gpt4 book ai didi

objective-c - 是什么导致 iOS 中的 CBCentralManager State Unknown?

转载 作者:技术小花猫 更新时间:2023-10-29 10:44:46 24 4
gpt4 key购买 nike

为什么我在 iPad 2 上使用这个简单的代码时会得到 CBCentralManagerStateUnknown

- (BOOL)viewDidLoad {

bluetoothManager = [[CBCentralManager alloc] initWithDelegate:self queue:nil];

if ([manager state] == CBCentralManagerStatePoweredOff) NSLog(@"CBCentralManagerStatePoweredOff");
if ([manager state] == CBCentralManagerStatePoweredOn) NSLog(@"CBCentralManagerStatePoweredOn");
if ([manager state] == CBCentralManagerStateResetting) NSLog(@"CBCentralManagerStateResetting");
if ([manager state] == CBCentralManagerStateUnauthorized) NSLog(@"CBCentralManagerStateUnauthorized");
if ([manager state] == CBCentralManagerStateUnknown) NSLog(@"CBCentralManagerStateUnknown");
if ([manager state] == CBCentralManagerStateUnsupported) NSLog(@"CBCentralManagerStateUnsupported");

}

我不明白 CBCentralManagerStateUnknown 是什么意思。我该怎么办? Apple docs就说:

State unknown, update imminent.

我在连接蓝牙设备时收到此响应,在蓝牙关闭时也收到此响应。如果我尝试运行类似 [manager retrieveConnectedPeripherals] 的程序,我还会在控制台中收到此消息:

CoreBluetooth[WARNING] <CBConcreteCentralManager: ...> is not powered on

最佳答案

我知道为什么从不调用委托(delegate)。因为对象从内存中删除了。就做一个强大的属性

@property (strong, nonatomic) DiscoverBluetoothDevices *btDevices;

在初始化

@implementation DiscoverBluetoothDevices
- (id) init
{
self = [super init];
if(self) {
centralManager = [[CBCentralManager alloc] initWithDelegate:self queue:dispatch_get_main_queue()];
[centralManager scanForPeripheralsWithServices:nil options:@{CBCentralManagerScanOptionAllowDuplicatesKey: @YES}];

}
return self;
}

现在委托(delegate)被正确调用了。

关于objective-c - 是什么导致 iOS 中的 CBCentralManager State Unknown?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12518562/

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