简体   繁体   中英

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.

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. 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). 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.
  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. 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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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