gpt4 book ai didi

bluetooth - iOS 8 如何检测蓝牙耳机是否已插入?

转载 作者:行者123 更新时间:2023-12-02 23:40:34 25 4
gpt4 key购买 nike

在我的项目中,我使用 AVAudioSession 来检测任何耳机的插入或拔出。但在这种情况下,我无法检测到蓝牙设备何时插入。这是我的耳机状态代码。

 - (void)audioRouteChangeListenerCallback:(NSNotification*)notification
{

NSDictionary *interuptionDict = notification.userInfo;

NSInteger routeChangeReason = [[interuptionDict valueForKey:AVAudioSessionRouteChangeReasonKey] integerValue];

switch (routeChangeReason) {

case AVAudioSessionRouteChangeReasonNewDeviceAvailable:
//NSLog(@"AVAudioSessionRouteChangeReasonNewDeviceAvailable");

NSLog(@"Headphone/Line plugged in");

[_soundButtonOutlet setImage:[UIImage imageNamed:@"sound-on.png"] forState:UIControlStateNormal];

_headSetState=YES;

break;

case AVAudioSessionRouteChangeReasonOldDeviceUnavailable:
NSLog(@"AVAudioSessionRouteChangeReasonOldDeviceUnavailable");

NSLog(@"Headphone/Line was pulled. Stopping player....");

[_soundButtonOutlet setImage:[UIImage imageNamed:@"sound-off.png"] forState:UIControlStateNormal];
if(_isPlaying==YES)
{


[self.player pause];

[_audioButtonOutlet setImage:[UIImage imageNamed:@"play.png"] forState:UIControlStateNormal];

_isPlaying=NO;

}
_headSetState=NO;

break;

case AVAudioSessionRouteChangeReasonCategoryChange:
// called at start - also when other audio wants to play
NSLog(@"AVAudioSessionRouteChangeReasonCategoryChange");


break;
}



- (BOOL)isHeadsetPluggedIn

{

AVAudioSessionRouteDescription* route = [[AVAudioSession sharedInstance] currentRoute];
for (AVAudioSessionPortDescription* desc in [route outputs]) {

if ([[desc portType] isEqualToString:AVAudioSessionPortHeadphones])
{
[_soundButtonOutlet setImage:[UIImage imageNamed:@"sound-on.png"] forState:UIControlStateNormal];
_headSetState=YES;
return YES;
}
else
{
[_soundButtonOutlet setImage:[UIImage imageNamed:@"sound-off.png"] forState:UIControlStateNormal];
_headSetState=NO;
return NO;

}
}


return NO;
}

}


- viewWillAppear {

[AVAudioSession sharedInstance];

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(audioRouteChangeListenerCallback:) name:AVAudioSessionRouteChangeNotification object:nil];

[self isHeadsetPluggedIn];

}

那么 iOS 8 如何检测蓝牙耳机是否已插入?

最佳答案

您可以检测当前事件的蓝牙输出设备(而不是输入设备)

Swift 代码:

import AVFoundation
func bluetoothAudioConnected() -> Bool{
let outputs = AVAudioSession.sharedInstance().currentRoute.outputs
for output in outputs{
if output.portType == AVAudioSessionPortBluetoothA2DP || output.portType == AVAudioSessionPortBluetoothHFP || output.portType == AVAudioSessionPortBluetoothLE{
return true
}
}
return false
}

蓝牙设备基于以下问题:What's the difference among AVAudioSessionPortBluetoothHFP, A2DP and LE?

希望对大家有帮助

<小时/>

针对 Swift 5.1 进行编辑(感谢 iago849 的修复)

var bluetoothDeviceConnected: Bool {
!AVAudioSession.sharedInstance().currentRoute.outputs.compactMap {
($0.portType == .bluetoothA2DP ||
$0.portType == .bluetoothHFP ||
$0.portType == .bluetoothLE) ? true : nil
}.isEmpty
}

关于bluetooth - iOS 8 如何检测蓝牙耳机是否已插入?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28928876/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com