【问题标题】:Get the list of classic bluetooth connected devices (no BLE) [EAAccessoryManager]获取经典蓝牙连接设备列表(无BLE)[EAAccessoryManager]
【发布时间】:2016-05-31 11:04:32
【问题描述】:

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

我的第一步是告诉当前连接的经典蓝牙设备是什么。我不能使用 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。

我怎样才能显示正确的数字?

【问题讨论】:

  • EAAcessoryManager 中似乎没有列出“普通”键盘和“普通”耳机等蓝牙设备。
  • 是的,我认为这是因为设备必须是 MFI,而“普通”蓝牙设备可能并非如此。我找到的解决方案是使用 [[AVAudioSession sharedInstance] availableInputs] 来获取蓝牙输入,但是,使用此解决方案,我无法在后台接收通知(我必须播放音乐才能被允许接收通知)。

标签: ios external-accessory ios-bluetooth


【解决方案1】:

来自苹果文档:

“支持外接附件的应用程序必须确保 正确配置他们的 Info.plist 文件。具体来说,您必须 包括 UISupportedExternalAccessoryProtocols 键来声明 您的应用程序支持的特定硬件协议。更多 有关此框架的信息,请参阅外部附件编程 话题。”here

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

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

问候

【讨论】:

    【解决方案2】:

    你不能。您可以做的是检查播放路线。问题是您的汽车免提将成为 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"
    

    【讨论】:

      猜你喜欢
      • 2021-10-09
      • 2014-12-17
      • 1970-01-01
      • 2015-12-18
      • 1970-01-01
      • 2021-07-10
      • 1970-01-01
      • 2018-05-26
      • 2021-07-19
      相关资源
      最近更新 更多