11

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

- (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意思。我该怎么办?苹果文档只是说:

状态未知,即将更新。

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

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

6 回答 6

16

我知道为什么从不调用委托。因为对象是从内存中删除的。只做强属性

@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;
}

现在委托被正确调用。

于 2013-03-30T16:15:55.447 回答
3

CBCentralManagerStateUnknown只是意味着iOS已经启动了BLE进程,但还没有完成初始化。稍等片刻,状态就会改变。

通常,您将通过检测CBCentralManagerDelegate委托处理程序中的状态变化来“给它一点时间”,而不是在初始化调用之后立即查看它。您将实施

- (void) centralManagerDidUpdateState: (CBCentralManager *) central;

有一些很好的例子可以证明这一点,例如 Apple 的心率监测器

于 2012-09-20T21:26:43.137 回答
2

如果中央的状态变为CBCentralManagerStateUnsupported(而设备支持低功耗蓝牙),则很可能意味着该应用程序对 CoreBluetooth 做了一些坏事。

检查iOS 蓝牙诊断日志记录。

例如,如果您这样做...

_cm1 = [[CBCentralManager alloc] initWithDelegate:self queue:nil options:@{ CBCentralManagerOptionRestoreIdentifierKey: @"not_unique" }]; _cm2 = [[CBCentralManager alloc] initWithDelegate:self queue:nil options:@{ CBCentralManagerOptionRestoreIdentifierKey: @"not_unique" }];

...第二中央的状态将变为CBCentralManagerStateUnsupported

于 2015-05-06T21:19:30.107 回答
1

实际答案(我知道的老问题);开始扫描外围设备,这将启动 BT LE,您的代表将被回调。在我这样做之前,我的代表和州信息没有改变。

一种。如下设置您的 cbcentralmanager b。在您的代码和 .h 文件中使用 -central* 代表 c。NSLog 或在屏幕上使用新状态更新标签。还有……成功。

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

[cManager scanForPeripheralsWithServices:nil options:@{CBCentralManagerScanOptionAllowDuplicatesKey: @YES}];
于 2013-03-23T06:32:16.857 回答
1

您需要保留CBCentralManager实例(将其放在 ivar 或私有属性中)并等待调用状态更改委托。(如果您在实例化管理器后立即检查状态,则该状态始终为“未知”。真实状态将暂时出现在委托方法中。)

于 2013-08-28T12:52:23.323 回答
0

就我而言,我确实使用 AppDelegate 作为代表

CBCentralManagerDelegate

和无方向的

 AVCaptureMetadataOutputObjectsDelegate.

一次。

1)注意线程。采用

dispatch_get_main_queue() 

或者

[NSThread mainThread]

与BLE一起工作。

2) 注意在 1 个对象上使用这 2 个委托。因为硬件不是主题和上下文保存

于 2015-08-10T19:24:25.550 回答