Beware that this post is related to an SDK in maintenance mode
More Info: Consider nRF Connect SDK for new designs

BLE NUS missing BLE_NUS_EVT_RX_DATA data

Hi everone!

I am developing with SDK17.0.2 and using the nordic NUS peripheral example.

I am seeing a strange effect:

I am connecting from an android mobile and when I try to send two packets right after another the second one overwrites somehow the first data:

NRF52840 does get two  BLE_NUS_EVT_RX_DATA events but both times with the second data? The first data sent is never given to my application.

If I put in a pause between the two messages on android I get both messages right.

Maybe anyone has a clue why this is happening? I am using the nordic UARTService on android. 

My Send waits till "onCharacteristicWrite" is called before sending the second message.

Thanks,

Andreas

  • Hi Andreas

    This sounds like the data is overwritten indeed. When you try sending two (or multiple) packets after each other with no delay this can happen as the data will have to be sent before you prepare the next package. We generally recommend waiting for a TX_COMPLETE event before sending the next package to avoid the buffer overwriting the TX data before it has been sent. Please try waiting for the message altogether has been transmitted before you send the second message.

    Best regards,

    Simon

  • Hi Simon,

    thank you for your answer.

    Well TX_COMPLETE sounds like you mean on peripheral side. But I am having problems when receiving data on NRF52840 form an android mobile. When sending the data on mobile I am already waiting for completion (at least I think):

         public void onCharacteristicWrite(BluetoothGatt gatt,
                                              BluetoothGattCharacteristic characteristic,
                                              int status) {
                synchronized (this) {
                    if (status == GATT_SUCCESS) {
                        setMyWriteState(WriteDataState.Success);
                    } else setMyWriteState(WriteDataState.Failed);
                }
            }

    Sending data is done with following method:

    public synchronized boolean SendMessage(String toSend) {
            int tt = 0;
    
            while (MyWriteState == UARTService.WriteDataState.Pending) {
                tt++;
                if (tt > 500) {
                    return false;
                }
                try {
                    wait(10);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            if (!writeRXCharacteristic(toSend.getBytes())) {
                return false;
            }
            return true;
        }

      public boolean writeRXCharacteristic(byte[] value) {
            synchronized (this) {
                BluetoothGattService RxService = mBluetoothGatt.getService(RX_SERVICE_UUID);
                if (RxService == null) {
                    broadcastUpdate(DEVICE_DOES_NOT_SUPPORT_UART);
                    return false;
                }
                BluetoothGattCharacteristic RxChar = RxService.getCharacteristic(RX_CHAR_UUID);
                if (RxChar == null) {
                    broadcastUpdate(DEVICE_DOES_NOT_SUPPORT_UART);
                    return false;
                }
                RxChar.setValue(value);
                RxChar.setWriteType(BluetoothGattCharacteristic.WRITE_TYPE_DEFAULT);
                boolean status = mBluetoothGatt.writeCharacteristic(RxChar);
                if (status) {
                    setMyWriteState(WriteDataState.Pending);
                    return true;
                } else {
                    setMyWriteState(WriteDataState.Failed);
                    return false;
                }
            }
        }

    If I did nothing wrong my send wait's up to 5 seconds before sending next bytes. But onCharacteristicWrite seems to be called very quickly. 

    I know that this a questions about android but as the UartService is an project from nordic maybe you can help with that...

    Thanks, Andreas

  • Hi

    Sorry about the misunderstanding! The same still goes for Android development though. If you keep on writing in a lopp to a characteristic you might be overwriting the previous packet before the data is sent, and you will need to write, then wait for the write callback before writing the next packet.

    Best regards,

    Simon

  • Hi,

    I am already waiting on the mobile side for the "onCharacteristicWrite" callback (see above code and "SetWriteState"). So data should already be sent by mobile when sending next data is done.

    Is it possible that incomming data on NRF5 side is overwriten before event handler for receiving is called?

  • Hi

    Please check out this Android forum post regarding the Bluetooth GATT server which Nordic UART service is built on in Android. Once you set the value on the characteristic on the Android side you should be able to call the API to notify the user that the characteristic has changed.

    Best regards,

    Simon

Related