我想使用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");
}
答案 0 :(得分:0)
我也一直在努力,希望我学到的一些东西会有所帮助。
一些事情:
1)您可能会因为在初始化CBCentralManager后立即调用[self.central retrieveConnectedPeripherals]
而导致“蓝牙未启动”错误。您需要给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设备。让我知道你发现了什么。