Android Question Learning & Building App Based Around BT Chat App

coolice

New Member
Licensed User
Longtime User
Hello All.

I'm new to the forum and new to trying to get my head around Android code.
I stumbled across basic4android by chance and after a short while took the plunge and brought it. I must say I am impressed and am starting to learn my way around little by little around work.

Basically I am looking to build and app that interrogates a Bluetooth module (HC-05) and sends it a hex command to generate a response, then displays the returned hex code interpreted as ASCII in various text boxes.
Using the Bluetooth chat application as a starting point and with the tutorials here, I have managed to get the chat app to send out the hex code and receive the response, which when translated is giving me the information I'm requesting. However I am struggling to now find the lines of code I'd need to change this ASCII hex code to readable text.
For example if I send 1C0A I get sent back 33 on one line and 322E34B043 on the 2nd, which is telling me the temperature reading.

The last sub of the chat app;

"Sub LogMessage(From AsString, Msg AsString)

txtLog.Text = txtLog.Text & From & ": " & Msg & CRLF

txtLog.SelectionStart = txtLog.Text.Length

End Sub"

So I'm guessing this is were I need to put another line of code or two, to take the received information and convert to display correctly. Various searches have not shown me anything obvious to try, other than what I already have and due to my newbie nature to Android coding it's taking me longer to work things out. I'm hoping that by working out what I need to do to the chat app, I can apply that knowledge to build the app I am looking for.

I came across this topic;

http://www.b4x.com/android/forum/threads/convert-string-to-ascii.32201/

But have yet to figure out where to apply the changes to make it work for me.

Any help would be greatly appreciated on my exciting adventure of trying to learn the basics of what I'm trying to achieve.

Ian
 

udg

Expert
Licensed User
Longtime User
Hi Ian,

use Agraham's ByteConverter library to convert from hex codes to string of ASCII chars.
I didn't read the Chat example so I don't know why you receive your temperature reading on two lines (possibly is it due to async char response?).

Umberto
 
Upvote 0

coolice

New Member
Licensed User
Longtime User
Hi Ian,

use Agraham's ByteConverter library to convert from hex codes to string of ASCII chars.
I didn't read the Chat example so I don't know why you receive your temperature reading on two lines (possibly is it due to async char response?).

Umberto

Evening Umberto.

Thank you for the information, I'll have to look at using it to convert back. I am already using it to convert the output from hex to bytes, have to work out how to make it go the other way.

Again thanks for your input, small steps and I will start to understand it better.

Ian
 
Upvote 0

udg

Expert
Licensed User
Longtime User
Hi Jan,

having a look at the library documentation, it seems it will suffice to concatenate the following two funcions:

HexToBytes (hex AsString) AsByte()

StringFromBytes
(bytes() AsByte, encoding AsString) AsString

On the assumption that the Hex string is complete; otherwise you could convert chunks of response from the device and show result when you receive a stop char (e.g. the grades symbol for temperature).

Umberto
 
Upvote 0
Top