techknight Well-Known Member Licensed User Longtime User Apr 9, 2012 #1 I might have missed it somewhere, but how do you send and receive hex data over the async prefix mode?
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 Apr 9, 2012 #2 To send hex data you can use this (modified from Bluetooth Chat example): Code: Copy 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: Code: Copy Sub AStream_NewData (Buffer() As Byte) LogMessage("You", BConv.HexFromBytes(Buffer)) End Sub Upvote 0
To send hex data you can use this (modified from Bluetooth Chat example): Code: Copy 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: Code: Copy Sub AStream_NewData (Buffer() As Byte) LogMessage("You", BConv.HexFromBytes(Buffer)) End Sub
techknight Well-Known Member Licensed User Longtime User Apr 9, 2012 #3 Yes, I had figured it out, i had to go download the library though. Thanks. Upvote 0
Beja Expert Licensed User Longtime User Nov 16, 2012 #4 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
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.
raphael75 Active Member Licensed User Longtime User Nov 16, 2012 #5 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
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.
Beja Expert Licensed User Longtime User Oct 2, 2013 #6 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
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.
raphael75 Active Member Licensed User Longtime User Oct 2, 2013 #7 Beja said: There are 3 control bytes + a checksum byte in the beginning of the arrived array of bytes (00 00 00 nn). Click to expand... Are these 4 bytes sent from the microcontroller or from the Android device? Beja said: I removed the "prefix" but didn't work. Click to expand... What didn't work? Upvote 0
Beja said: There are 3 control bytes + a checksum byte in the beginning of the arrived array of bytes (00 00 00 nn). Click to expand... Are these 4 bytes sent from the microcontroller or from the Android device? Beja said: I removed the "prefix" but didn't work. Click to expand... What didn't work?
Beja Expert Licensed User Longtime User Oct 2, 2013 #8 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 48 KB · Views: 603 Upvote 0
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"
Beja Expert Licensed User Longtime User Oct 2, 2013 #9 forgot to say the 4th. byte is the count of the transmitted data.. in the above case 5 bytes. Upvote 0
raphael75 Active Member Licensed User Longtime User Oct 2, 2013 #10 These 4 bytes are used in prefix mode, so you have to remove prefix mode. I don't know why the application stops working in your case. Can you try the Bluetooth example http://www.b4x.com/android/forum/threads/android-bluetooth-bluetoothadmin-tutorial.14768/ and just remove the prefix mode to see if it works? Upvote 0
These 4 bytes are used in prefix mode, so you have to remove prefix mode. I don't know why the application stops working in your case. Can you try the Bluetooth example http://www.b4x.com/android/forum/threads/android-bluetooth-bluetoothadmin-tutorial.14768/ and just remove the prefix mode to see if it works?
Beja Expert Licensed User Longtime User Oct 2, 2013 #11 This is how I did it.. pls let me know if missing anything. Code: Copy 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: Oct 2, 2013 Upvote 0
This is how I did it.. pls let me know if missing anything. Code: Copy 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
raphael75 Active Member Licensed User Longtime User Oct 2, 2013 #12 You must remove the second parameter "True" when you use Initialize instead of InitializePrefix. Here is the correct code: Code: Copy AStream.Initialize(Main.serial1.InputStream, Main.serial1.OutputStream, "AStream") Upvote 0
You must remove the second parameter "True" when you use Initialize instead of InitializePrefix. Here is the correct code: Code: Copy AStream.Initialize(Main.serial1.InputStream, Main.serial1.OutputStream, "AStream")
Beja Expert Licensed User Longtime User Oct 2, 2013 #13 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
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..