簡體   English   中英

iOS如何發現藍牙耳機及其事件

[英]iOS how to discover bluetooth headset and its events

我正在嘗試發現“藍牙耳機”並獲取其事件。 我閱讀了“ 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
{

}

就像提到的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. 如果要在后台運行時獲取回調,則需要在目標的功能上添加Audio和AirPlay:
    在此處輸入圖片說明

!! 這個答案對我有幫助

CoreBluetooth.framework用於低功耗藍牙。
藍牙低能耗並非用於傳遞聲音(退出耳機,揚聲器等)
所以我的問題是:您確定您的耳機正在使用低功耗藍牙嗎?
我不這么認為。

因此,這就是為什么未觸發委托方法centralManager:didDiscoverPeripheral:的原因。

如果要獲取耳機的一些事件,例如“用戶按下的下一首歌曲”,則可以使用“ 遠程控制事件” 我懷疑您可能想聽“其他事件”,所以我想您的耳機在MFI之下。 因此它可能具有自己的協議。 例如,我在iOS應用程序上使用的藍牙耳機具有其他功能,例如呼叫喜歡的號碼等。但是,那么,您將需要使用ExternalAccessory.framework ,並且可能需要對協議進行反向工程。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM