iPhone如何知道是否连接了蓝牙耳机

问题描述:

使用iphone sdk 3.1.2.

using iphone sdk 3.1.2.

是否知道蓝牙耳机是否已连接到设备?不需要任何信息,除非已连接.这与通过音频会话的属性侦听器知道是否插入了一个插件不同.

Is there anyway of knowing if a Bluetooth headset is connected to the device? Don't need any info except if its connected or not. This is different from knowing if one was plugged in or not which one can do via a Property Listener of an Audio Session.

谢谢

调用此方法以查找蓝牙耳机是否已连接.

Call this method to find out the bluetooth headset is connected or not.

首先导入此框架#import <AVFoundation/AVFoundation.h>

- (BOOL) isBluetoothHeadsetConnected
    {
        AVAudioSession *session = [AVAudioSession sharedInstance];
        AVAudioSessionRouteDescription *routeDescription = [session currentRoute];

        NSLog(@"Current Routes : %@", routeDescription);

        if (routeDescription)
        {
            NSArray *outputs = [routeDescription outputs];

            if (outputs && [outputs count] > 0)
            {
                AVAudioSessionPortDescription *portDescription = [outputs objectAtIndex:0];
                NSString *portType = [portDescription portType];

                NSLog(@"dataSourceName : %@", portType);

                if (portType && [portType isEqualToString:@"BluetoothA2DPOutput"])
                {
                    return YES;
                }
            }
        }

        return NO;
    }