获取经典蓝牙连接设备列表(无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).
我的第一步是告诉当前连接的经典蓝牙设备是什么。我不能使用CoreBluetooth,因为它只适用于LE。我尝试使用外部附件框架。
My first step is to tell what are the current connected classic Bluetooth devices. I cannot use CoreBluetooth because it's only for 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);
}
我已经尝试将iPhone连接到蓝牙键盘并同时使用蓝牙耳机。在这两种情况下,控制台都会显示该数字为0.
I have tried when the iPhone was connected to a Bluetooth keyboard and also with a Bluetooth headset. In both cases, the console displays that the number is 0.
如何显示正确的数字?
推荐答案
你做不到。你可以做的是检查播放路线。问题是你的汽车免提将成为HeadsetBT。这是我在我的应用程序中使用的代码。
You cannot. What you can do is to check the playback route. The problem is that your cars handsfree will be a 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"
这篇关于获取经典蓝牙连接设备列表(无BLE)[EAAccessoryManager]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!