6

我需要做一个能够判断我当前是否连接到经典蓝牙设备的应用程序(实际上,它将是蓝牙汽车设备)。

我的第一步是告诉当前连接的经典蓝牙设备是什么。我不能使用 CoreBluetooth,因为它仅适用于 LE。我尝试使用外部附件框架。

这是代码(一个按钮启动方法):

- (IBAction)startMethodGetConnected:(id)sender {
     NSLog(@"button taped");
     // Get the number of accessories connected
     NSUInteger NumberOfAccessoriesConnected = [[EAAccessoryManager sharedAccessoryManager].connectedAccessories count];
     //Display the number 
     NSLog(@"number of accessories connected : %d", NumberOfAccessoriesConnected);
 }

当 iPhone 连接到蓝牙键盘和蓝牙耳机时,我尝试过。在这两种情况下,控制台都显示数字为 0。

如何显示正确的数字?

4

2 回答 2

1

来自苹果文档:

“支持外部附件的应用程序必须确保正确配置其 Info.plist 文件。具体而言,您必须包含UISupportedExternalAccessoryProtocols来声明您的应用程序支持的特定硬件协议。有关此框架的更多信息,请参阅外部附件编程主题。” 这里

您需要使用 MFi 设备的协议在 Info.plist 文件中添加此密钥。

<key>UISupportedExternalAccessoryProtocols</key>
<array>
    <string>your_protocol_name</string>
</array>

问候

于 2016-03-17T19:05:52.413 回答
1

你不能。您可以做的是检查播放路线。问题是您的汽车免提将成为 HeadsetBT。这是我在我的应用程序中使用的代码。

// create and set up the audio session
AVAudioSession* audioSession = [AVAudioSession sharedInstance];
[audioSession setDelegate:self];
[audioSession setCategory: AVAudioSessionCategoryPlayAndRecord error: nil];
[audioSession setActive: YES error: nil];

// set up for bluetooth microphone input
UInt32 allowBluetoothInput = 1;
OSStatus stat = AudioSessionSetProperty (kAudioSessionProperty_OverrideCategoryEnableBluetoothInput,
                                         sizeof (allowBluetoothInput),
                                         &allowBluetoothInput
                                         );
NSLog(@"status = %x", stat);    // problem if this is not zero

// check the audio route
UInt32 size = sizeof(CFStringRef);
CFStringRef route;
OSStatus result = AudioSessionGetProperty(kAudioSessionProperty_AudioRoute, &size, &route);
NSLog(@"route = %@", route);
// if bluetooth headset connected, should be "HeadsetBT"
// if not connected, will be "ReceiverAndMicrophone"
于 2016-03-17T19:13:27.057 回答