好的,我正在做一个有趣的项目,该项目有一个障碍,我需要为我的 iOS 应用启用蓝牙音频支持。
我遇到的障碍是我什至无法开始获取已连接的蓝牙音频设备的列表。即使我的 iPhone 5S 识别出我的听筒(准确地说是 3 - 4 年的LG HBM-230)并通过它播放音频以进行电话通话,但当我查询两者时,外部附件和CoreBluetooth都没有给我任何有用的信息。
我的代码基于我为CoreBluetooth和External Accessory框架找到的问题和答案。
当我的代码只是尝试为“设置”->“蓝牙”说可见并已连接的任何scanForPeripheralsWithServices:nil
蓝牙设备“ ”时,下面的代码根本不会在控制台中的“”消息之外出现单次点击。 CBCentralManagerStatePoweredOn
我的代码中的这一行(带有有效的 EAAccessoryManager 实例)
NSArray * connectedDevices = [self.eAAccessoryManager connectedAccessories];
还返回一个 nil 数组。
我可能做错了什么?
顺便说一句,我已将此代码作为 GitHub 项目提供。
@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