【发布时间】:2014-01-20 16:51:41
【问题描述】:
好的,我正在做一个有趣的项目,但我需要为我的 iOS 应用启用蓝牙音频支持。
我遇到的障碍是我什至无法开始获取已连接蓝牙音频设备的列表。尽管我的 iPhone 5S 可以识别我的听筒(准确地说是 3 - 4 岁 LG HBM-230)并通过它播放音频以进行通话,BOTH当我同时查询时,CoreBluetooth 并没有给我任何有用的信息。
我的代码基于我为CoreBluetooth 和External Accessory 框架找到的问题和答案。
当我的代码只是尝试“scanForPeripheralsWithServices:nil”为设置->蓝牙说是可见和连接的 任何蓝牙设备时,下面的代码根本不会出现超出控制台中的“CBCentralManagerStatePoweredOn”消息。
我的代码中的这一行(带有有效的 EAAccessoryManager 实例)
NSArray * connectedDevices = [self.eAAccessoryManager connectedAccessories];
还返回一个 nil 数组。
我做错了什么?
顺便说一句,I've made this code available as a GitHub project.
@implementation BluetoothManager
+ (BluetoothManager *)sharedInstance
{
static dispatch_once_t pred = 0;
__strong static id _bluetoothMGR = nil;
dispatch_once(&pred, ^{
_bluetoothMGR = [[BluetoothManager alloc] init];
});
return _bluetoothMGR;
}
- (id)init
{
self = [super init];
if(self)
{
dispatch_queue_t centralQueue = dispatch_queue_create("com.yo.mycentral", DISPATCH_QUEUE_SERIAL);
// whether we try this on a queue of "nil" (the main queue) or this separate thread, still not getting results
self.cbManager = [[CBCentralManager alloc] initWithDelegate:self queue:centralQueue options:nil];
}
return self;
}
// this would hit.... if I instantiated this in a storyboard of XIB file
- (void)awakeFromNib
{
if(!self.cbManager)
self.cbManager = [[CBCentralManager alloc] initWithDelegate:self queue:nil options:nil];
}
- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI {
NSLog(@"hey I found %@",[advertisementData description]);
}
- (void)centralManager:(CBCentralManager *)central didRetrieveConnectedPeripherals:(NSArray *)peripherals
{
NSLog( @"I retrieved CONNECTED peripherals");
}
-(void)centralManager:(CBCentralManager *)central didRetrievePeripherals:(NSArray *)peripherals{
NSLog(@"This is it!");
}
- (void)centralManagerDidUpdateState:(CBCentralManager *)central{
NSString *messtoshow;
switch (central.state) {
case CBCentralManagerStateUnknown:
{
messtoshow=@"State unknown, update imminent.";
break;
}
case CBCentralManagerStateResetting:
{
messtoshow=@"The connection with the system service was momentarily lost, update imminent.";
break;
}
case CBCentralManagerStateUnsupported:
{
messtoshow=@"The platform doesn't support Bluetooth Low Energy";
break;
}
case CBCentralManagerStateUnauthorized:
{
messtoshow=@"The app is not authorized to use Bluetooth Low Energy";
break;
}
case CBCentralManagerStatePoweredOff:
{
messtoshow=@"Bluetooth is currently powered off.";
break;
}
case CBCentralManagerStatePoweredOn:
{
messtoshow=@"Bluetooth is currently powered on and available to use.";
NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:YES], CBCentralManagerScanOptionAllowDuplicatesKey, nil];
[_cbManager scanForPeripheralsWithServices:nil options:options];
break;
}
}
NSLog(@"%@", messtoshow);
}
@end
【问题讨论】:
-
你的设备支持 BLE 吗?
-
核心蓝牙只支持低功耗设备
-
感谢@UndercoverDeveloper 和 Bamsworld,我确实在各个地方读到过有关 CB 仅适用于 BLE 设备的信息。我仍然想知道 iOS 如何在 Settings -> Bluetooth 中识别和列出设备,但我也无法从 ExternalAccessory 框架中获得任何爱。是否有一些其他可能的方式从我的可能非标准设备获取设备信息?
-
有最新的 iBeacon API,但我认为它也仅适用于 BLE:/
-
CoreBluetooth.framework 在这里无关紧要。它仅适用于 BLE(而不是“经典”蓝牙)。 BLE 不支持音频。
标签: ios iphone audio core-bluetooth external-accessory