Android Question Bluetooth module not recvieving data sent from B4A program

Discussion in 'Android Questions' started by dmtulsa, Jul 26, 2019.

  1. dmtulsa

    dmtulsa Member Licensed 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
     

    Attached Files:

  2. emexes

    emexes Well-Known Member Licensed User

    Probably because of this:
    Code:
    Private Sub Serial_Connected (Success As Boolean)
       ...
            
    'prefix mode! Change to non-prefix mode if communicating with non-B4X device.
            AStream.InitializePrefix(serial.InputStream, Falseserial.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 is a well-worn path, and you're in good company :)
    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.
     
    Erel and Shelby like this.
  3. dmtulsa

    dmtulsa Member Licensed User

    Thank you
     
  4. emexes

    emexes Well-Known Member Licensed User

  5. dmtulsa

    dmtulsa Member Licensed 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
     

    Attached Files:

  6. emexes

    emexes Well-Known Member Licensed User

    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 :)
     
  7. emexes

    emexes Well-Known Member Licensed User

    Although ^^^^^^^^^^^^^^ is the first usual suspect, per post #3...
     
  8. emexes

    emexes Well-Known Member 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:
    Code:
    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 :)
     
  9. dmtulsa

    dmtulsa Member Licensed 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
     

    Attached Files:

Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice