简体   繁体   English

同时将数据传输到多个外围设备

[英]Transfer Data to multiple peripheral simultaneously ios BLE

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. 要同时传输数据并同时反映到所有外围设备。

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. 发生此数据丢失的原因是内部iOS缓冲区的溢出,该缓冲区保存在您对- writeValue:forCharacteristic:type:调用与实际调用之间的数据。 If you want to prevent data loss, you can do either of the following things. 如果要防止数据丢失,可以执行以下任一操作。

  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). 我的实验表明,正常情况下此缓冲区的大小约为3kb(iPhone 6,iOS9、1个外围设备)。 For other devices, several connected peripherals and/or bidirectional transfer this size can be smaller. 对于其他设备,几个连接的外围设备和/或双向传输的大小可能会更小。 So, if you have eg 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. 因此,如果您有例如1 kb的数据要发送到4个外围设备,并且通过迭代调用- writeValue:forCharacteristic:type: ,则肯定会面临数据丢失的情况。
  2. Implement a protocol to request re-sending missed packets in case of data loss on top of the characteristic used for writes without response. 在无响应的写入特性的基础上,实现一个协议以请求重新发送丢失的数据包,以防止数据丢失。
  3. 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. 使用iOS8 / iOS9,您可以期望在单个数据包中发送多达155个字节的有效负载。 So if you need to send eg 300 bytes, it's better to split them into 2 150-bytes chunks than 15 20-bytes chunks. 因此,如果您需要发送例如300个字节,最好将它们分成2个150字节的块,而不是15个20字节的块。 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 ` 顺便说一句,当您要编写带有响应并提交超过155个字节的值时,iOS会为您拆分它,但是在这种情况下,您将不会收到回调`
    • peripheral:didWriteValueForCharacteristic:error:` after the data is delivered. 数据交付后,外围设备:didWriteValueForCharacteristic:错误:`。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM