简体   繁体   English

获取经典的蓝牙连接设备列表(无BLE)[EAAccessoryManager]

[英]Get the list of classic bluetooth connected devices (no BLE) [EAAccessoryManager]

I need to do an app which will be able to tell if I am currently connected to a classic Bluetooth device or not (actually, it will be a Bluetooth car device). 我需要做一个应用程序,该应用程序可以判断我当前是否已连接经典的蓝牙设备(实际上,它将是蓝牙车载设备)。

My first step is to tell what are the current connected classic Bluetooth devices. 我的第一步是告诉当前连接的经典蓝牙设备是什么。 I cannot use CoreBluetooth because it's only for LE. 我不能使用CoreBluetooth,因为它仅用于LE。 I try to use the External Accessory framework. 我尝试使用外部附件框架。

Here is the code (a button starts the method): 这是代码(一个按钮启动方法):

- (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);
 }

I have tried when the iPhone was connected to a Bluetooth keyboard and also with a Bluetooth headset. 我已尝试将iPhone连接到蓝牙键盘和蓝牙耳机。 In both cases, the console displays that the number is 0. 在这两种情况下,控制台都会显示数字为0。

How can I display the correct number? 如何显示正确的号码?

From apple documentation: 从苹果文档:

"Applications that support external accessories must be sure to configure their Info.plist file correctly. Specifically, you must include the UISupportedExternalAccessoryProtocols key to declare the specific hardware protocols your application supports. For more information about this framework, see External Accessory Programming Topics." “支持外部附件的应用程序必须确保正确配置其Info.plist文件。特别是, 您必须包括 UISupportedExternalAccessoryProtocols键,以声明应用程序支持的特定硬件协议。有关此框架的更多信息,请参见外部附件编程主题。” here 这里

You need to add this key in your Info.plist file with the protocol of your MFi device. 您需要使用MFi设备的协议将此密钥添加到Info.plist文件中。

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

Regards 问候

You cannot. 你不能。 What you can do is to check the playback route. 您可以做的是检查播放路径。 The problem is that your cars handsfree will be a HeadsetBT. 问题在于您的汽车免提将是HeadsetBT。 This is the code I use in my app. 这是我在应用程序中使用的代码。

// 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"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM