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:

emexes

Expert
Licensed User
sorry suggest to me an instruction...
If you are sending bytes, better to use byte arrays. It is made a bit tedious by Bytes being signed rather than unsigned, but that's just the way it is. Soon you'll be using Bit.And(bytevalue, 0xFF) like you were born talking bits.

Change your intermediate routines from String to Byte() eg:
B4X:
Public Sub SendC314(cmsg() As Byte)                             'change this line
    LogColor("Dentro SendC314"&" Messaggio "&cmsg,Colors.red)
    qualecaratteristica = C314_trasponder_index_06
    SendTrasponder(cmsg)                                        'change this line
End Sub

Public Sub SendC315(cmsg() As Byte)                             'change this line
    LogColor("Dentro SendC315"&" Messaggio "&cmsg,Colors.red)
    qualecaratteristica = C315_trasponder_index_07
    SendTrasponder(cmsg.GetBytes)                               'change this line
End Sub
and then you would change this:
B4X:
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)
to:
B4X:
Dim bt1() As Byte = Array As Byte(17, 255, 32, 32)
CallSub2(Starter, "SendC315", bt1)
You'll need to modify your Logs of Strings to use this function to Log Byte Arrays instead:
B4X:
Sub ByteArrayToString(B() As Byte)

    Dim S As String

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

    Return S.Trim

End Sub

'LogColor("Dentro SendC315"&" Messaggio "&cmsg,Colors.red)
LogColor("Dentro SendC315" & Messaggio " & ByteArrayToString(cmsg), Colors.Red)
 
Upvote 0

Marco Maria Vilucchi

Active Member
Licensed User
Longtime User
ok thanks emexes it works, but if I need to put a string with an accented letter like "abcà" ?
i'm trying with:
Dim a1 As Byte = Asc("a")
Dim a2 As Byte = Asc("b")
Dim a3 As Byte = Asc("c")
Dim a4 As Byte = Asc("à")
First three letters are ok, ma "à" become another character (224 of ascii table)
 
Upvote 0

emexes

Expert
Licensed User
Dim a1 As Byte = Asc("a")
Dim a2 As Byte = Asc("b")
Dim a3 As Byte = Asc("c")
Dim a4 As Byte = Asc("à")
First three letters are ok, ma "à" become another character (224 of ascii table)
That should work. The problem might be that the destination device is using IBM PC characters, where à is character 133 (cf in Unicode it is character 224).

What is the destination device (manufacturer, model, name) that you are sending these four bytes to? And what is the meaning of the four bytes? Is there a data sheet or technical manual that describes the expected packet layout? Like... are they LED intensities? Or bit patterns for turning 32 outputs on/off? Or four characters displayed on a screen?

That we are dealing with just four bytes, makes me think that it is fixed-format numeric data, rather than text. If this is the case, like if they are say four 8-bit numbers that you are sending, then LET GO OF THINKING ABOUT THEM AS A STRING ! ! ! Your code would be better like this eg:
B4X:
Sub ConstructPacketType314(RedIntensity As Float, GreenIntensity As Float, BlueIntensity As Float) As Byte()

    Dim Packet(4) As Byte

    Packet(0) = Max(0, Min(255, RedIntensity * 256))
    Packet(1) = Max(0, Min(255, GreenIntensity * 256))
    Packet(2) = Max(0, Min(255, BlueIntensity * 256))
    Packet(3) = 0xE0    'binary 1110 0000 = enable channels 5, 6, 7 ???

    Return Packet

End Sub
 
Upvote 0

emexes

Expert
Licensed User
this four characters are only a name, but there may be an accented letter
Like a sharemarket stock code, or a postcode, or a province abbreviation? Is it always four characters exactly, or might it grow or shrink?
First three letters are ok, ma "à" become another character (224 of ascii table)
What is the other character that it changes to? Is it the greek letter alpha "α" by any chance?
 
Upvote 0

Marco Maria Vilucchi

Active Member
Licensed User
Longtime User
they are 4 characters eventually filled with spaces
these characters are stored into a Client card and the client name can contain any character (such as Swedish or Romanian names). In the future they will also be Russian names in Cyrillic
 
Upvote 0

emexes

Expert
Licensed User
First three letters are ok, ma "à" become another character (224 of ascii table)
This is a super-minor point, but... ASCII is only characters 0..127. Anything above that is a bit of a gamble, although Unicode has helped (sort of).

The original common 8-bit character set is that used by the IBM PC, which contains common (but not all) accented characters and letters of other alphabets, plus a lot of line-drawing characters. Its official designation is now Code Page 437, but I often see it referred to as Extended ASCII.

A later common 8-bit character set is that used by Windows, which broadly follows the IBM PC set, but dumped the line drawing characters (since they're not needed for a GUI environment) and replaced them with more accented characters etc from other alphabets. This is the Windows 1250 character encoding. I think it is almost the same as Unicode characters 0..255, but not quite.
 
Upvote 0

emexes

Expert
Licensed User
they are 4 characters eventually filled with spaces
these characters are stored into a Client card and the client name can contain any character (such as Swedish or Romanian names). In the future they will also be Russian names in Cyrillic
Is the BLE device a card?

How did you know that the character 224 became another character? What character did it become? What character were you expecting?

What I'm trying to deduce here is: what character set is the BLE device using? And if it is displaying from a set of 256 characters (ie "code page") then how do/can we tell it to use Code Page 855 that includes Cyrillic characters?
 
Upvote 0

Marco Maria Vilucchi

Active Member
Licensed User
Longtime User
How did you know that the character 224 became another character? What character did it become? What character were you expecting?
I see chip via nrf connect and I can see what contain my characteristic. I expecting 85 ( that is 133)

I sent "iaàà"
this is the log of ByteArrayToString(cmsg)
105 97 224 224

105 is i
97 is a
but 224?
 
Upvote 0

emexes

Expert
Licensed User
but if I have 133 and write 133, why is stored 224?
Show me the code

But if you came up with those numbers independently, and not from my post above, then the answer is that IBM PC character 133 is the same character as Unicode character 224.

How did you know that the character 224 became another character? What character did it become? What character were you expecting?
 
Upvote 0

emexes

Expert
Licensed User
I see chip via nrf connect and I can see what contain my characteristic. I expecting 85 ( that is 133)

I sent "iaàà"
this is the log of ByteArrayToString(cmsg)
105 97 224 224
So you are setting the characteristic value to the four bytes (numbers)

105 97 224 224 (decimal, or 69 61 E0 E0 hex)

but you are expecting to see

105 97 133 133 (decimal, or 69 61 85 85 hex)

???

Show me the code that assembles the four bytes into the characteristic value, and tell me what values you then see in nrf connect (hex or decimal, I don't mind).
 
Upvote 0

Marco Maria Vilucchi

Active Member
Licensed User
Longtime User
here it is:

Dim a1 As Byte = Asc(wnominativo.SubString2(12,13))
Dim a2 As Byte = Asc(wnominativo.SubString2(13,14))
Dim a3 As Byte = Asc(wnominativo.SubString2(14,15))
Dim a4 As Byte = Asc(wnominativo.SubString2(15,16))

CallSub2(Starter, "SendC314", Array As Byte(a1, a2, a3, a4))

then the sub:

Public Sub SendC314(cmsg() As Byte)
LogColor("Dentro SendC314"&" Messaggio "& ByteArrayToString(cmsg),Colors.red)
qualecaratteristica = C314_trasponder_index_06
SendTrasponder(cmsg)
End Sub

and now the log:

Dentro SendC314 Messaggio 105 97 224 224
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…