简体   繁体   中英

Android BLE connection breaks when sending many packets with WRITE_TYPE_NO_RESPONSE

I am getting crazy with a project where I need to send firmware files from an Android device to a STM32F4 chip using Bluetooth LE.

I have already implemented BLE on both ends successfully and I am working with it with several characteristics for a long period without any problem.

Now a file transfer ought to be implemented that shall be able to send files in size of about 250K. My implementation seem to work but only in one of 10 cases. It does start sending packets in chunks of 20 bytes but then it stops communication in 90% of the test cases on an undetermined point. I need to disconnect/reset and restart to get things up again.

Characteristic for file transger on the STM32F4 are defined as:

 ret = aci_gatt_add_char(fileServiceHandle,
                            UUID_TYPE_128,                      // File xfer  UUID 
                            uuid,                               // Char UUID
                            FILEIO_RECORD_LEN,                  // Maximum length of the characteristic value (20)
                            CHAR_PROP_WRITE|CHAR_PROP_WRITE_WITHOUT_RESP|CHAR_PROP_NOTIFY,   // WRITE NOTIFY me
                            ATTR_PERMISSION_NONE,               // Nothing special
                            GATT_NOTIFY_ATTRIBUTE_WRITE,        // The application will be notified when a client writes to this attribute.
                                                                // An @ref EVT_BLUE_GATT_ATTRIBUTE_MODIFIED will be issued.
                            16,                                 // Encryption key size
                            0,                                  // is fixed length (1== variable size)
                            &fileRequestHandle);                // ReturnValue als handle

In Andoid I am setting the WRITE_TYPE_NO_RESPONSE flag in the service characteristic to

public void onServicesDiscovered(BluetoothGatt gatt, int status) {
   ... aServiceCharacteristic.setWriteType(BluetoothGattCharacteristic.WRITE_TYPE_NO_RESPONSE);

Writing the packets is done in the onCharacteristicWrite call back function for a FIFO of maximum 8 packets.

  • Build up to 8 fragments of file data and queue it to a fifo

  • wrtCharacteristic.setValue(firstQueueItem);

  • in onCharacteristicWrite call back: if queue not empy { wrtCharacteristic.setValue(nextQueueItem); }

  • If the last packet is received in the STM32F4, all packets in that group are verified and an acknowledge is send back causing an event in the APP. The event then triggers sending the next 8 packets.

This looks pretty straight forward to me and seem to work sometimes. It works always though if I am setting the number of consecutive blocks to 1. All other sizes do not complete sending the in files in almost all cases.

There is no evidence of when the transfer is broken, sometimes immediately, sometimes after sending more than 80% of the data.

I have also tried to skip writing the received data on the STM32F4 to the flash storage to avoid SPI interferences without any changes in behavior.

Is there anything that I am missing here? Where could I check for errors. Any help wouldbe very much appreciated.

For unknown reasons, this problem does occur any longer. My implementation has not changed compared to what I stated on top. I tried to request a BLE response for the last packet of each group but that does not seem to make any difference.

I thank everybody who read and commented on this entry.

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