0

我正在尝试发现“蓝牙耳机”并获取其事件。我阅读了“CoreBluetooth”文档并实现了如下示例代码。它不会触发委托方法“ didDiscoverPeripheral”。

有什么解决办法吗?

代码:

CBCentralManager *myCentralManager;
[myCentralManager scanForPeripheralsWithServices:nil options:nil];


-(void)centralManagerDidUpdateState:(CBCentralManager *)central{

    //following line prints CBCentralManagerStatePoweredOn

    NSLog(@"state:%@", [self getCentralManagerState:central.state]);
}


//following method does not fire

-(void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI
{

}
4

2 回答 2

2

CoreBluetooth.framework用于低功耗蓝牙。
低功耗蓝牙不是为传递声音(退出耳机、扬声器等)而设计的。
所以我的问题是:您确定您的耳机使用的是低功耗蓝牙吗?
我不这么认为。

因此,这centralManager:didDiscoverPeripheral:就是未触发委托方法的原因。

如果您想获取耳机的某些事件,例如“用户按下下一首歌曲”,您可以使用远程控制事件。我怀疑你可能想听“其他事件”,我猜你的耳机在 MFI 之下。所以它可能有自己的协议。例如,我为具有其他功能的蓝牙耳机开发了 iOS 应用程序,例如拨打喜欢的号码等。但是,您需要使用ExternalAccessory.framework,并且可能必须对协议进行逆向工程。

于 2014-05-21T07:21:16.677 回答
2

就像提到的那样,CoreBluetooth 仅适用于 LE 设备,
所以这就是我为 BluetoothHFP设备获取事件所做的:
1. 您需要打开 AVAudioSeesion:
链接AVFoundation.framework到您的项目
2. 对于当前可用的输入:

NSArray *availInputs = [[AVAudioSession sharedInstance] availableInputs];

3. 路线变更通知
:设置新的AVAudioSession
b. 注册观察员AVAudioSessionRouteChangeNotification

- (BOOL)prepareAudioSession {

    // deactivate session
    BOOL success = [[AVAudioSession sharedInstance] setActive:NO error: nil];
    if (!success) {
        NSLog(@"deactivationError");
    }

    // set audio session category AVAudioSessionCategoryPlayAndRecord options AVAudioSessionCategoryOptionAllowBluetooth
    success = [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord withOptions:AVAudioSessionCategoryOptionAllowBluetooth error:nil];
    if (!success) {
        NSLog(@"setCategoryError");
    }

    // activate audio session
    success = [[AVAudioSession sharedInstance] setActive:YES error: nil];
    if (!success) {
        NSLog(@"activationError");
    }

    return success;
}

并在您想开始收听更改时调用它:

[self prepareAudioSession];

NSNotificationCenter *defaultCenter = [NSNotificationCenter defaultCenter];
[defaultCenter addObserver:self
                  selector:@selector(bluetoothAvailabilityDidChange:)
                      name:@"BluetoothConnectabilityChangedNotification"
                    object:nil];
  1. 如果您想在后台获取回调,则需要在目标功能上添加音频和 AirPlay:
    在此处输入图像描述

!!得到解决方案时,这个答案对我有帮助

于 2015-03-31T11:49:57.020 回答