Android Question BLE2 Using WriteData to send a string Write Without Response

Steve-h

Member
How can I send a 5 character string to the remote device. The best I can come up with is below, which is painfully slow and clearly reveals my lack of ability. The logs show retries and the third character is often sent twice. All characters are eventually received but very slowly.

The remote device is set as Write Without Response so I am wondering if the BLEmanager is expecting a response before proceeding. Checking for A WriteComplete event shows nothing although I am not certain if that is expecting a handshake from the remote end or simply reporting the data has been sent, whichever it is no event seems to get raised.

So the problem is two things, how can I send a string when WriteData requires single byte and is it so painfully slow because it is waiting for a response but the remote end is not able to send one.

B4X:
Private Sub ButSend_Click
    Dim serveuuid As String = "49535343-fe7d-4ae5-8fa9-9fafd205e455"     'UUID for RN4870 transparent service
    Dim rxuuid As String = "49535343-8841-43f4-a8d4-ecbe34729bb3"        'UUID for RN4870 transparent rx
    Dim n As Int=0
    Dim dat As Byte

    Do While n <6
        If n=0 Or n=3 Then dat=84: If n=1 Then dat=69: If n=2 Then dat=83 : If n=4 Then dat=32
        manager.WriteData(serveuuid,rxuuid,Array As Byte (dat))
        Log(n)
    n = n+1
    Loop
    
    End Sub

Thank you
 

Steve-h

Member
Ah, yes I can see that now. Thank you.
I do apologise for my difficulty in understanding. B4x is still unfamiliar territory and to me almost everything is confusing.
 
Upvote 0

Angel Maza

Member
Licensed User
Longtime User
I've the same need. I'm doing NRF DFU and having a lot of problems.

Seeing the DFU example in Nordic (https://github.com/NordicSemiconductor/Android-DFU-Library/ ), I see that every time a write packet is sent, there is a call to:
BaseCustomDfuImpl.java @ line 422:
characteristic.setWriteType(BluetoothGattCharacteristic.WRITE_TYPE_NO_RESPONSE);
        characteristic.setValue(locBuffer);
        gatt.writeCharacteristic(characteristic);

I'm not sure if this is the problem, but seems to be (sometimes sends some packets ok, but fails a lot of them making unusable for DFU)

thanks.
 
Upvote 0

Steve-h

Member
I have no idea what NRF or DFU are, which sort of relates to my post elsewhere on the forum about putting notation in code examples. You know what the acronyms mean but your reader may not.

However my problem was more a case of not understanding Bluetooth rather than not understanding the BLE2. In my application I Write Without Response and the reply is sent as a Notification. Write Without Response and Notification potentially loose packets specifically because there is “no response”. To protect against this I always send the same length string and always end with the same character. I then can ask for it again if it is the wrong length.

The main challenge for me was not understanding what BLE2 is doing when you call one of its functions. To help with this I used one of the many Bluetooth sniffer applications to examine the data which made things a bit clearer.
 
Upvote 0

Angel Maza

Member
Licensed User
Longtime User
Sorry for the NRF & DFU acronyms. NRF is Nordic related BLE chipset (nrf51, nrf52), and DFU is the Device Firmware Update that Nordic provides.

Attached a capture of nrfconnect to the device, as you can see a char has Write without response attribute and the other only Write (with response).

Screenshot_2022-01-23-19-50-24-954_no.nordicsemi.android.mcp.jpg


In the next link there is an explanation of the difference, with response there is a dumb 0x13 response (something like ack/received)

BLE with or without response

BLE2.WriteData doesn't handle with and without response (maybe does automatically).

Can you recommend a BLE sniffer?

Thanks.
 
Upvote 0

Similar Threads

Top