如何通过编程方式扫描并连接到iOS上的蓝牙A2DP设备

如何通过编程方式扫描并连接到iOS上的蓝牙A2DP设备,ios,xcode,bluetooth,a2dp,Ios,Xcode,Bluetooth,A2dp,我已经建立了一个Android应用程序,它可以处理扫描,返回附近的设备,并在BLE和蓝牙A2DP上连接步骤,而且运行良好。现在我正在开发具有完全相同功能的iOS版本。对于可探测部分,我可以使用CoreBlutooth来执行我需要的操作,而没有任何问题,但我不知道如何在iOS上为Bluetooth A2DP设备实现“扫描->返回附近可探测设备->连接”的步骤。到目前为止,我找到的唯一解决方案是从我的iOS应用程序导航到设置页面并在其上执行连接。有没有办法在我的iOS应用程序中以编程方式实现蓝牙A2

我已经建立了一个Android应用程序,它可以处理扫描,返回附近的设备,并在BLE和蓝牙A2DP上连接步骤,而且运行良好。现在我正在开发具有完全相同功能的iOS版本。对于可探测部分,我可以使用CoreBlutooth来执行我需要的操作,而没有任何问题,但我不知道如何在iOS上为Bluetooth A2DP设备实现“扫描->返回附近可探测设备->连接”的步骤。到目前为止,我找到的唯一解决方案是从我的iOS应用程序导航到设置页面并在其上执行连接。有没有办法在我的iOS应用程序中以编程方式实现蓝牙A2DP连接过程

在iOS中,蓝牙在中央外围设备概念上工作。下面是它如何扫描附近的设备

#import <UIKit/UIKit.h>
#import <CoreBluetooth/CoreBluetooth.h>


@interface MyViewController : UIViewController <CBPeripheralDelegate, CBCentralManagerDelegate>
{
    CBCentralManager *mgr;
}
@property (readwrite, nonatomic) CBCentralManager *mgr;
@end



- (void)viewDidLoad
{
    [super viewDidLoad];

    mgr = [[CBCentralManager alloc] initWithDelegate:self queue:nil];
}


- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI {


    NSLog([NSString stringWithFormat:@"%@",[advertisementData description]]);
}

-(void)centralManager:(CBCentralManager *)central didRetrievePeripherals:(NSArray *)peripherals{
    NSLog(@"This is it!");
}


- (void)centralManagerDidUpdateState:(CBCentralManager *)central{ 
    NSString *messtoshow;

    switch (central.state) {
        case CBCentralManagerStateUnknown:
        {
            messtoshow=[NSString stringWithFormat:@"State unknown, update imminent."];
            break;
        }
        case CBCentralManagerStateResetting:
        {
            messtoshow=[NSString stringWithFormat:@"The connection with the system service was momentarily lost, update imminent."];
            break;
        }
        case CBCentralManagerStateUnsupported:
        {
            messtoshow=[NSString stringWithFormat:@"The platform doesn't support Bluetooth Low Energy"];
            break;
        }
        case CBCentralManagerStateUnauthorized:
        {
            messtoshow=[NSString stringWithFormat:@"The app is not authorized to use Bluetooth Low Energy"];
            break;
        }
        case CBCentralManagerStatePoweredOff:
        {
            messtoshow=[NSString stringWithFormat:@"Bluetooth is currently powered off."];
            break;
        }
        case CBCentralManagerStatePoweredOn:
        {
            messtoshow=[NSString stringWithFormat:@"Bluetooth is currently powered on and available to use."];
            [mgr scanForPeripheralsWithServices:nil options:nil];
            //[mgr retrieveConnectedPeripherals];

//--- it works, I Do get in this area!

            break;
        }   

    }
    NSLog(messtoshow); 
} 
#导入
#进口
@接口MyViewController:UIViewController
{
中央银行经理*经理;
}
@属性(读写,非原子)CBCentralManager*mgr;
@结束
-(无效)viewDidLoad
{
[超级视图下载];
mgr=[[CBCentralManager alloc]initWithDelegate:自队列:nil];
}
-(无效)centralManager:(CBCentralManager*)central DidDiscoveryPeripheral:(CBPeripheral*)peripheral广告数据:(NSDictionary*)广告数据RSSI:(NSNumber*)RSSI{
NSLog([NSString stringWithFormat:@“%@,[advertisementData description]]);
}
-(无效)中央管理器:(CBCentralManager*)中央数据检索外围设备:(NSArray*)外围设备{
NSLog(@“就是这样!”);
}
-(void)CentralManagerDipDateState:(CBCentralManager*)central{
NSString*messtoshow;
开关(中央状态){
案例CBCentralManager状态未知:
{
messtoshow=[NSString stringWithFormat:@“状态未知,即将更新。”];
打破
}
案例CBCentralManager状态重置:
{
messtoshow=[NSString stringWithFormat:@“与系统服务的连接暂时中断,即将更新。”;
打破
}
案例CBCentralManager状态不受支持:
{
messtoshow=[NSString stringWithFormat:@“平台不支持蓝牙低能量”];
打破
}
案例CBCentralManager未经授权:
{
messtoshow=[NSString stringWithFormat:@“应用程序未被授权使用蓝牙低能量”];
打破
}
案例CBCentralManager状态断电:
{
messtoshow=[NSString stringWithFormat:@“蓝牙当前已关机。”];
打破
}
案例CBCentralManager状态通电:
{
messtoshow=[NSString stringWithFormat:@“蓝牙当前已通电并可用。”;
[经理扫描外围设备服务:无选项:无];
//[经理检索连接的外围设备];
//---行得通,我真的到这地方了!
打破
}   
}
NSLog(messtoshow);
} 

我实现了您之前在我的应用程序中发布的代码,但我注意到它只适用于BLE而不适用于蓝牙A2DP设备,不是吗?你知道如何在iOS上扫描和连接蓝牙A2DP吗?谢谢!您无法在iOS上的应用程序中发现并连接到旧版蓝牙设备。用户必须在“设置”中执行此操作。例外情况是,如果您是设备制造商,并且您的设备经过mfi认证