同时将数据传输到多个外设ios BLE

时间:2022-09-06 19:02:50

Able to connect to multiple iOS devices via Bluetooth, working as 1 master and 4 slave devices.

能够通过蓝牙连接到多个iOS设备,作为1个主设备和4个从设备。

Data transfers from central to peripheral through the following code

数据通过以下代码从*传输到外围设备

[peripheral.peripheral writeValue:currentData forCharacteristic:peripheral.characteristic type:CBCharacteristicWriteWithoutResponse];

But this made hugs data loss, but was faster.

但这使得拥抱数据丢失,但速度更快。

then tried with the following code for not losing data

然后尝试使用以下代码不丢失数据

[peripheral.peripheral writeValue:currentData forCharacteristic:peripheral.characteristic type:CBCharacteristicWriteWithResponse];

Trying to transfer data to multiple peripheral at a same time (concurrently)

尝试同时将数据传输到多个外设(同时)

    for (Peripheral * peripheral in self.connectedPeripherals) {
 [peripheral.peripheral writeValue:currentData forCharacteristic:peripheral.characteristic type:CBCharacteristicWriteWithResponse];
}

Data transfers one by one it seems like a delay once 1st peripheral is received the data then 2nd peripheral gets the data and go on.

一旦第一个外设接收到数据然后第二个外设获得数据继续进行数据传输似乎是一个延迟。

Want to transfer data simultaneously and reflect at same time to all the peripherals.

想要同时传输数据并同时反映到所有外围设备。

1 个解决方案

#1


0  

When you transfer data with response, you have to wait for the acknowledgement of its receipt each time you send a packet. When you transfer data without response, the acknowledgement is not being sent back, so the throughput is higher. However, as you correctly point out, when transferring data without response there can be data loss. This data loss happens because of the overflow of internal iOS buffer that holds the data between your call to - writeValue:forCharacteristic:type: and its actual departure. If you want to prevent data loss, you can do either of the following things.

使用响应传输数据时,每次发送数据包时都必须等待确认收据。在没有响应的情况下传输数据时,不会发回确认,因此吞吐量更高。但是,正如您正确指出的那样,在没有响应的情况下传输数据时,可能会丢失数据。这种数据丢失的原因是内部iOS缓冲区溢出,它在您的调用 - writeValue:forCharacteristic:type:和它的实际离开之间保存数据。如果要防止数据丢失,可以执行以下任一操作。

  1. Don't write too much data to the buffer, because it gets silently discarded if the buffer overflows. My experiments indicate that the size of this buffer in normal conditions is around 3kb (iPhone 6, iOS9, 1 peripheral). For other devices, several connected peripherals and/or bidirectional transfer this size can be smaller. So, if you have e.g. 1 kb of data you want to send to your 4 peripherals and you do it by iteratively calling - writeValue:forCharacteristic:type:, you'll definitely face data loss.
  2. 不要向缓冲区写入太多数据,因为如果缓冲区溢出,它会被静默丢弃。我的实验表明,在正常条件下,此缓冲区的大小约为3kb(iPhone 6,iOS9,1外设)。对于其他设备,这种尺寸的多个连接外围设备和/或双向传输可以更小。所以,如果你有例如要发送到4个外设的1 kb数据,你可以通过迭代调用来实现 - writeValue:forCharacteristic:type:,你肯定会面临数据丢失。

  3. Implement a protocol to request re-sending missed packets in case of data loss on top of the characteristic used for writes without response.
  4. 实现协议以请求在没有响应的情况下在用于写入的特性之上丢失数据时重新发送丢失的分组。

  5. Write with response, but split your data into as large chunks as possible. As I said earlier, the acknowledgement is sent back after every packet of data, but these packets can be of different sizes. With iOS8/iOS9 you can expect to send up to 155 bytes of payload in a single packet. So if you need to send e.g. 300 bytes, it's better to split them into 2 150-bytes chunks than 15 20-bytes chunks. By the way, when you want to write with response and submit a value longer than 155 bytes, iOS will split it for you, but in this case you won't receive a callback `
    • peripheral:didWriteValueForCharacteristic:error:` after the data is delivered.
    • peripheral:didWriteValueForCharacteristic:error:`数据传递后。

  6. 写入响应,但将数据拆分为尽可能大的块。正如我之前所说,在每个数据包之后发送回确认,但这些数据包可以有不同的大小。使用iOS8 / iOS9,您可以在单个数据包中发送最多155个字节的有效负载。所以如果你需要发送,例如300字节,最好将它们分成2个150字节的块而不是15个20字节的块。顺便说一下,当你想用响应写入并提交一个超过155字节的值时,iOS会为你分割它,但是在这种情况下你不会收到回调`peripheral:didWriteValueForCharacteristic:error:`数据之后是`交付。

#1


0  

When you transfer data with response, you have to wait for the acknowledgement of its receipt each time you send a packet. When you transfer data without response, the acknowledgement is not being sent back, so the throughput is higher. However, as you correctly point out, when transferring data without response there can be data loss. This data loss happens because of the overflow of internal iOS buffer that holds the data between your call to - writeValue:forCharacteristic:type: and its actual departure. If you want to prevent data loss, you can do either of the following things.

使用响应传输数据时,每次发送数据包时都必须等待确认收据。在没有响应的情况下传输数据时,不会发回确认,因此吞吐量更高。但是,正如您正确指出的那样,在没有响应的情况下传输数据时,可能会丢失数据。这种数据丢失的原因是内部iOS缓冲区溢出,它在您的调用 - writeValue:forCharacteristic:type:和它的实际离开之间保存数据。如果要防止数据丢失,可以执行以下任一操作。

  1. Don't write too much data to the buffer, because it gets silently discarded if the buffer overflows. My experiments indicate that the size of this buffer in normal conditions is around 3kb (iPhone 6, iOS9, 1 peripheral). For other devices, several connected peripherals and/or bidirectional transfer this size can be smaller. So, if you have e.g. 1 kb of data you want to send to your 4 peripherals and you do it by iteratively calling - writeValue:forCharacteristic:type:, you'll definitely face data loss.
  2. 不要向缓冲区写入太多数据,因为如果缓冲区溢出,它会被静默丢弃。我的实验表明,在正常条件下,此缓冲区的大小约为3kb(iPhone 6,iOS9,1外设)。对于其他设备,这种尺寸的多个连接外围设备和/或双向传输可以更小。所以,如果你有例如要发送到4个外设的1 kb数据,你可以通过迭代调用来实现 - writeValue:forCharacteristic:type:,你肯定会面临数据丢失。

  3. Implement a protocol to request re-sending missed packets in case of data loss on top of the characteristic used for writes without response.
  4. 实现协议以请求在没有响应的情况下在用于写入的特性之上丢失数据时重新发送丢失的分组。

  5. Write with response, but split your data into as large chunks as possible. As I said earlier, the acknowledgement is sent back after every packet of data, but these packets can be of different sizes. With iOS8/iOS9 you can expect to send up to 155 bytes of payload in a single packet. So if you need to send e.g. 300 bytes, it's better to split them into 2 150-bytes chunks than 15 20-bytes chunks. By the way, when you want to write with response and submit a value longer than 155 bytes, iOS will split it for you, but in this case you won't receive a callback `
    • peripheral:didWriteValueForCharacteristic:error:` after the data is delivered.
    • peripheral:didWriteValueForCharacteristic:error:`数据传递后。

  6. 写入响应,但将数据拆分为尽可能大的块。正如我之前所说,在每个数据包之后发送回确认,但这些数据包可以有不同的大小。使用iOS8 / iOS9,您可以在单个数据包中发送最多155个字节的有效负载。所以如果你需要发送,例如300字节,最好将它们分成2个150字节的块而不是15个20字节的块。顺便说一下,当你想用响应写入并提交一个超过155字节的值时,iOS会为你分割它,但是在这种情况下你不会收到回调`peripheral:didWriteValueForCharacteristic:error:`数据之后是`交付。