3

我想使用 CoreBluetooth(因为我的应用程序必须符合 App Store 的条件)来检查所有当前配对和连接的蓝牙设备。这应该不需要任何蓝牙扫描,对吧?我只是想看看系统与什么配对。如果我做不到,第二个选择是扫描。

我正在尝试的方法不起作用。它说蓝牙未通电并崩溃,但 CBCentralManager 的状态为开启!关于如何解决这个问题的任何想法,还是我完全偏离了轨道?

现在所有这些都在 ViewController 中:

    - (void)viewDidLoad{

    [super viewDidLoad];
    self.centralManager = [[CBCentralManager alloc] initWithDelegate:self queue:dispatch_get_main_queue()];

    [self.centralManager retrieveConnectedPeripherals]; //makes the system call didRetrieveConnectedPeripherals
}

- (void)centralManager:(CBCentralManager *)central didRetrieveConnectedPeripherals:(NSArray *)peripherals{
    NSLog(@"didRetrieveConnectedPeripherals called");
    for (CBPeripheral *a in peripherals){
        NSLog(a.name); //just log the name for now to see if it recognized it
    } //but it never ends up logging anything, and I have a BT keyboard paired/connected with the iPhone 5
} //and I get an error at some point, <CBConcreteCentralManager: 0x71ab020> is not powered on

- (void)centralManagerDidUpdateState:(CBCentralManager *)manager{
    if ([manager state] == CBCentralManagerStatePoweredOff) NSLog(@"CBCentralManagerStatePoweredOff");
    if ([manager state] == CBCentralManagerStatePoweredOn) NSLog(@"CBCentralManagerStatePoweredOn"); //this is what gets logged when I run it on an iPhone 5
    if ([manager state] == CBCentralManagerStateResetting) NSLog(@"CBCentralManagerStateResetting");
    if ([manager state] == CBCentralManagerStateUnauthorized) NSLog(@"CBCentralManagerStateUnauthorized");
    if ([manager state] == CBCentralManagerStateUnknown) NSLog(@"CBCentralManagerStateUnknown");
    if ([manager state] == CBCentralManagerStateUnsupported) NSLog(@"CBCentralManagerStateUnsupported");
}
4

1 回答 1

0

我也一直在努力,希望我学到的一些东西会有所帮助。

几件事:

1)您可能会收到“蓝牙未开机”错误,因为您[self.central retrieveConnectedPeripherals]在初始化 CBCentralManager 后立即调用。您需要给 CBCentralManager 足够的时间来连接蓝牙硬件。

试试这个,

- (void)viewDidLoad{
    [super viewDidLoad];
    self.centralManager = [[CBCentralManager alloc] initWithDelegate:self queue:nil];
}
- (void)centralManagerDidUpdateState:(CBCentralManager *)manager {
   switch (manager.state) {
        case CBCentralManagerStatePoweredOn:
             NSLog(@"CBCentral Manager powered on");
             [self.centralManager retrieveConnectedPeripherals];
             break;
        case CBCentralManagerStatePoweredOff:
             ...etc.
}

另外,请注意 CBCentralManager 适用于具有蓝牙 4.0 的 iOS 设备。目前,蓝牙 4 安装在 iPhone 4S/5、iPod 4 和 iPad 3/4/mini 上。因此,请注意您的应用程序无法在 iPad 1/2、iPhone 2/3 和 iPod 2/3 上运行。

仍在弄清楚这一点,但您也可能看不到您的键盘,因为它不是蓝牙 4 设备。让我知道你发现了什么。

于 2013-08-13T19:51:08.713 回答