我可以在扫描的同时连接多个外围设备吗?

问题描述 投票:0回答:1

我正在尝试在 iOS 上构建一个具有低功耗蓝牙的功能,该功能允许:

  1. 外设发送一些信息(最多1024字节)到中央
  2. Central接收来自外设的数据,并以列表的形式显示已连接的外设
  3. 当用户点击列表中的任何外围设备时,中央设备会发回一些信息。

我的问题是:

如果我附近有很多外围设备,我需要连接到所有这些外围设备 用于往返数据传输,但 BLE 是否允许(或推荐) 当中央仍在扫描时连接到外围设备?

我问的原因是,对于我在网上看到的大多数示例,外围设备在使用

func startAdvertising(_ advertisementData: [String : Any]?)
建立连接之前向中央广播数据;然而,这个advertisementData的MTU为31字节,这对于我的步骤1来说是不够的。

为了实现这一点,我需要确保在发送数据之前连接到发现的外设,因为当外设开始广播时,我想要通过 BLE 发送的数据大于advertisementData MTU,这意味着我无法将1024 字节数据放入

peripheralManager.startAdvertising(advertisementData)
,因为advertisementData 的大小限制为 31。

这是我目前的做法:

周边:

public func startAdvertising() {
    peripheralManager.startAdvertising(advertisementData)
    delegate?.didStartAdvertising()
}

func viewDidLoad() {
    startAdvertising()
}

public func peripheralManager(_ peripheral: CBPeripheralManager, central: CBCentral, didSubscribeTo characteristic: CBCharacteristic) {
    peripheralManager.updateValue(dataFromPeripheralThatIsLargerThan1024Bytes, for: peripheralSendingCharacteristic, onSubscribedCentrals: nil)
}

中环

func viewDidLoad() {
    centralManager.scanForPeripherals(
        withServices: [TransferService.serviceUUID],
             options: [CBCentralManagerScanOptionAllowDuplicatesKey: false]
        )
}

public func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String: Any], rssi RSSI: NSNumber) {
    centralManager.connect(peripheral, options: nil)
}

public func centralManager(_ central: CBCentralManager, didConnect peripheral: CBPeripheral) {
    peripheral.delegate = self
    peripheral.discoverServices([TransferService.serviceUUID])
    delegate?.didConnectPeripheral(central: central, peripheral: peripheral)
}

// CBPeripheralDelegate
public func peripheral(_ peripheral: CBPeripheral, didDiscoverServices error: (any Error)?) {
    guard let peripheralServices = peripheral.services else {return}
for service in peripheralServices {
peripheral.discoverCharacteristics([TransferService.peripheralSendingCharacteristicUUID, TransferService.centralSendingCharacteristicUUID], for: service)
}

public func peripheral(_ peripheral: CBPeripheral, didDiscoverCharacteristicsFor service: CBService, error: (any Error)?) {
    peripheral.setNotifyValue(true, for: characteristic)
}
public func peripheral(_ peripheral: CBPeripheral, didUpdateValueFor characteristic: CBCharacteristic, error: (any Error)?) {
    peripheral.writeValue(centralResponseDataInStep3, for: characteristic, type: .withResponse)
}

我当前的代码仅适用于 1:1 发现和数据传输。 为了使其成为 1 Central : multi Peripheral,我需要在中央构建一个表视图和一个已发现的外围设备数组。 但是,在这种情况下,我可能需要在扫描时进行连接,BLE 允许这样做吗? 如果没有,有什么建议吗? (假设 BLE 是我可以用来传输数据的唯一方法。)

谢谢!

bluetooth-lowenergy core-bluetooth
1个回答
0
投票

如果我附近有许多外设,我需要连接到所有外设以进行往返数据传输,但是 BLE 是否允许(或建议)在中央设备仍在扫描时连接到外设?

是的,没关系。请注意,这可能会导致您的扫描需要更长的时间才能发现新设备,因为它们都会争夺天线上的时间。

我问的原因是,对于我在网上看到的大多数示例,外围设备在使用 func startAdvertising(_advertisingData: [String : Any]?); 建立连接之前向中央广播数据。然而,这个advertisementData的MTU为31字节,这对于我的步骤1来说是不够的。

如果您的设备支持蓝牙 5,iOS 将使用扩展广告接受最多 124 字节的广告负载。听起来你的外围设备是其他iPhone,所以它们通常都会支持扩展广告。您可以通过使用

extendedScanAndConnect
查询 CBCentralManager 上的
supports(_ features :)
功能来进行检查。

但注意到;这仍然远低于 1KB,所以可能对你没有多大帮助。

我需要在中央构建一个表格视图和一个发现的外围阵列。然而,在这种情况下,我可能需要在扫描时进行连接,BLE 允许这样做吗?

是的。这并不罕见。

© www.soinside.com 2019 - 2024. All rights reserved.