【问题标题】:How get the list of paired bluetooth devices in swift?如何快速获取配对的蓝牙设备列表?
【发布时间】:2016-04-08 03:02:01
【问题描述】:

我需要获取与iOS设置中“蓝牙”部分中的列表相同的配对蓝牙设备(iOS设备)列表,如下图所示。



有可能吗?
您是否见过任何具有此类功能的应用程序?

我尝试了以下方法: link1link2link3link4link5link6

但没有什么能帮助我清楚地获得确切的清单。我希望应该有一种方法来实现这一点。请通过分享您的经验来帮助我。 谢谢。

【问题讨论】:

  • 您可以检索已配对/连接的 BLE 设备的详细信息,但不能检索耳机/免提等旧设备的详细信息
  • 好的,谢谢。您能否向我提供有关工作解决方案的指南,以制作配对/连接的 BLE 设备列表?
  • @Paulw11 我也会感兴趣,因为我不知道列出断开连接设备的 API。对于连接的设备,有- retrieveConnectedPeripheralsWithServices: API。
  • @allprog。关于配对(但当前未连接)设备的任何想法?
  • @Paulw11。我在stackoverflow.com/a/28342831/1996294 的答案中看到“但是,没有列出配对设备的 API”。你确定吗?

标签: ios bluetooth-lowenergy core-bluetooth external-accessory ios-bluetooth


【解决方案1】:

无法从 iOS 检索配对外围设备列表。也无法检查特定外围设备是否已配对。

检索配对的外围设备

有两种情况需要考虑:

  1. 外围设备可能已连接到系统中(iOS 会自动连接某些外围设备,例如显示电池电量)。在这种情况下,外围设备将不会进行广播,并且使用 scanForPeripherals 进行检测将不起作用。

  2. 外设已配对,但已断开连接。在这种情况下,retrieveConnectedPeripherals(withServices:) 将不起作用。

因此,要检索您的外围设备,您需要将两者结合起来。首先,您需要检查它是否在从retrieveConnectedPeripherals(withServices:) 返回的外围设备中。如果没有,你应该scanForPeripherals

如果您想检索超出范围的外围设备,您可以尝试使用retrievePeripherals(withIdentifiers:),但它也可能返回未配对的设备,并且它依赖于您必须在配对后保存的外围设备的UUID

检测外设是否配对

有一种方法可以检测特定外围设备是否已配对。您需要尝试从受保护的特征(需要加密 - 绑定)中读取。如果您收到预期的数据,则表示用户接受了配对请求。否则,您将收到空响应或没有响应。

参考文献

您可以在我的文章中阅读有关蓝牙的更多信息:

【讨论】:

    【解决方案2】:

    如果您将 Classic Bluetooh 用于 MFi 设备,则可以使用 ShowBluetoothAccessoryPicker

    https://developer.apple.com/documentation/externalaccessory/eaaccessorymanager/1613913-showbluetoothaccessorypicker

    下面的代码是一个 C#,但你可以很容易地将它转换成 IOS(对象 c 你的 swift)

    EAAccessoryManager.SharedAccessoryManager.ShowBluetoothAccessoryPicker(null, completion: ((Foundation.NSError error) => {
                    Console.WriteLine("My callback");
                    if (error != null && (uint)error.Code != (uint)EABluetoothAccessoryPickerError.AlreadyConnected)
                    {
                        Console.WriteLine(String.Format("Error code: {0} Desc: {1}", error.Code, error.DebugDescription));
                        Console.WriteLine("Failed? " + EABluetoothAccessoryPickerError.Failed.ToString());
                        Console.WriteLine("Failed? " + Convert.ToInt64(EABluetoothAccessoryPickerError.Failed));
                    }
                }));
    

    【讨论】:

      【解决方案3】:

      您需要找到您感兴趣的服务 UUID,在我的情况下它可以完美运行,

      NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:YES], CBCentralManagerScanOptionAllowDuplicatesKey, nil];
      [self.centralManager scanForPeripheralsWithServices:@[[CBUUID UUIDWithString:SERVICE_UUID]]
                                                  options:options];
      

      当它找到任何宣传相同服务 UUID 的设备时,它将出现在您上面指出的屏幕中。

      以这种方式处理 didDiscoverperipherel:

      -(void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI{
      
          _discoveredPeripheral = peripheral;
      
          if(![self.mRemoteDevices containsObject:_discoveredPeripheral])
          {
               NSArray *peripherels = [self.centralManager retrievePeripheralsWithIdentifiers:@[_discoveredPeripheral.identifier]];
              [self.mRemoteDevices addObject:[peripherels objectAtIndex:0]];
          }
      }
      

      【讨论】:

      • 我只需要配对列表,我只需要名字就足够了。我需要为此使用哪个 UUID。
      • 请注意,仅凭 UUID 无法区分设备。
      • 你不能简单地获取设备,你需要有你感兴趣的服务UUID。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-06
      • 1970-01-01
      • 2014-07-14
      • 1970-01-01
      • 2012-04-28
      • 2013-06-05
      相关资源
      最近更新 更多