简体   繁体   English

如何以编程方式扫描并连接到iOS上的Bluetooth A2DP设备

[英]How to scan and connect to Bluetooth A2DP device on iOS programmatically

I have built an Android APP which handles the scan, return nearby devices, and connect steps on both BLE and Bluetooth A2DP, and it works well. 我已经构建了一个Android APP,可以处理扫描,返回附近的设备以及在BLE和Bluetooth A2DP上连接步骤,并且运行良好。 Now I'm developing the iOS version which is with exactly the same functionalities. 现在,我正在开发功能完全相同的iOS版本。 For the BLE part, I can use CoreBluetooth to perform what I need without any problems, yet I don't know how to implement the steps of “scan -> return nearby discoverable devices -> connect” on iOS for Bluetooth A2DP device. 对于BLE部分,我可以使用CoreBluetooth毫无问题地执行所需的操作,但是我不知道如何在iOS上为Bluetooth A2DP设备执行“扫描->返回附近可发现的设备->连接”的步骤。 The only solution I've found so far is to navigate to Settings page from my iOS APP and perform the connection on it. 到目前为止,我发现的唯一解决方案是从iOS APP导航到“设置”页面并在其上执行连接。 Is there any way to implement the Bluetooth A2DP connection process inside of my iOS APP programmatically? 有什么方法可以在我的iOS APP内部以编程方式实现Bluetooth A2DP连接过程吗?

in iOS, bluetooth works on central peripheral concept. 在iOS中,蓝牙适用于中央外围设备概念。 below is the how it scan for nearby devices. 以下是它如何扫描附近的设备。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在iPhone上使用A2DP连接到远程设备 - How to connect to a remote device using A2DP on iPhone iOS上的蓝牙A2DP API支持? - Bluetooth A2DP API Support on iOS? 是否可以将十六进制数据快速发送到 A2DP 蓝牙设备? - Is possible send hexadecimal data to A2DP Bluetooth Device in swift? AVAudioSession 不会自动连接蓝牙 A2DP 音频? - AVAudioSession does not automatically connect to Bluetooth A2DP audio? iOS蓝牙双模; 同时将BLE(GATT)连接到已连接的BR / EDR(A2DP / HFP)立体声耳机 - iOS Bluetooth dual-mode; connect BLE (GATT) to an already connected BR/EDR (A2DP/HFP) stereo headset simultaneously 如何检测 iOS 中是否连接了 HFP 或 A2DP? - How can I detect whether an HFP or A2DP is connected in iOS? 如何扫描并连接到 Xamarin forms 中 iOS 的蓝牙经典版? - How to scan and connect to Bluetooth classic for iOS in Xamarin forms? 如何使用蓝牙4.0(BLE)如何使A2DP蓝牙配置文件工作? 我需要AptX编解码器吗? - How are you able to get an A2DP bluetooth profile to work using bluetooth 4.0 (BLE)? Do I need an AptX codec? 在iOS中以编程方式连接蓝牙耳机吗? - Connect Bluetooth headset programmatically in iOS? 蓝牙LE设备从iOS后台扫描 - Bluetooth LE Device scan in background from iOS
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM