[英]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.