我已经构建了一个 Android APP,它可以处理扫描、返回附近的设备以及在 BLE 和蓝牙 A2DP 上连接步骤,并且运行良好。现在我正在开发具有完全相同功能的 iOS 版本。对于BLE部分,我可以使用CoreBluetooth毫无问题地执行我需要的操作,但我不知道如何在iOS上为蓝牙A2DP设备实现“扫描->返回附近可发现的设备->连接”的步骤。到目前为止,我发现的唯一解决方案是从我的 iOS APP 导航到“设置”页面并在其上执行连接。有没有办法以编程方式在我的 iOS APP 中实现蓝牙 A2DP 连接过程?
问问题
863 次
1 回答
-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);
}
于 2018-04-25T12:03:09.770 回答