[英]How to scan and connect to Bluetooth A2DP device on iOS programmatically
我已經構建了一個Android APP,可以處理掃描,返回附近的設備以及在BLE和Bluetooth A2DP上連接步驟,並且運行良好。 現在,我正在開發功能完全相同的iOS版本。 對於BLE部分,我可以使用CoreBluetooth毫無問題地執行所需的操作,但是我不知道如何在iOS上為Bluetooth A2DP設備執行“掃描->返回附近可發現的設備->連接”的步驟。 到目前為止,我發現的唯一解決方案是從iOS APP導航到“設置”頁面並在其上執行連接。 有什么方法可以在我的iOS APP內部以編程方式實現Bluetooth 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);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.