how to send hex bytes using AsyncStreams?

techknight

Well-Known Member
Licensed User
Longtime User
I might have missed it somewhere, but how do you send and receive hex data over the async prefix mode?
 

raphael75

Active Member
Licensed User
Longtime User
To send hex data you can use this (modified from Bluetooth Chat example):

B4X:
Sub Globals
  [...]
  Dim BConv As ByteConverter  ' Needs ByteConverter library
End Sub

Sub btnSend_Click
  AStream.Write(BConv.HexToBytes(txtInput.Text.Replace(" ", "")))
  [...]
End Sub

I use the code Text.Replace(" ", "") to remove spaces between hex bytes.

To receive hex data:

B4X:
Sub AStream_NewData (Buffer() As Byte)
  LogMessage("You", BConv.HexFromBytes(Buffer))
End Sub
 
Upvote 0

Beja

Expert
Licensed User
Longtime User
Hi guys,
Not familiar with bluetooth, but I feel like the first example above is missing something.. I don't see in the code a device ID.. in other words, to which bluetooth device the hex data is sent? or may be there is unlisted code and the device was already detected and referenced in other module or sub.
thanks in advance for your explanation.
 
Upvote 0

raphael75

Active Member
Licensed User
Longtime User
First you must download the Bluetooth Chat example (Bluetooth.zip file at the bottom of the first post) and unzip the file.
Then you open the Bluetooth.b4a file, you go to the ChatActivity module and you modify the three Sub (Globals, btnSend_Click and AStream_NewData) as shown above.
 
Upvote 0

Beja

Expert
Licensed User
Longtime User
Thanks raphaelcno, you found the solution in just a couple of lines.
I have two problems though!
1- There are 3 control bytes + a checksum byte in the beginning of the arrived array of bytes (00 00 00 nn). This is very
good as simple CRC for transmission validation, but unfortunately my MCU controller board is designed differently and
its firmware can not parse these additional bytes (at this time). Is there any way to get rid of them? I removed the "prefix" but
didn't work.
2- When I send back bytes to the Android device, the first transmission doesn't appear, but the second causes the app
to shut down. Is there any explanation?

Thanks in advance.
 
Upvote 0

Beja

Expert
Licensed User
Longtime User
1- The 4 bytes are transmitted along with the actual data from my cell phone to the PC.
Please see the attached screen shot

2- What didn't work is the whole app when I removed the "Prefix"
 

Attachments

  • comm.jpg
    comm.jpg
    48 KB · Views: 361
Upvote 0

Beja

Expert
Licensed User
Longtime User
forgot to say the 4th. byte is the count of the transmitted data.. in the above case 5 bytes.
 
Upvote 0

Beja

Expert
Licensed User
Longtime User
This is how I did it.. pls let me know if missing anything.


B4X:
If AStream.IsInitialized = False Then
        'AStream.InitializePrefix(Main.serial1.InputStream, True, Main.serial1.OutputStream, "AStream")
        AStream.Initialize(Main.serial1.InputStream, True, Main.serial1.OutputStream, "AStream")
    End If
 
Last edited:
Upvote 0

raphael75

Active Member
Licensed User
Longtime User
You must remove the second parameter "True" when you use Initialize instead of InitializePrefix.
Here is the correct code:
B4X:
AStream.Initialize(Main.serial1.InputStream, Main.serial1.OutputStream, "AStream")
 
Upvote 0

Beja

Expert
Licensed User
Longtime User
raphaelcno,
Now perfect.. many thanks to you. I will need the prefix mode though but when I manage to modify the controller's
firmware.
Thanks again..
 
Upvote 0
Top