Android Question How can I turn string to byte and turn byte to string?

Alphaw

Member
Licensed User
Longtime User
Dear all, I am trying to connect android with an arduino, and I follow the step from b4r tutorial, https://www.b4x.com/android/forum/threads/hc-05-classic-bluetooth.66677/
I just use the b4a part and want to add a textbox to send the message to arduino.

However, I fail for turning the editText1.text to byte and compile the file. Why? Am I lack of any library? How can I solve it.

Besides, I also cannot show any words except integer in my app. How can I turn the byte to string?

Thank you.
 

Attachments

  • cap.PNG
    cap.PNG
    272 KB · Views: 1,234
  • bt.zip
    60.9 KB · Views: 267
  • cap2.PNG
    cap2.PNG
    27.3 KB · Views: 459

Cableguy

Expert
Licensed User
Longtime User
Try the new serializel ibrary, with it you can turn into bytes any object, send it and convert it back in the receiver
 
Upvote 0

Alphaw

Member
Licensed User
Longtime User
Oh, I see. But will I still cannot compile the file? Actually I do not know what wrong with the code.
 
Upvote 0

Cableguy

Expert
Licensed User
Longtime User
Have made the appropriate changes in order to use serialisation?
What error do you get?
Can you post your code?
 
Upvote 0

Alphaw

Member
Licensed User
Longtime User
My code is just attached in first post and the photo show that I cannot compile to an app as the problem with byte.
 
Upvote 0

Cableguy

Expert
Licensed User
Longtime User
So you haven't change your original code?
USE serialisation in your code, and if that doesn't work THEN post any relevant code.
Your current code error tells you that it cannot parse a byte into a bytes array...
Dealing with strings in arduino is somewhat complex, so Erel created an "universal" serialisation lib to be used in cases like this one
 
Upvote 0

Alphaw

Member
Licensed User
Longtime User
Oh, I see. Let me try first. Thank you.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
You should post the relevant code and error message as text instead of screenshots.

String.GetBytes returns an array of bytes.
B4X:
Dim b() As Byte = EditText1.Text.GetBytes("UTF8") 'convert to bytes
Log(BytesToString(b, 0, b.Length, "UTF8")) 'convert to string

I recommend using B4RSerializator.
 
Upvote 0