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



我已经构建了一个Android应用程序,该应用程序可以处理扫描,返回附近的设备并连接BLE和蓝牙A2DP上的步骤,并且效果很好。现在,我正在开发具有完全相同功能的iOS版本。对于BLE部件,我可以使用CoreBluetooth在没有任何问题的情况下执行我需要的内容,但我不知道如何在蓝牙A2DP设备上实现iOS的"扫描 ->返回附近可发现设备 -> Connect"的步骤。到目前为止,我唯一发现的解决方案是从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); 
} 

最新更新