Android Question Bluetooth module not recvieving data sent from B4A program

dmtulsa

Member
Licensed User
Longtime User
Hello. I am using the example code for Bluetooth classic to try to send and receive data (simple strings)' The ESP32 module connects fine but data from the b4a program is not sent or received. If I use a bluetooth terminal app connected to the esp32 using the same firmware it works fine.
It must be something stupid I'm doing wrong but I just don't see it. The data looks fine when its sent (Using Debug in b4a).
Attached are the b4a and esp32 code
Thank you
 

Attachments

  • b4acode.zip
    13.3 KB · Views: 229
  • ESP32code.txt
    1.1 KB · Views: 184

emexes

Expert
Licensed User
data from the b4a program is not sent or received.
Probably because of this:
B4X:
Private Sub Serial_Connected (Success As Boolean)
   ...
        'prefix mode! Change to non-prefix mode if communicating with non-B4X device.
        AStream.InitializePrefix(serial.InputStream, False, serial.OutputStream, "astream")
(although usually that fails with a "message too large" error...)

Prefix mode prepends each write with a 32-bit binary packet size, ie the size of the written buffer. If you receive normal text ("simple strings"?) then the first four bytes are interpreted as a number, usually a huge one eg zillions, and prefix mode then waits for that many bytes to be received. Which they never are.

The fix is to use AStream.Initialize (ie no Prefix).
It must be something stupid I'm doing wrong but I just don't see it.
It is a well-worn path, and you're in good company :)
Bluetooth classic
Thinking ahead: bear in mind that that Apple have extra hurdles to jump for using Bluetooth classic in released apps. This is the reason that, eg, many relay boards on Ebay have only an Android app. Consider using BLE.
 
Upvote 0

dmtulsa

Member
Licensed User
Longtime User
Well I haven't gotten that far yet. I've tried for a couple days to figure out whats wrong but I just don't see it I still cant send or receive data. I can with a terminal program so I'm sure the other end is working.
I do have AStream.InitializePrefix in the serial connect sub.
Attached is my code fbased on the serial bluetooth example
 

Attachments

  • Bluetooth.zip
    285.6 KB · Views: 203
Upvote 0

emexes

Expert
Licensed User
I've tried for a couple days to figure out whats wrong but I just don't see it I still cant send or receive data.
Ouch. That's no good. I don't know how well our timezones match up, but let's get this sorted ASAP. I'll be back soon :)
 
Upvote 0

emexes

Expert
Licensed User
I was slightly confused that there was no .B4A file in that zip, presumed that you didn't want all your code out in public, maybe there's some company IP in there or something. No worries, because BluetoothManager.bas is our first line of attack:
B4X:
Else
    If AStream.IsInitialized Then AStream.Close
    'prefix mode! Change to non-prefix mode if communicating with non-B4X device.
    'prefix mode! Change to non-prefix mode if communicating with non-B4X device.
    'AStream.InitializePrefix(serial.InputStream, False, serial.OutputStream, "astream")

    '''AStream.InitializePrefix(serial.InputStream, False, serial.OutputStream, "astream")    '***** COMMENT OUT THIS LINE AND *****
    AStream.Initialize(serial.InputStream, serial.OutputStream, "astream")    '***** REPLACE IT WITH THIS LINE *****
    StartActivity(control)
End If
Give that a burl, post the results :)
 
Upvote 0

dmtulsa

Member
Licensed User
Longtime User
There should have been a .b4s in the zip. I'll attach it here. Nothing in it that everyone can't see. I'll try your suggestion now.

Well sir I do thank you very much .The false should not have been there. I could have searched for a month and not found that. That fixed it!!

Thank you
 

Attachments

  • Bluetooth.zip
    5.3 KB · Views: 232
Upvote 0
Top