如何以编程方式在iOS上扫描并连接蓝牙A2DP设备

时间:2018-04-25 09:12:56

标签: ios xcode bluetooth a2dp

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

1 个答案:

答案 0 :(得分:-2)

在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); 
}