【问题标题】:Bluetooth classic connection awareness蓝牙经典连接感知
【发布时间】:2015-04-26 02:37:39
【问题描述】:

我有一个蓝牙应用程序,可以通过低功耗与外围设备进行通信。

此外围设备还具有与 iOS 设备的经典(HFP 和/或 A2DP)连接。经典连接有时会中断。

我需要的是能够在应用程序中通知用户经典连接已丢失。

如何让我的应用程序知道经典连接?

您希望以哪种方式执行此操作?

【问题讨论】:

  • 经典连接是指您使用的是External Accessory 框架吗?如果是这样,您可以注册连接和断开连接的通知。
  • 不,我没有使用外部附件框架。我只使用 CoreBluetooth,我的应用程序通过 BLE 与设备通信。但同一设备也通过经典连接与 iOS 设备连接,因此它可以从 iTunes 流式传输音频。我能否让我的应用程序知道该(经典)连接何时中断,以及如何?
  • “经典连接”蓝牙 2.0 是什么意思?
  • 如果信息不足,请见谅。我认为通过“流式传输”可以理解 - HFP 和/或 A2DP。
  • 我会尝试外部附件框架。我将在下面的答案中发布代码。

标签: ios objective-c bluetooth bluetooth-lowenergy core-bluetooth


【解决方案1】:

虽然 CoreBluetooth 用于访问蓝牙 LE 或 4.0 设备,但您可以使用ExternalAccessory 框架与其他蓝牙设备进行通信。

像这样:

- (void)registerForNotifications
{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(accessoryDidConnect:) name:EAAccessoryDidConnectNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(accessoryDidDisconnect:) name:EAAccessoryDidDisconnectNotification object:nil];
    [[EAAccessoryManager sharedAccessoryManager] registerForLocalNotifications];
} 

- (void)accessoryDidConnect:(NSNotification *)notification
{
    // Weird thing with ExternalAccessory where this notification is called more than once per accessory...
    if ([[(EAAccessory *)[[(EAAccessoryManager *)notification.object connectedAccessories] lastObject] protocolStrings] count]) {
        // Valid call
        if ([[(EAAccessory *)[notification.userInfo valueForKey:EAAccessoryKey] protocolStrings] containsObject:/*Protocol string for the accessory*/]) {

        }
    }
}

- (void)accessoryDidDisconnect:(NSNotification *)notification
{
    if ([[(EAAccessory *)[notification.userInfo valueForKey:EAAccessoryKey] protocolStrings] containsObject:/*Protocol string for the accessory*/]) {
        // Disconnected
    }
}

为此,您必须将“支持的外部附件协议”键添加到应用的 info.plist 中,并在该键下的数组中列出蓝牙设备的协议。

另请注意,要在 App Store 上分发蓝牙设备,必须在 Apple MFi 计划下注册,并且您必须是(设备制造商)认可的开发人员。

【讨论】:

  • 设备来自其他供应商。我可以假设这不是我关心的问题吗?
  • 如果您尝试发布使用非蓝牙 LE 的蓝牙连接的应用程序,除非该设备已在 MFi 程序中注册并且您是公认的开发人员,否则 Apple 将失败。我遇到了这个问题,现在正在切换到蓝牙 LE 设备来省去麻烦。
【解决方案2】:

对于非 MFI 设备,我发现获取有关连接的蓝牙经典设备的任何信息的唯一方法是(间接)通过 AVAudioSession 单例。 AVAudioSession Class Reference

首先查看 currentRoute 属性。

【讨论】:

    猜你喜欢
    • 2013-10-25
    • 2021-10-09
    • 2021-07-19
    • 1970-01-01
    • 2021-03-11
    • 1970-01-01
    • 2016-05-31
    • 1970-01-01
    • 2021-07-10
    相关资源
    最近更新 更多