如何在iOS中找到蓝牙音频设备

前端之家收集整理的这篇文章主要介绍了如何在iOS中找到蓝牙音频设备前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
好吧,我正在开发一个有趣的项目,这个项目有一个障碍,我需要为我的iOS应用程序启用蓝牙音频支持.

我所处的障碍是,我根本无法得到连接的蓝牙音频设备的列表.即使我的iPhone 5S识别我的耳机(一个〜3 – 4年LG HBM-230,准确),并播放音频通过它的电话,BOTH外部配件和CoreBluetooth给我没有任何有用的,当我查询两者.

我把自己的代码放在问题&我发现CoreBluetoothExternal Accessory框架的答案.

当我的代码只是试图为“蓝牙设备”显示和连接的任何蓝牙设备“scanForPeripheralsWithServices:nil”时,下面的代码根本就没有出现在控制台中的“CBCentralManagerStatePoweredOn”消息之外.

而在我的代码中的这一行(有一个有效的EAAccessoryManager实例)

NSArray * connectedDevices = [self.eAAccessoryManager connectedAccessories];

也返回一个零数组.

我该怎么做错了?

B.T.W.,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

解决方法

首先,您将需要配置应用程序音频会话以允许支持音频的蓝牙连接.你可以这样做,例如你的应用程序委托 – (BOOL)应用程序:(UIApplication *)应用程序didFinishLaunchingWithOptions

猜你在找的iOS相关文章