Android Question write characteristics into ESP32 chip

Marco Maria Vilucchi

Active Member
Licensed User
Longtime User
hi all,
I don't know how to explain this problem.
I need to write a characteristic of ESP32 chip via BLE.
I have two cases:
1) I need to write a letter, in particular "à"
2) I need to write hex FF
I use this code:

B4X:
'    CHIAMATA BLUETOOTH C314_trasponder_index_06
    Main.chichiededellapedana="CicloCard"
    Log("ciclocard C314")
    Dim s As String ="à"
    CallSub2(Starter, "SendC314", s)

    Sleep(1000)

'    CHIAMATA BLUETOOTH C315_trasponder_index_07 id cliente
    Main.chichiededellapedana="CicloCard"
    Log("ciclocard C315")
    Dim s As String
    Dim bt1(4) As Byte
    bt1(0)=17
    bt1(1)=255
    bt1(2)=32
    bt1(3)=32
    s = BytesToString(bt1, 0, bt1.Length, "UTF-8")
    CallSub2(Starter, "SendC315", s)
on starter module code is:
B4X:
Public Sub SendC314(cmsg As String)
    LogColor("Dentro SendC314"&" Messaggio "&cmsg,Colors.red)
    qualecaratteristica = C314_trasponder_index_06
    SendTrasponder(cmsg.GetBytes("utf8"))
End Sub

Public Sub SendC315(cmsg As String)
    LogColor("Dentro SendC315"&" Messaggio "&cmsg,Colors.red)
    qualecaratteristica = C315_trasponder_index_07
    SendTrasponder(cmsg.GetBytes("utf8"))
End Sub

Public Sub SendTrasponder(msg() As Byte)
    Dim strmsg As String = BytesToString(msg, 0, msg.Length, "utf8")
    LogColor("Dentro SendTrasponder Blestate:"&BLEState&" Messaggio "&strmsg,Colors.red)
    messagesToSend.Add(TrimMessage(msg))
'    qualecaratteristica = C329_trasponder_index_31

    If messagesToSend.Size = 1 Then
        Do While messagesToSend.Size > 0
            Try
                manager.WriteData(ServiceId, qualecaratteristica, messagesToSend.Get(0))
            Catch
                FailedToSend
                Return
            End Try
            Wait For Manager_WriteComplete (Characteristic As String, Status As Int)
            If Status <> 0 Then
                FailedToSend
            End If
            If connected = False Or messagesToSend.Size = 0 Then Return
            messagesToSend.RemoveAt(0)
        Loop
    End If
End Sub

Before start App I emptied characteristics (using nRF Connect app)

I have this log:
B4X:
ciclocard C314
Dentro SendC314 Messaggio à
Dentro SendTrasponder Blestate:1 Messaggio à
ciclocard C315
Dentro SendC315 Messaggio �
Dentro SendTrasponder Blestate:1 Messaggio �
Failed to send message. Disconnecting.

Then I read characteristics (via nRF Connect) and I found:
on C314: C3 A0 (two characters instead of 1 and à is 133 (hex 85))
on C315: empty

Where am I doing wrong?
 
Last edited:

Marco Maria Vilucchi

Active Member
Licensed User
Longtime User
I missed a sub

Sub ByteArrayToString(B() As Byte)

Dim S As String

For I = 0 To B.Length - 1
S = S & " " & Bit.And(B(I), 0xFF)
Next

Return S.Trim

End Sub
 
Upvote 0

Marco Maria Vilucchi

Active Member
Licensed User
Longtime User
yes.
69 61 E0 E0
this is correct: E0 is 224, but why 224 and not 133? The problem now is in the code, not in the chip or in BLE...
I asc ascii code of à, that is 133 and obtain 224....
 
Upvote 0

emexes

Expert
Licensed User
What does nrf connect now show for that characteristic?
Does it show the four actual byte values?
yes.
69 61 E0 E0
That looks correct to me. They are the same four bytes that were put in to the characteristic, according to:
Dentro SendC314 Messaggio 105 97 224 224
this is correct:
Great minds think alike!
E0 is 224, but why 224 and not 133?
E0 hex = 11100000 binary = 128 + 64 + 32 decimal = 224 decimal
That is why E0 is 224.
Why would E0 be 133? 133 decimal = 128 + 5 decimal = 80 + 5 hex = 85 hex.
I think I am not understanding your question here, or perhaps it is losing something in translation (but no worries - your English is heaps better than my Italian ;-)
I asc ascii code of à, that is 133 and obtain 224....
This might be the issue. In B4A, which is based on Java and thus also Unicode, Asc("à") returns the Unicode character number 224.

But if you were using a PC environment or BASIC dialect that does not support Unicode, then Asc("à") will probably instead return the IBM PC Extended ASCII character number 133. But we are in B4A, so we are talking Unicode, so we get 224.
 
Upvote 0

emexes

Expert
Licensed User
tomorrow I try to read from card E0 and display à
That should work ok.

Where you might run into trouble is with Unicode characters outside of 0..255 eg Cyrillic characters are at:

https://en.wikipedia.org/wiki/Cyrillic_(Unicode_block)

and so you might need to redefine this four-byte characteristic to be 8 bytes of UTF-8 so that it can include characters > 255 (by using 2 bytes per character, to repesent characters up to U+07FF cf Cyrillic U+0400..04FF).
 
Upvote 0

emexes

Expert
Licensed User
redefine this four-byte characteristic to be 8 bytes of UTF-8
This page here lists the data types that should be recognizable by generic BLE readers eg NRF Connect:

https://web.archive.org/web/2017010...r.gatt.characteristic_presentation_format.xml

Type 25 is UTF-8 and type 26 is UTF-16.

UTF-8 is more common*, but two bytes of UTF-16 can represent a larger range of characters that two bytes of UTF-8, and that might make it a better choice for a fixed-length field if you are expecting to use Unicode characters beyond U+07FF.


* mostly due to the nice feature that ASCII maps 1:1 to Unicode, ie no conversion required: an ASCII string is also already a UTF-8 string
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…